Natural Cubic Spline Function Interpolation Examples 1

Natural Cubic Spline Function Interpolation Examples 1

Recall from the Natural Cubic Spline Function Interpolation page that we can construct a natural cubic spline of the $n$ distinct points $(x_1, y_1)$, $(x_2, y_2)$, …, $(x_n, y_n)$ where $x_1 < x_2 < ... < x_n$ by defining a piecewise smooth function of cubic polynomials on the entire interval $[x_1, x_n]$. On each subinterval $[x_{j-1}, x_j]$ for $j = 2, 3, ..., n$ we define one of the cubic pieces by:

(1)
\begin{align} \quad s(x) = \frac{(x_j - x)^3 M_{j-1} + (x - x_{j-1})^3 M_j}{6(x_j - x_{j-1})} + \frac{(x_j - x)y_{j-1} + (x - x_{j-1})y_j}{x_j - x_{j-1}} - \frac{1}{6} (x_j - x_{j-1})\left ( (x_j - x)M_{j-1} + (x - x_{j-1})M_j \right ) \end{align}

The values of the $M$'s in the above equation are obtained by solving the following system of equations:

(2)
\begin{align} \quad \left\{\begin{matrix} M_1 = M_n = 0\\ \frac{1}{6}(x_j - x_{j-1})M_{j-1} + \frac{1}{3}(x_{j+1}-x_{j-1})M_j + \frac{1}{6}(x_{j+1} -x_j)M_{j+1} = \frac{y_{j+1}-y_j}{x_{j+1}-x_j} - \frac{y_j - y_{j-1}}{x_j - x_{j-1}} & j = 2, 3, ..., n-1 \end{matrix}\right. \end{align}

We will now look at an example of constructing a natural cubic spline function.

Example 1

Find the natural cubic spline that interpolates the the points $(1, 1)$, $\left ( 2, \frac{1}{2} \right )$, $\left ( 3, \frac{1}{3} \right )$, and $\left (4 , \frac{1}{4} \right )$.

We note that we have $n = 4$ distinct points. We must first solve for the $M$'s, that is, solve the following system of equations:

(3)
\begin{align} \quad \left\{\begin{matrix} M_1 = M_4 = 0 \\ \frac{1}{6}(x_2 - x_{1})M_{1} + \frac{1}{3}(x_{3}-x_{1})M_2 + \frac{1}{6}(x_{3} -x_2)M_{3} = \frac{y_{3}-y_2}{x_{3}-x_2} - \frac{y_2 - y_{1}}{x_2- x_{1}} \\ \frac{1}{6}(x_3 - x_{2})M_{2} + \frac{1}{3}(x_{4}-x_{2})M_3 + \frac{1}{6}(x_{4} -x_3)M_{4} = \frac{y_{4}-y_3}{x_{4}-x_3} - \frac{y_3 - y_{2}}{x_3 - x_{2}} \end{matrix}\right. \end{align}

This is equivalent to solving the system for $M_2$ and $M_3$:

(4)
\begin{align} \quad \left\{\begin{matrix} \frac{1}{3}(x_{3}-x_{1})M_2 + \frac{1}{6}(x_{3} -x_2)M_{3} = \frac{y_{3}-y_2}{x_{3}-x_2} - \frac{y_2 - y_{1}}{x_2- x_{1}} \\ \frac{1}{6}(x_3 - x_{2})M_{2} + \frac{1}{3}(x_{4}-x_{2})M_3 = \frac{y_{4}-y_3}{x_{4}-x_3} - \frac{y_3 - y_{2}}{x_3 - x_{2}} \end{matrix}\right. \end{align}

Substituting the values for $x$ and $y$ and we get that:

(5)
\begin{align} \quad \left\{\begin{matrix} \frac{1}{3}(3-1)M_2 + \frac{1}{6}(3 -2)M_{3} = \frac{\frac{1}{3}-\frac{1}{2}}{3-2} - \frac{\frac{1}{2} - 1}{2- 1} \\ \frac{1}{6}(3 - 2)M_{2} + \frac{1}{3}(4-2)M_3 = \frac{\frac{1}{4}-\frac{1}{3}}{4-3} - \frac{\frac{1}{3} - \frac{1}{2}}{3 - 2} \end{matrix}\right. \end{align}

When we simplify this system, we get:

(6)
\begin{align} \quad \left\{\begin{matrix} \frac{2}{3}M_2 + \frac{1}{6}M_{3} = \frac{1}{3} \\ \frac{1}{6}M_{2} + \frac{2}{3}M_3 = \frac{1}{12} \end{matrix}\right. \end{align}

We now isolate $M_2$ in the second equation:

(7)
\begin{align} \quad \frac{1}{6}M_{2} + \frac{2}{3}M_3 = \frac{1}{12} \\ \quad \frac{1}{6}M_2 = \frac{1}{12} - \frac{2}{3} M_3 \\ \quad M_2 = \frac{1}{2} - 4 M_3 \end{align}

We now substitution this into the first equation and solve for $M_3$:

(8)
\begin{align} \quad \frac{2}{3}M_2 + \frac{1}{6}M_{3} = \frac{1}{3} \\ \quad \frac{2}{3}\left ( \frac{1}{2} - 4 M_3 \right ) + \frac{1}{6}M_{3} = \frac{1}{3} \\ \quad \frac{1}{3} - \frac{8}{3} M_3 + \frac{1}{6}M_3 = \frac{1}{3} \\ \quad 2 - 16 M_3 + M_3 = 2 \\ \quad -15M_3 = 0 \\ \quad M_3 = 0 \end{align}

Plugging $M_3 = 0$ into either of the two equations above gives us that $M_2 = \frac{1}{2}$.

We are now ready to construct our cubic spline.

On the interval $[1, 2]$ we have that:

(9)
\begin{align} \quad \quad s(x) = \frac{(x_2 - x)^3 M_{1} + (x - x_{1})^3 M_2}{6(x_2 - x_{1})} + \frac{(x_2 - x)y_{1} + (x - x_{1})y_2}{x_2 - x_{1}} - \frac{1}{6} (x_2 - x_{1})\left ( (x_2 - x)M_{1} + (x - x_{1})M_2 \right ) \\ \quad \quad s(x) = \frac{(x - 1)^3 \frac{1}{2}}{6(2 - 1)} + \frac{(2 - x) + (x - 1)\frac{1}{2}}{2 - 1} - \frac{1}{6} (2 - 1)\left ( (x - 1) \frac{1}{2} \right ) \end{align}

On the interval $[2, 3]$ we have that:

(10)
\begin{align} \quad \quad s(x) = \frac{(x_3 - x)^3 M_{2} + (x - x_{2})^3 M_3}{6(x_3 - x_{2})} + \frac{(x_3 - x)y_{2} + (x - x_{2})y_3}{x_3 - x_{2}} - \frac{1}{6} (x_3 - x_{2})\left ( (x_3 - x)M_{2} + (x - x_{2})M_3 \right ) \\ \quad \quad s(x) = \frac{(3 - x)^3 \frac{1}{2}}{6(3 - 2)} + \frac{(3 - x)\frac{1}{2} + (x - 2)\frac{1}{3}}{3 - 2} - \frac{1}{6} (3 - 2)\left ( (3 - x)\frac{1}{2} \right ) \\ \end{align}

On the interval $[3, 4]$ we have that:

(11)
\begin{align} \quad \quad s(x) = \frac{(x_4 - x)^3 M_{3} + (x - x_{3})^3 M_4}{6(x_4 - x_{3})} + \frac{(x_4 - x)y_{3} + (x - x_{3})y_4}{x_4 - x_{3}} - \frac{1}{6} (x_4 - x_{3})\left ( (x_4 - x)M_{3} + (x - x_{3})M_4 \right ) \\ \quad \quad s(x) = \frac{(4 - x)\frac{1}{3} + (x - 3)\frac{1}{4}}{4 - 3} \\ \end{align}

Each piece of our cubic spline can be greatly simplified, but we will omit it here.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License