# Clamped Cubic Spline

**URL:** https://discourse.julialang.org/t/clamped-cubic-spline/90222
**Category:** New to Julia
**Tags:** question, splines
**Created:** [November 14, 2022, 3:53am UTC](https://discourse.julialang.org/t/clamped-cubic-spline/90222 "2022-11-14T03:53:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![JHall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jhall/32/206240_2.png) [@JHall](https://discourse.julialang.org/u/JHall)
#### Post date: [November 14, 2022, 3:53am UTC](https://discourse.julialang.org/t/clamped-cubic-spline/90222/1 "2022-11-14T03:53:49Z")

</div>

Hi all,

I am trying to use cubic spline interpolation where I specify the derivatives at the endpoint. The functions I am trying to interpolate with are value functions and thus often have poles at 0. As a consequence, I sometimes run into weird behavior with say normal splines. I had thought that I perhaps I could improve the quality of the interpolation by specifying that the derivatives of the spline interpolation at the endpoints of the grid be equal to the derivative of the utility function.

I have read the documentation of Interpolations, and it doesn’t seem to me that it supports clamped splines in this way, but happy to be told I am wrong!

Thanks!

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [November 14, 2022, 9:29am UTC](https://discourse.julialang.org/t/clamped-cubic-spline/90222/2 "2022-11-14T09:29:31Z")

</div>

I’m not currently aware of the ability to do this in Interpolations.jl. Note there are many other interpolation packages.

For example, BasicInterpolators.jl seems to do what you want:  
[https://markmbaum.github.io/BasicInterpolators.jl/dev/1d/#BasicInterpolators.CubicSplineInterpolator](https://markmbaum.github.io/BasicInterpolators.jl/dev/1d/#BasicInterpolators.CubicSplineInterpolator)

---

<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: [November 14, 2022, 2:21pm UTC](https://discourse.julialang.org/t/clamped-cubic-spline/90222/3 "2022-11-14T14:21:28Z")

</div>

> [@JHall](#):
>
> The functions I am trying to interpolate with are value functions and thus often have poles at 0.

If you know _a priori_ that you have a pole at zero, I would try to remove this singularity analytically before doing a spline fit.

For example, if you have data (x\_k, y\_k) for f(x) which has a known simple pole at x=0, I would instead do a spline fit for x \times f(x), i.e. fit (x\_k, x\_k \times y\_k) to a spline s(x), and your desired function is s(x) / x.

---

<div class="post-metadata">

### Author: ![JHall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jhall/32/206240_2.png) [@JHall](https://discourse.julialang.org/u/JHall)
#### Post date: [November 15, 2022, 1:33am UTC](https://discourse.julialang.org/t/clamped-cubic-spline/90222/4 "2022-11-15T01:33:17Z")

</div>

Thank you, this is certainly a different idea from what I had in mind but I will try it!

---

<div class="post-metadata">

### Author: ![markmbaum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markmbaum/32/32745_2.png) [@markmbaum](https://discourse.julialang.org/u/markmbaum)
#### Post date: [November 29, 2022, 4:43pm UTC](https://discourse.julialang.org/t/clamped-cubic-spline/90222/5 "2022-11-29T16:43:14Z")

</div>

Yep, there’s an easy constructor for clamped cubic splines. You can do

```julia
using BasicInterpolators

interp = CubicSplineInterpolator(
    x, #grid points (vector)
    y, #grid values (vector),
    d1, #derivative at left boundary (scalar),
    dn, #derivative at right boundary (scalar)
)

```
