# Boundary intervals for splines using CompactBases

**URL:** <https://discourse.julialang.org/t/boundary-intervals-for-splines-using-compactbases/71344>\
**Category:** General Usage\
**Tags:** package, splines\
**Created:** [November 11, 2021, 7:12pm UTC](https://discourse.julialang.org/t/boundary-intervals-for-splines-using-compactbases/71344 "2021-11-11T19:12:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan.phi](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@Dan.phi](https://discourse.julialang.org/u/Dan.phi)\
**Post date:** [November 11, 2021, 7:12pm UTC](https://discourse.julialang.org/t/boundary-intervals-for-splines-using-compactbases/71344/1 "2021-11-11T19:12:38Z")

</div>

I want to implement natural cubic splines in Julia via B-splines (which are a basis for splines). I found [CompactBases.jl](https://juliaapproximation.github.io/CompactBases.jl/dev/), which uses B-splines and allows me to evaluate the basis functions at a value `x`.

Now, let the set of knots be t = \{\zeta\_1, \dots, \zeta\_K\}. Splines split \mathbb{R} into K+1 segments, (-\infty, \zeta\_1], (\zeta\_1,\zeta\_2],\dots,(\zeta\_K, \infty) and use polynomials (here cubic) in each segment. Natural splines make the function linear in the boundary segments, (-\infty, \zeta\_1] and (\zeta\_K,\infty).

However, when I implement them via CompactBases, the polynomial seems to be of order zero (a constant) rather than one (linear). See the below MWE, where I try to approximate \sin{x} over -6:6 but let t be in -5:5.

Outside -5:5, the approximation is just constant (at zero). Any help would be appreciated.

```julia
using Plots
using CompactBases

t = LinearKnotSet(4, -5, 5, 3)
B = BSpline(t)

x = [-6:0.01:6;]
y = sin.(x)
c = B[x,:] \ y

X = B[x,:]
approx = X*c

plot(x,[y, approx])

```

![fig](https://global.discourse-cdn.com/julialang/original/3X/3/1/31d70797def1f7d3aaa4d5cadfb167ac1ef9194e.png)

---

<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:** [November 12, 2021, 8:18am UTC](https://discourse.julialang.org/t/boundary-intervals-for-splines-using-compactbases/71344/2 "2021-11-12T08:18:31Z")

</div>

Could you ellaborate why linear polynomials are expected?

That code deals with B-splines which use cubic polynomials as basis functions (_when k=4_) . In your example, these can be plotted with:

```julia
plot(X, legend=:outerright)

```

![CompactBases_B-splines_basis_function](https://global.discourse-cdn.com/julialang/original/3X/6/4/649aa1e01973ea93ded4407a568ae1e6950ff0cb.png)

It is not surprising that such package will only work within the compact support defined but you can contact the author at [CompactBases.jl github](https://github.com/JuliaApproximation/CompactBases.jl) page.

---

<div class="post-metadata">

**Author:** ![Dan.phi](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@Dan.phi](https://discourse.julialang.org/u/Dan.phi)\
**Post date:** [November 12, 2021, 9:53am UTC](https://discourse.julialang.org/t/boundary-intervals-for-splines-using-compactbases/71344/3 "2021-11-12T09:53:18Z")

</div>

Not that they are expected, but I wanted to use the package to build natural splines. For that to be the case, there needs to be an option to choose the degree of the polynomials for the boundary intervals, and I was wondering whether it was possible.

---

<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:** [November 12, 2021, 11:45am UTC](https://discourse.julialang.org/t/boundary-intervals-for-splines-using-compactbases/71344/4 "2021-11-12T11:45:59Z")

</div>

OK, thanks.

Just in case, note that the `Interpolations.jl` package has natural cubic splines using `CubicSplineInterpolation()` and also natural cubic B-splines using something like `interpolate(x, BSpline(Cubic(Natural(OnGrid()))))`, whose second derivatives at the two endpoints are zero.

---

<div class="post-metadata">

**Author:** ![Dan.phi](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@Dan.phi](https://discourse.julialang.org/u/Dan.phi)\
**Post date:** [November 24, 2021, 11:44am UTC](https://discourse.julialang.org/t/boundary-intervals-for-splines-using-compactbases/71344/5 "2021-11-24T11:44:08Z")

</div>

Thank you for your suggestion. That would be great, but apparently `Interpolations.jl` does not allow to evaluate some vlaue `x` in the basis functions–or more generally the functions comprising the spline–directly (`CompactBases.jl` does, see my example above when calling `B[x,:]`).
