Piecewise polynomial interpolation

Let us repeat the data from Poor conditioning in polynomial interpolation but use piecewise polynomials constructed using the Interpolations package.

using FundamentalsNumericalComputation
Copy to clipboard
n = 12
t = LinRange(-1,1,n+1)
y = @. t^2 + t + 0.5*sin(20*t)

scatter(t,y,label="data",leg=:top)
Copy to clipboard

Here is an interpolant that is linear between each pair of consecutive nodes.

p = LinearInterpolation(t,y)
plot!(x->p(x),-1,1,label="piecewise linear")
Copy to clipboard

We may instead request a smoother interpolant that is piecewise cubic.

p = CubicSplineInterpolation(t,y)
plot!(x->p(x),-1,1,label="piecewise cubic")
Copy to clipboard