# How to calculate derivative by Lagrange interpolation

**URL:** https://discourse.julialang.org/t/how-to-calculate-derivative-by-lagrange-interpolation/74905
**Category:** Modelling & Simulations
**Tags:** question, package, diffeq
**Created:** [January 20, 2022, 4:59am UTC](https://discourse.julialang.org/t/how-to-calculate-derivative-by-lagrange-interpolation/74905 "2022-01-20T04:59:15Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![manoj.centura](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manoj.centura/32/216443_2.png) [@manoj.centura](https://discourse.julialang.org/u/manoj.centura)
#### Post date: [January 20, 2022, 4:59am UTC](https://discourse.julialang.org/t/how-to-calculate-derivative-by-lagrange-interpolation/74905/1 "2022-01-20T04:59:15Z")

</div>

hi All,

is there a pkg to calculate derivative at untabulated points using langrange interpolation?

Thanks  
M

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [January 20, 2022, 5:01am UTC](https://discourse.julialang.org/t/how-to-calculate-derivative-by-lagrange-interpolation/74905/2 "2022-01-20T05:01:18Z")

</div>

does it have to be lagrange interpolation? That’s not necessarily a stable method, for example there’s Runge’s phenomenon. [Runge's phenomenon - Wikipedia](https://en.wikipedia.org/wiki/Runge%27s_phenomenon)

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [January 20, 2022, 5:55am UTC](https://discourse.julialang.org/t/how-to-calculate-derivative-by-lagrange-interpolation/74905/3 "2022-01-20T05:55:13Z")

</div>

> [@manoj.centura](#):
>
> is there a pkg to calculate derivative at untabulated points using langrange interpolation?

I’d suspect the combination of [Polynomials.jl](https://juliamath.github.io/Polynomials.jl/stable/) and [SpecialPolynomials.jl](https://docs.juliahub.com/SpecialPolynomials/LrhA0/0.2.7/) could help.

---

<div class="post-metadata">

### Author: ![tobydriscoll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobydriscoll/32/1843_2.png) [@tobydriscoll](https://discourse.julialang.org/u/tobydriscoll)
#### Post date: [January 20, 2022, 8:33pm UTC](https://discourse.julialang.org/t/how-to-calculate-derivative-by-lagrange-interpolation/74905/4 "2022-01-20T20:33:41Z")

</div>

I’m not sure what you mean by “untabulated,” but [ApproxFun](https://github.com/JuliaApproximation/ApproxFun.jl) may be of help. Especially look at the documentation FAQ about values on predefined grids.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 20, 2022, 8:56pm UTC](https://discourse.julialang.org/t/how-to-calculate-derivative-by-lagrange-interpolation/74905/5 "2022-01-20T20:56:30Z")

</div>

> [@manoj.centura](#):
>
> is there a pkg to calculate derivative at untabulated points using langrange interpolation?

The numerically stable version of Lagrange interpolation is barycentric interpolation, which is implemented here: [GitHub - dawbarton/BarycentricInterpolation.jl: A Julia implementation of Barycentric interpolation and differentiation formulae](https://github.com/dawbarton/BarycentricInterpolation.jl) …always use this rather than Lagrange.

Beware that if you choose the interpolation points unwisely (e.g. equally spaced or randomly) then at high polynomial degrees you are likely to hit [Runge phenomena](https://en.wikipedia.org/wiki/Runge%27s_phenomenon) with _any_ polynomial interpolation method. The main ways around this is to (a) choose the points well, e.g. at Chebyshev nodes or (b) least-square fit to a lower-degree polynomial (e.g. fit to a degree-10 polynomial with 100 points). [GitHub - JuliaMath/FastChebInterp.jl: fast multidimensional Chebyshev interpolation and regression in Julia](https://github.com/stevengj/FastChebInterp.jl) can do both of those things for you, and there are other packages as well (e.g. ApproxFun can do it, but is really oriented towards other tasks than interpolating data).

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 4, 2022, 4:11pm UTC](https://discourse.julialang.org/t/how-to-calculate-derivative-by-lagrange-interpolation/74905/6 "2022-04-04T16:11:42Z")

</div>

[https://github.com/PumasAI/DataInterpolations.jl](https://github.com/PumasAI/DataInterpolations.jl)

It has an implementation of the Lagrange interpolation in Barycentric form (IIRC) and it specializes the derivative calculation as well.

[https://github.com/PumasAI/DataInterpolations.jl/blob/master/src/derivatives.jl#L55](https://github.com/PumasAI/DataInterpolations.jl/blob/master/src/derivatives.jl#L55)
