# Cubic splinesΒΆ

For illustration, here is a spline interpolant using just a few nodes.

using FundamentalsNumericalComputation

f = x -> exp(sin(7*x))

plot(f,0,1,label="function")

t = [0, 0.075, 0.25, 0.55, 0.7, 1]  # nodes
y = f.(t)                           # values at nodes

scatter!(t,y,label="nodes")

S = FNC.spinterp(t,y)

plot!(S,0,1,label="spline")


Now we look at the convergence rate as the number of nodes increases.

x = (0:10000)/1e4              # sample the difference at many points
n = @. round(Int,2^(3:0.5:8))  # numbers of nodes
err = zeros(0)
for n in n
t = (0:n)/n
S = FNC.spinterp(t,f.(t))
dif = @. f(x)-S(x)
push!(err,norm(dif,Inf))
end

pretty_table((n=n,error=err),backend=:html)

n error
Int64 Float64
8 0.030563368320724926
11 0.020756199827707045
16 0.005907614897266766
23 0.0013458709271774172
32 0.00036704942417364883
45 9.177847457819688e-5
64 2.1530596014285308e-5
91 5.042916653597018e-6
128 1.240124746004767e-6
181 3.0042607446212344e-7
256 7.319440786801579e-8

Since we expect convergence that is $$O(h^4)=O(n^{-4})$$, we use a log-log graph of error and expect a straight line of slope $$-4$$.

order4 = @. (n/n[1])^(-4)

plot(n,[err order4],m=[:o :none],l=[:solid :dash],label=["error" "4th order"],
xaxis=(:log10,"n"), yaxis=(:log10,"\$\\| f-S \\|_\\infty\$"),
title="Convergence of spline interpolation")