# 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} , 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} where each $C_{i}=a_{i}+b_{i}x+c_{i}x^{2}+d_{i}x^{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:

1. First derivatives at the endpoints are known:
$C_{1}^{'}(x_{0})=f_{0}^{'},{\mbox{ and }}C_{n}^{'}(x_{n})=f_{n}^{'}$ .
This is called clamped boundary conditions.
2. Second derivatives at the endpoints are known:
$C_{1}^{''}(x_{0})=f_{0}^{''},{\mbox{ and }}C_{n}^{''}(x_{n})=f_{n}^{''}$ .
The special case $C_{1}^{''}(x_{0})=C_{n}^{''}(x_{n})=0$ is called natural or simple boundary conditions.
3. When the exact function f(x) is a periodic function with period $x_{n}-x_{0}$ , S(x) is a periodic function with period $x_{n}-x_{0}$ too. Thus
$C_{1}(x_{0})=C_{n}(x_{n}),C_{1}^{'}(x_{0})=C_{n}^{'}(x_{n}),{\mbox{ and }}C_{1}^{''}(x_{0})=C_{n}^{''}(x_{n})$ .
The spline functions S(x) satisfying this type of boundary condition are called periodic splines.

## 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

$C_{i}(x)=M_{i-1}{\frac {(x_{i}-x)^{3}}{6h_{i}}}+M_{i}{\frac {(x-x_{i-1})^{3}}{6h_{i}}}+\left(y_{i-1}-{\frac {M_{i-1}h_{i}^{2}}{6}}\right){\frac {x_{i}-x}{h_{i}}}+\left(y_{i}-{\frac {M_{i}h_{i}^{2}}{6}}\right){\frac {x-x_{i-1}}{h_{i}}}\quad {\text{for}}\quad x\in [x_{i-1},x_{i}].$ (1)

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

$C'_{i}(x)=-M_{i-1}{\frac {(x_{i}-x)^{2}}{2h_{i}}}+M_{i}{\frac {(x-x_{i-1})^{2}}{2h_{i}}}+{\frac {y_{i}-y_{i-1}}{h_{i}}}-{\frac {M_{i}-M_{i-1}}{6}}h_{i}.$ (2)

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

$\mu _{i}M_{i-1}+2M_{i}+\lambda _{i}M_{i+1}=d_{i}\quad {\text{for}}\quad i=1,2,\cdots ,n-1,$ (3)

where

$\mu _{i}={\frac {h_{i}}{h_{i}+h_{i+1}}},\quad \lambda _{i}=1-\mu _{i}={\frac {h_{i+1}}{h_{i}+h_{i+1}}},\quad {\text{and}}\quad d_{i}=6f[x_{i-1},x_{i},x_{i+1}]\,$ (4)

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 (2), 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}$ $\Rightarrow 2M_{0}+M_{1}={\frac {6}{h_{1}}}(f[x_{0},x_{1}]-f'_{0})=6f[x_{0},x_{0},x_{1}]$ .

(5.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

$M_{n-1}+2M_{n}={\frac {6}{h_{n}}}(f'_{n}-f[x_{n-1},x_{n}])=6f[x_{n-1},x_{n},x_{n+1}]$ .

(5.2)

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 (3), (5.1) and (5.2) together, so the system of equations that we need to solve is

$\left[{\begin{array}{ccccccc}2&\lambda _{0}&&&&&\\\mu _{1}&2&\lambda _{1}&&&&\\&\ddots &\ddots &\ddots &&&\\&&\ddots &\ddots &\ddots &&\\&&&\ddots &\ddots &\ddots &\\&&&&\mu _{n-1}&2&\lambda _{n-1}\\&&&&&\mu _{n}&2\end{array}}\right]\left[{\begin{array}{c}M_{0}\\M_{1}\\\vdots \\\vdots \\\vdots \\M_{n-1}\\M_{n}\end{array}}\right]=\left[{\begin{array}{c}d_{0}\\d_{1}\\\vdots \\\vdots \\\vdots \\d_{n-1}\\d_{n}\end{array}}\right].$ (6)

II. For type II boundary condition, we are given

$M_{0}=f''_{0}$ and $M_{n}=f''_{n}$ (7)

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 (6).

## 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$ .

## 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$ . Since this is the type II boundary condition, we use $M_{0}=$ and $M_{3}=$ . Also, we have $\lambda _{0}=$ , $\mu _{3}=$ , $d_{0}=$ and $d_{3}=$ . Same as the above example, we have $\lambda _{1}=\lambda _{2}=\mu _{1}=\mu _{2}=$ and $d_{1}=$ and $d_{2}=$ .

Therefore, we can construct the system of equations:

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