# 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 ${\displaystyle a=x_{0}, the spline S(x) is a function satisfying:

1. ${\displaystyle S(x)\in C^{2}[a,b]}$;
2. On each subinterval ${\displaystyle [x_{i-1},x_{i}],S(x)}$ is a polynomial of degree 3, where ${\displaystyle i=1,\cdots ,n.}$
3. ${\displaystyle S(x_{i})=y_{i},}$ for all ${\displaystyle i=0,1,\cdots ,n.}$

Let us assume that

${\displaystyle 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 ${\displaystyle C_{i}=a_{i}+b_{i}x+c_{i}x^{2}+d_{i}x^{3}(d_{i}\neq 0)}$ is a cubic function, ${\displaystyle i=1,\cdots ,n}$.

## Boundary Conditions

To determine this cubic spline S(x), we need to determine ${\displaystyle a_{i},b_{i},c_{i}{\mbox{ and }}d_{i}}$ for each i by:

• ${\displaystyle C_{i}(x_{i-1})=y_{i-1}}$ and ${\displaystyle C_{i}(x_{i})=y_{i}}$, ${\displaystyle i=1,\cdots ,n.}$
• ${\displaystyle C_{i}^{'}(x_{i})=C_{i+1}^{'}(x_{i})}$, ${\displaystyle i=1,\cdots ,n-1.}$
• ${\displaystyle C_{i}^{''}(x_{i})=C_{i+1}^{''}(x_{i})}$, ${\displaystyle i=1,\cdots ,n-1.}$

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

There are three types of common boundary conditions:

I. First derivatives at the endpoints are known:

${\displaystyle C_{1}^{'}(x_{0})=f'_{0},{\mbox{ and }}C_{n}^{'}(x_{n})=f'_{n}}$.

This is called clamped boundary conditions.

II. Second derivatives at the endpoints are known:

${\displaystyle C_{1}^{''}(x_{0})=f_{0}^{''},{\mbox{ and }}C_{n}^{''}(x_{n})=f_{n}^{''}}$.

The special case ${\displaystyle C_{1}^{''}(x_{0})=C_{n}^{''}(x_{n})=0}$ is called natural or simple boundary conditions.

III. When the exact function f(x) is a periodic function with period ${\displaystyle x_{n}-x_{0}}$, S(x) is a periodic function with period ${\displaystyle x_{n}-x_{0}}$ too. Thus

${\displaystyle 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 w: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 w:Cubic Hermite spline, this gives us the spline in w:Hermite form.

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

Let ${\displaystyle h_{i}=x_{i}-x_{i-1}}$, ${\displaystyle i=1,\cdots ,n}$, ${\displaystyle S''(x_{i})=C''_{i}(x_{i})=C''_{i+1}(x_{i})=M_{i}(i=1,\cdots ,n-1)}$ and ${\displaystyle S''(x_{0})=C''_{1}(x_{0})=M_{0},{\mbox{ and }}S''(x_{n})=C''_{n}(x_{n})=M_{n}}$. Note that ${\displaystyle M_{i}}$'s are unknown (except for type II boundary condition, ${\displaystyle M_{0}{\mbox{ and }}M_{n}}$ are given).

Since each ${\displaystyle C_{i}}$ is a cubic polynomial, ${\displaystyle C''_{i}}$ is linear.

By w:Lagrange interpolation, we can interpolate each ${\displaystyle C''_{i}}$ on ${\displaystyle [x_{i-1},x_{i}]}$ since ${\displaystyle C''_{i}(x_{i-1})=M_{i-1}}$ and ${\displaystyle C''_{i}(x_{i})=M_{i}}$, the Lagrange form of this interpolating polynomial is:

${\displaystyle C''_{i}(x)=M_{i-1}{\frac {x_{i}-x}{h_{i}}}+M_{i}{\frac {x-x_{i-1}}{h_{i}}}}$ for ${\displaystyle x\in [x_{i-1},x_{i}]}$.

Integrating the above equation twice and using the condition that

${\displaystyle C_{i}(x_{i-1})=y_{i-1}}$ and ${\displaystyle C_{i}(x_{i})=y_{i}}$ to determine the constants of integration, we haveString Module Error: function rep expects a number as second parameter, received "

"
${\displaystyle 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)

String Module Error: function rep expects a number as second parameter, received "

"

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

For ${\displaystyle i=1,\cdots ,n-1,}$ when ${\displaystyle x\in [x_{i},x_{i+1}],}$, we can calculate that

${\displaystyle 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, ${\displaystyle 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 ${\displaystyle x\in [x_{i-1},x_{i}],}$, we can shift the index to obtainString Module Error: function rep expects a number as second parameter, received "

"
${\displaystyle 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)

String Module Error: function rep expects a number as second parameter, received "

"

Thus, ${\displaystyle 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 ${\displaystyle C'_{i+1}(x_{i})=C'_{i}(x_{i})}$, we can deriveString Module Error: function rep expects a number as second parameter, received "

"
${\displaystyle \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)

String Module Error: function rep expects a number as second parameter, received "

" whereString Module Error: function rep expects a number as second parameter, received "

"
${\displaystyle \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)

String Module Error: function rep expects a number as second parameter, received "

"

and ${\displaystyle 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 ${\displaystyle M_{i}}$'s.

I. For type I boundary condition, we are given ${\displaystyle C'_{1}(x_{0})=f'_{0}}$ and ${\displaystyle C'_{n}(x_{n})=f'_{n}}$. According to equation (2 ), we can obtain

${\displaystyle 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}.}$
${\displaystyle \Rightarrow f'_{0}=-M_{0}{\frac {h_{1}}{2}}+f[x_{0},x_{1}]-{\frac {M_{1}-M_{0}}{6}}h_{1}}$String Module Error: function rep expects a number as second parameter, received "
"
${\displaystyle \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)

String Module Error: function rep expects a number as second parameter, received "

"

Similarly, simplifying

${\displaystyle 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 haveString Module Error: function rep expects a number as second parameter, received "

"
${\displaystyle 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}]}$.

(5.2)

String Module Error: function rep expects a number as second parameter, received "

"

Therefore, let ${\displaystyle \lambda _{0}=\mu _{n}=1,d_{0}=6f[x_{0},x_{0},x_{1}]}$ and ${\displaystyle 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 isString Module Error: function rep expects a number as second parameter, received "

"
${\displaystyle \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)

String Module Error: function rep expects a number as second parameter, received "

" II. For type II boundary condition, we are givenString Module Error: function rep expects a number as second parameter, received "

"
${\displaystyle M_{0}=f''_{0}}$ and ${\displaystyle M_{n}=f''_{n}}$

(7)

String Module Error: function rep expects a number as second parameter, received "

"

directly, so let ${\displaystyle \lambda _{0}=\mu _{n}=0}$, ${\displaystyle d_{0}=2f''_{0}}$, and ${\displaystyle 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 ${\displaystyle S(x)}$ satisfying ${\displaystyle S'(0)=0.2}$ and ${\displaystyle S'(3)=-1}$.

## Exercise

 For points (0,0), (1,0.5), (2,2) and (3,1.5), find the interpolating cubic spline ${\displaystyle S(x)}$ satisfying ${\displaystyle S''(0)=-0.3}$ and ${\displaystyle S''(3)=3.3}$. Since this is the type II boundary condition, we use ${\displaystyle M_{0}=}$ and ${\displaystyle M_{3}=}$ . Also, we have ${\displaystyle \lambda _{0}=}$ , ${\displaystyle \mu _{3}=}$ , ${\displaystyle d_{0}=}$ and ${\displaystyle d_{3}=}$ . Same as the above example, we have ${\displaystyle \lambda _{1}=\lambda _{2}=\mu _{1}=\mu _{2}=}$ and ${\displaystyle d_{1}=}$ and ${\displaystyle d_{2}=}$ .

Therefore, we can construct the system of equations:

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

Figure 1: Interpolating Cubic Splines

## References

Polynomial and Spline Interpolation, http://www.math.ohiou.edu/courses/math3600/lecture19.pdf