# Cubicspline Interpolation where domain is non-uniform array

**URL:** <https://discourse.julialang.org/t/cubicspline-interpolation-where-domain-is-non-uniform-array/63864>\
**Category:** New to Julia\
**Tags:** interpolations\
**Created:** [June 30, 2021, 11:16pm UTC](https://discourse.julialang.org/t/cubicspline-interpolation-where-domain-is-non-uniform-array/63864 "2021-06-30T23:16:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![donkeysaddle](https://avatars.discourse-cdn.com/v4/letter/d/3e96dc/32.png) [@donkeysaddle](https://discourse.julialang.org/u/donkeysaddle)\
**Post date:** [June 30, 2021, 11:16pm UTC](https://discourse.julialang.org/t/cubicspline-interpolation-where-domain-is-non-uniform-array/63864/1 "2021-06-30T23:16:11Z")

</div>

Perhaps this is the answer to my question: [Using CubicSplineInterpolation on a vector (not on a Lazy Array)](https://discourse.julialang.org/t/using-cubicsplineinterpolation-on-a-vector-not-on-a-lazy-array/40044)

but wanted to make sure…

```julia
using Interpolations

function f(x)
    return x/2
end

times = [1,20,100,150,200,238,300,400,540,600,707,800,1000,1200,1400]
vals = [f(i) for i in times]
itp = CubicSplineInterpolation(times,vals)

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 1, 2021, 10:44pm UTC](https://discourse.julialang.org/t/cubicspline-interpolation-where-domain-is-non-uniform-array/63864/2 "2021-07-01T22:44:42Z")

</div>

With **Interpolations.jl** , for cubic spline interpolation on irregular grid:

```julia
using Interpolations

f(x) = 5e-3*x^2

# input data to interpolate on non-uniform grid
times = [1.,20,100,150,200,238,300,400,540,600,707,800,1000,1200,1400]
vals = f.(times)

# Interpolations with parametric cubic splines
t = LinRange(0,1,length(times))
itp = Interpolations.scale(interpolate(hcat(times,vals), (BSpline(Cubic(Natural(OnGrid()))), NoInterp())), t, 1:2)
ti = 0:.01:1
timesitp, valsitp = [itp(t,1) for t in ti], [itp(t,2) for t in ti]

# Plot results
scatter(times,vals, legend=:topleft, label="f(t) = 5e-3*t^2")
plot!(timesitp, valsitp, xlabel="times", ylabel="vals", label="Interpolations' parametric spline")

```

![Interpolations_parametric_splines_irregular_grid](https://global.discourse-cdn.com/julialang/original/3X/6/c/6c65e8b470ff8c28c9219e08a01d2fc6b5bd2a55.png)

---

<div class="post-metadata">

**Author:** ![donkeysaddle](https://avatars.discourse-cdn.com/v4/letter/d/3e96dc/32.png) [@donkeysaddle](https://discourse.julialang.org/u/donkeysaddle)\
**Post date:** [July 1, 2021, 10:50pm UTC](https://discourse.julialang.org/t/cubicspline-interpolation-where-domain-is-non-uniform-array/63864/3 "2021-07-01T22:50:35Z")

</div>

Thanks! This looks like it’s probably exactly what I am after.

Unfortunately, I don’t understand any of it…

Regardless, thanks @rafael.guerra!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 1, 2021, 10:54pm UTC](https://discourse.julialang.org/t/cubicspline-interpolation-where-domain-is-non-uniform-array/63864/4 "2021-07-01T22:54:12Z")

</div>

Do not worry, me neither 🙂

Fyi, the syntax may look more user friendly in Dierckx.jl or BSplineKit.jl, you may want to take a look there too. And for the example provided, there is no need to go parametric on those packages (_could not find another way in Interpolations.jl though_).

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 1, 2021, 11:10pm UTC](https://discourse.julialang.org/t/cubicspline-interpolation-where-domain-is-non-uniform-array/63864/5 "2021-07-01T23:10:42Z")

</div>

Using Dierckx.jl, in non-parametric mode (probably more useful for your use case):

```julia
using Dierckx

f(x) = 5e-3*x^2

# input data to interpolate on non-uniform grid
times = [1.,20,100,150,200,238,300,400,540,600,707,800,1000,1200,1400]
vals = f.(times)

spl = Spline1D(times, vals)

ti = 1:0.5:1400
vi = spl(ti)

```

---

<div class="post-metadata">

**Author:** ![donkeysaddle](https://avatars.discourse-cdn.com/v4/letter/d/3e96dc/32.png) [@donkeysaddle](https://discourse.julialang.org/u/donkeysaddle)\
**Post date:** [July 1, 2021, 11:12pm UTC](https://discourse.julialang.org/t/cubicspline-interpolation-where-domain-is-non-uniform-array/63864/6 "2021-07-01T23:12:18Z")

</div>

@rafael.guerra the age old dilemma - when to get to the bottom of something vs. when to just use it and move on…

Thanks so much! Really appreciate it.

---

<div class="post-metadata">

**Author:** ![donkeysaddle](https://avatars.discourse-cdn.com/v4/letter/d/3e96dc/32.png) [@donkeysaddle](https://discourse.julialang.org/u/donkeysaddle)\
**Post date:** [July 7, 2021, 1:35am UTC](https://discourse.julialang.org/t/cubicspline-interpolation-where-domain-is-non-uniform-array/63864/7 "2021-07-07T01:35:38Z")

</div>

Perhaps I should open a new issue, but what if I want to get the derivatives of valsitp w.r.t. to timesitp?

One idea, let’s call valsitp, y(t), and timesitp, x(t), I could create two separate interpolators for each of these. Then I can easily get dy/dt & dx/dt using Interpolations.gradient. Once I’ve done this, (dy/dt)/(dx/dt) would give me dy/dx, but it’s not ideal…

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 7, 2021, 5:51am UTC](https://discourse.julialang.org/t/cubicspline-interpolation-where-domain-is-non-uniform-array/63864/8 "2021-07-07T05:51:10Z")

</div>

With Dierckx, do :  
`derivative(spl, times)`

---

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [February 22, 2022, 5:39am UTC](https://discourse.julialang.org/t/cubicspline-interpolation-where-domain-is-non-uniform-array/63864/9 "2022-02-22T05:39:19Z")

</div>

Is there a way to construct the interpolation to evaluate at a specific `times` given the exact same example you posted above? It seems like this would be a very basic use case and yet I’m having a hell of a time figuring out to construct this using `Interpolations.jl` . I know there are other packages out there but I’d like to see if I can get the native julia library implemented in my workflow. I can’t seem to find this posted elsewhere or in the documentation.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 22, 2022, 5:59am UTC](https://discourse.julialang.org/t/cubicspline-interpolation-where-domain-is-non-uniform-array/63864/10 "2022-02-22T05:59:33Z")

</div>

Could you use [BSplineKit.jl](https://discourse.julialang.org/t/best-way-to-take-derivatives-of-unevenly-spaced-data-with-interpolations-discrete-derivatives/54097/6)?  
It is native Julia and well supported.

---

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [February 22, 2022, 5:30pm UTC](https://discourse.julialang.org/t/cubicspline-interpolation-where-domain-is-non-uniform-array/63864/11 "2022-02-22T17:30:48Z")

</div>

@rafael.guerra thanks for pointing me BSplineKit.jl… beautiful package… thanks @jipolanco for creating it
