# Interpolations: boundary conditions for the second and first derivative

**URL:** https://discourse.julialang.org/t/interpolations-boundary-conditions-for-the-second-and-first-derivative/37036
**Category:** Modelling & Simulations
**Tags:** question, interpolations
**Created:** [April 4, 2020, 8:25pm UTC](https://discourse.julialang.org/t/interpolations-boundary-conditions-for-the-second-and-first-derivative/37036 "2020-04-04T20:25:07Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)
#### Post date: [April 4, 2020, 8:25pm UTC](https://discourse.julialang.org/t/interpolations-boundary-conditions-for-the-second-and-first-derivative/37036/1 "2020-04-04T20:25:07Z")

</div>

Is there a way to have a cubic interpolation with boundary conditions for the 2nd and 1st derivative?  
I am using [Interpolations.jl](http://juliamath.github.io/Interpolations.jl/latest/)

Here is an example

```julia
using Interpolations, Plots
x = 1:10
y = rand(10)
y[1] = 0
y[10] = 0
yitp = CubicSplineInterpolation(x, y, extrapolation_bc = Line())
xfine = 1:0.01:10
plot(xfine, yitp.(xfine))
scatter!(x, y)

```

![interpolations](https://global.discourse-cdn.com/julialang/original/3X/7/2/72ebe1ff6dd42c499e795e261f7e3e5914ce4719.png)

My goal is to have zero second and first derivatives in both boundary conditions.

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [April 4, 2020, 11:35pm UTC](https://discourse.julialang.org/t/interpolations-boundary-conditions-for-the-second-and-first-derivative/37036/2 "2020-04-04T23:35:30Z")

</div>

Your condition is already satisfied:

```julia
julia> f(x) = yitp(x);

julia> grad(x) = Interpolations.gradient(yitp, x)[1];

julia> lap(x) = Interpolations.hessian(yitp, x)[1];

julia> plot(xfine, [f, grad, lap], label=["f(x)" "f'(x)" "f''(x)"])

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/7/975465190a67b817da4b502f949eb6f2e4d1434a.png)

---

<div class="post-metadata">

### Author: ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)
#### Post date: [April 5, 2020, 7:30am UTC](https://discourse.julialang.org/t/interpolations-boundary-conditions-for-the-second-and-first-derivative/37036/3 "2020-04-05T07:30:32Z")

</div>

Of course you are right!  
I forgot to mention that I want the first derivative to be zero as well. I edited the question. Thanks!

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 5, 2020, 9:16am UTC](https://discourse.julialang.org/t/interpolations-boundary-conditions-for-the-second-and-first-derivative/37036/4 "2020-04-05T09:16:24Z")

</div>

You can do this if you solve for the coefficients of the B-splines explicitly using their values and derivatives at the gridpoints (as applicable in your problem), but AFAIK Interpolations.jl does not expose B-spline basis functions in its API.

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [April 6, 2020, 10:19am UTC](https://discourse.julialang.org/t/interpolations-boundary-conditions-for-the-second-and-first-derivative/37036/5 "2020-04-06T10:19:53Z")

</div>

See the different `prefiltering_system`s in `src/b-splines/cubic.jl`. You could add a new system.
