Cubic Spline Interpolation

Cubic spline interpolation is a special case for Spline interpolation that is used very often to avoid the problem of Runge's phenomenon. This method gives an interpolating polynomial that is smoother and has smaller error than some other interpolating polynomials such as Lagrange polynomial and Newton polynomial.

Definition
Given a set of n + 1 data points (xi,yi) where no two xi are the same and $$a=x_0<x_1<\cdots<x_n=b$$, the spline S(x) is a function satisfying:
 * 1) $$S(x)\in C^2[a,b]$$;
 * 2) On each subinterval $$[x_{i-1},x_{i}], S(x)$$ is a polynomial of degree 3, where $$i=1,\dotsc,n.$$
 * 3) $$S(x_i)=y_i,$$ for all $$i=0,1,\dotsc,n.$$

Let us assume that

$$S(x)=\left\{\begin{array}{rl} C_1(x), & x_0 \leq x \leq x_1\\ \cdots &\\ C_i(x), & x_{i-1} < x \leq x_i\\ \cdots &\\ C_n(x), & x_{n-1} < x \leq x_n\end{array}\right.$$

where each $$C_i=a_i+b_ix+c_ix^2+d_ix^3 (d_i \neq 0)$$ is a cubic function, $$i=1,\cdots,n$$.

Boundary Conditions
To determine this cubic spline S(x), we need to determine $$a_i, b_i, c_i \mbox{ and } d_i$$ for each i by:
 * $$C_i(x_{i-1})= y_{i-1}$$ and $$C_i(x_{i})= y_{i}$$, $$i=1,\dotsc,n$$.
 * $$C^{'}_i(x_{i})=C^{'}_{i+1}(x_{i})$$, $$i=1,\dotsc,n-1$$.
 * $$C^{}_i(x_{i})=C^{}_{i+1}(x_{i}) $$, $$i=1,\dotsc,n-1$$.

We can see that there are $$n+n+(n-1)+(n-1)=4n-2$$ conditions, but we need to determine $$4n$$ coefficients, so usually we add two boundary conditions to solve this problem.

There are three types of common boundary conditions:

Methods
There are several methods that can be used to find the spline function S(x) according to its corresponding conditions. Since there are 4n coefficients to determine with 4n conditions, we can easily plug the values we know into the 4n conditions and then solve the system of equations. Note that all the equations are linear with respect to the coefficients, so this is workable and computers can do it quite well.

The algorithm given in Spline interpolation is also a method by solving the system of equations to obtain the cubic function in the symmetrical form.

The other method used quite often is Cubic Hermite spline, this gives us the spline in Hermite form.

Here, we discuss another method using second derivatives $$S''(x_i)=M_i (i=0,1,\cdots,n)$$ to find the expression for spline $$S(x)$$.

Let $$h_i=x_{i}-x_{i-1}$$, $$i=1,\cdots,n$$, $$S(x_i)=C_i(x_i)=C_{i+1}(x_i)=M_i (i=1,\cdots,n-1)$$ and $$S(x_0)=C_1(x_0)=M_0, \mbox{ and } S(x_n)=C''_n(x_n)=M_n$$. Note that $$M_i$$'s are unknown (except for type II boundary condition, $$M_0 \mbox{ and } M_n$$ are given).

Since each $$C_i$$ is a cubic polynomial, $$C''_i$$ is linear.

By Lagrange interpolation, we can interpolate each $$C_i$$ on $$[x_{i-1},x_i]$$ since $$C_i(x_{i-1})=M_{i-1}$$ and $$C''_i(x_{i})=M_{i}$$, the Lagrange form of this interpolating polynomial is:
 * $$C''_i(x)=M_{i-1}\frac{x_{i}-x}{h_i}+M_i\frac{x-x_{i-1}}{h_i}$$ for $$x\in [x_{i-1},x_i]$$.

Integrating the above equation twice and using the condition that $$C_i(x_{i-1})=y_{i-1}$$ and $$C_i(x_{i})=y_{i}$$ to determine the constants of integration, we have

This expression gives us the cubic spline S(x) if $$M_i, i=0,1,\cdots,n$$ can be determined.

For $$i=1,\cdots,n-1,$$ when $$x\in [x_{i},x_{i+1}],$$, we can calculate that
 * $$C'_{i+1}(x)=-M_i\frac{(x_{i+1}-x)^2}{2h_{i+1}}+M_{i+1}\frac{(x-x_i)^2}{2h_{i+1}}+\frac{y_{i+1}-y_i}{h_{i+1}}-\frac{M_{i+1}-M_i}{6}h_{i+1}.$$

Therefore, $$C'_{i+1}(x_i)=-M_i\frac{h_{i+1}}{2}+\frac{y_{i+1}-y_i}{h_{i+1}}-\frac{M_{i+1}-M_i}{6}h_{i+1}.$$

Similarly, when $$x\in [x_{i-1},x_{i}],$$, we can shift the index to obtain

Thus, $$C'_{i}(x_i)=M_i\frac{h_{i}}{2}+\frac{y_{i}-y_{i-1}}{h_{i}}-\frac{M_{i}-M_{i-1}}{6}h_{i}.$$

Since $$C'_{i+1}(x_i)=C'_{i}(x_i)$$, we can derive

where

and $$f[x_{i-1},x_i,x_{i+1}]$$ is a divided difference.

According to different boundary conditions, we can solve the system of equations above to obtain the values of $$M_i$$'s.

I. For type I boundary condition, we are given $$C'_1(x_0)=f'_0$$ and $$C'_n(x_n)=f'_n$$. According to equation ($$), we can obtain
 * $$C'_{1}(x_0)=-M_{0}\frac{(x_{1}-x_0)^2}{2h_{1}}+M_{1}\frac{(x_0-x_{0})^2}{2h_{1}}+\frac{y_{1}-y_{0}}{h_{1}}-\frac{M_{1}-M_{0}}{6}h_{1}.$$
 * $$\Rightarrow f'_0=-M_{0}\frac{h_1}{2}+f[x_0,x_1]-\frac{M_{1}-M_{0}}{6}h_{1}$$

Similarly, simplifying
 * $$C'_{n}(x_n)=-M_{n-1}\frac{(x_{n}-x_n)^2}{2h_{n}}+M_{n}\frac{(x_n-x_{n-1})^2}{2h_{n}}+\frac{y_{n}-y_{n-1}}{h_{n}}-\frac{M_{n}-M_{n-1}}{6}h_{n}$$

we will have

Therefore, let $$\lambda_0=\mu_n=1, d_0=6f[x_0,x_0,x_1]$$ and $$d_n=6f[x_{n-1},x_n,x_n]$$, combine ($$), ($$) and ($$) together, so the system of equations that we need to solve is

II. For type II boundary condition, we are given

directly, so let $$\lambda_0=\mu_n=0$$, $$d_0=2f_0$$, and $$d_n=2f_n$$, and we need to solve the system of equations in the same form as ($$).

Example
For points (0,0),(1,0.5), (2,2) and (3,1.5), find the interpolating cubic spline $$S(x)$$ satisfying $$S'(0)=0.2$$ and $$S'(3)=-1$$.

Solution: We can easily see that $$h_i=1$$ for $$i=1,2,3$$ so $$\lambda_0=1, \lambda_1=\lambda_2=\mu_1=\mu_2=\frac{1}{2}$$ and $$\mu_3=1.$$

Also, since this is the type I boundary condition problem, we can calculate that
 * $$f[x_0,x_1,x_0]=f[x_0,x_0,x_1]=(f[x_1,x_0]-f[x_0,x_0])/(x_1-x_0)=\left(\frac{0.5-0}{1-0}-0.2\right)/1=0.3;$$
 * $$f[x_0,x_1,x_2]=f[x_0,x_1,x_2]=(f[x_2,x_1]-f[x_1,x_0])/(x_2-x_0)=\left(\frac{2-0.5}{2-1}-\frac{0.5-0}{1-0}\right)/2=0.5;$$
 * $$f[x_1,x_2,x_3]=f[x_1,x_2,x_3]=(f[x_3,x_2]-f[x_2,x_1])/(x_3-x_1)=\left(\frac{1.5-2}{3-2}-\frac{2-0.5}{2-1}\right)/2=-1;$$ and
 * $$f[x_3,x_2,x_3]=f[x_2,x_3,x_3]=(f[x_3,x_3]-f[x_2,x_3])/(x_3-x_2)=\left(-1-\frac{1.5-2}{3-2}\right)/1=-\frac{1}{2}.$$

Therefore, plug into the system of equations, we have
 * $$\left[\begin{array}{cccc}

2 & 1 & & \\ 1/2 & 2 & 1/2 & \\ & 1/2 & 2 & 1/2\\ & & 1 & 2\end{array} \right] \left[\begin{array}{c} M_0 \\ M_1 \\ M_2\\ M_3\end{array} \right]= \left[\begin{array}{c} 6\times 0.3 \\ 6\times 0.5 \\ 6\times (-1)\\ 6\times (-1/2)\end{array} \right].$$ The solution is $$M_0=-0.36, M_1=2.52, M_2=-3.72$$ and $$M_3=0.36.$$

Therefore, by the general expression of the solution, we have
 * $$\begin{align}

C_1(x)&=M_0\frac{(x_1-x)^3}{6}+M_1\frac{(x-x_0)^3}{6}+(y_{0}-\frac{M_{0}}{6})\frac{x_1-x}{1}+(y_{1}-\frac{M_{1}}{6})\frac{x-x_{0}}{1}\\ &=-0.36\frac{(1-x)^3}{6}+2.52\frac{x^3}{6}+\frac{0.36}{6}(1-x)+(0.5-\frac{2.52}{6})x\\ &=0.06(x-1)^3+0.42x^3+0.06(1-x)+0.08x\\ &=0.48x^3-0.18x^2+0.2x\,.\end{align} $$ Similarly,
 * $$C_2(x)=-1.04(x-1)^3+1.26(x-1)^2+1.28(x-1)+0.5,$$ and
 * $$C_3(x)=0.68(x-2)^3-1.86(x-2)^2+0.68(x-2)+2.$$

Thus the cubic spline is
 * $$S(x)=\left\{\begin{array}{rl}

0.48x^3-0.18x^2+0.2x, & 0 \leq x \leq 1\\ -1.04(x-1)^3+1.26(x-1)^2+1.28(x-1)+0.5, & 1 < x \leq 2\\ 0.68(x-2)^3-1.86(x-2)^2+0.68(x-2)+2, & 2 < x \leq 3\end{array}\right.$$

Exercise
{ For points (0,0), (1,0.5), (2,2) and (3,1.5), find the interpolating cubic spline $$S(x)$$ satisfying $$S(0)=-0.3$$ and $$S(3)=3.3$$.
 * type="{}"}

Since this is the type II boundary condition, we use

$$M_0=$$ { -0.3 } and $$M_3=$$ { 3.3 }.

Also, we have

$$\lambda_0=$$ { 0.0 }, $$\mu_3=$$ { 0.0 }, $$d_0=$$ { -0.6 } and $$d_3=$$ { 6.6 }.

Same as the above example, we have

$$\lambda_1=\lambda_2=\mu_1=\mu_2=$$ { 0.5 } and

$$d_1=$$ { 3 } and $$d_2=$$ { -6 }.

Therefore, we can construct the system of equations:

Solution:
 * $$\left[\begin{array}{cccc}

2 & 0 & & \\ 1/2 & 2 & 1/2 & \\ & 1/2 & 2 & 1/2\\ & & 0 & 2\end{array} \right] \left[\begin{array}{c} M_0 \\ M_1 \\ M_2\\ M_3\end{array} \right]= \left[\begin{array}{c} -0.6 \\ 3 \\ -6\\ 6.6\end{array} \right].$$

Since $$M_0=-0.3$$ and $$M_3=3.3$$, we can find the solution:


 * $$M_1=2.7$$ and $$M_2=-4.5$$.

Plug these into the general expression for cubic spline and simplify, we can obtain


 * $$S(x)=\left\{\begin{array}{rl}

0.5x^3-0.15x^2+0.15x, & 0 \leq x \leq 1\\ -1.2(x-1)^3+1.35(x-1)^2+1.35(x-1)+0.5, & 1 < x \leq 2\\ 1.3(x-2)^3-2.25(x-2)^2+0.45(x-2)+2, & 2 < x \leq 3\end{array}\right.$$

You can see the difference of the two cubic splines in Figure 1.