# Explanation of cubic spline interpolation

Can someone explain to me what a cubic spline is, and how we could use it to interpolate a function?

I have searched on the internet but I would like a simple explanation.

If you have a function $f(x)$ on some interval $[a,b]$, which is divided on $[x_{i-1}, x_i]$ such as $a=x_0< x_1< ... <x_N=b$ then you can interpolate this function by a cubic spline $S(x)$. $S(x)$ is a piecewise function: on each $h_i = x_i - x_{i-1}$ it's a cubic polynomial, which can be written for simplicity as $S_i(x) = a_i + b_i(x - x_i) + {c_i\over2}(x-x_i)^2 + {d_i\over6}(x - x_i)^3 \,\!$. It has to satisfy the next constraints:

1) passing through the knots : $S_i\left(x_{i}\right) = f(x_{i})$

2) be continuous up to the 2nd derivative:

$S_i\left(x_{i-1}\right) = S_{i-1}(x_{i-1}) \\ S'_i\left(x_{i-1}\right) = S'_{i-1}(x_{i-1}) \\ S''_i\left(x_{i-1}\right) = S''_{i-1}(x_{i-1})$

3) for natural splines:

$S''(a) = S''(b) = 0.$

These equations will uniquely define spline coefficients.

A good way to understand this is to take e.g. 3 points and manually solve systems for coefficients of $S_1(x)$ and $S_2(x)$

Finally you should get the next system:

$a_i = f\left(x_{i}\right) \,\!$

$h_ic_{i-1} + 2(h_i + h_{i+1})c_i + h_{i+1}c_{i+1} = 6\left({{f_{i+1} - f_i}\over{h_{i+1}}} - {{f_{i} - f_{i-1}}\over{h_{i}}}\right) \,\!$

$d_i = {{c_i - c_{i-1}}\over{h_i}} \,\!$

$b_i = {1\over2}h_ic_i - {1\over6}h_i^2d_i + {{f_i - f_{i-1}}\over{h_i}}= {{f_i - f_{i-1}}\over{h_i}} + {{h_i(2c_i + c_{i-1})}\over6} \,\!$

• Would it be easy for you to explain it to me through an example?Like if we have a function f(x)=(1/2)|x| and x_0=1,x_1=0,x_2=1 how we interpolate this by a cubic spline with s_1"(x_0)=s_1"(x_2)=0 ?? – cube Jan 11 '14 at 14:04
• it has to be s_0''(x_0) = 0 and s_3''(x_2) =0, which gives you c_0 = 0 and c_3=0. then you can use these values in the system, which I've added to the answer. Is it ok? – user2575760 Jan 11 '14 at 14:20