Course Webpage

Exercise

For the nodes $x_{0}=-1,$ $x_{1}=1,$ $x_{2}=3$ and $x_{3}=5,$ and the function $$f\left(x\right)=\sin\left(\dfrac{\pi}{6}x\right)$$ if the cubic natural spline that interpolates these nodes is $$s(x)=\begin{cases} s_{1}(x)=a\,(x+1)^{3}+b\,(x+1)^{2}+c\,(x+1)+d & \mathrm{if\;}x\in\left[-1,1\right]\\ s_{2}(x)=e\,(x-1)^{3}+f\,(x-1)^{2}+g\,(x-1)+h & \mathrm{if\;}x\in\left[1,3\right]\\ s_{3}(x)=i\,(x-3)^{3}+j\,(x-3)^{2}+k\,(x-3)+l & \mathrm{if\;}x\in\left[3,5\right] \end{cases}$$

  • Compute the coefficients of these three degree polynomials.
  • Approximate the value of $f$ in $x=2$ using the spline

Compute the coefficients of these three degree polynomials

We need the values of the function at the nodes to calculate the spline

$$f\left(x\right)=\sin\left(\dfrac{\pi}{6}x\right)\\[0.7cm] y_0=f\left(-1\right)=\sin\left(-\dfrac{\pi}{6}\right)=-\frac{1}{2}\quad y_1=f\left(1\right)=\sin\left(\dfrac{\pi}{6}\right)=\frac{1}{2}\\[0.7cm] y_2=f\left(3\right)=\sin\left(3\dfrac{\pi}{6}\right)=\sin\left(\dfrac{\pi}{2}\right)=1\quad y_3=f\left(5\right)=\sin\left(\dfrac{5\pi}{6}\right)=\frac{1}{2}$$
\begin{array}{|l|cccc|} \hline k & 0 & 1 & 2 & 3\\ \hline x & -1 & 1 & 3 & 5\\ \hline y &-\dfrac{1}{2} & \dfrac{1}{2} & 1 & \dfrac{1}{2}\\ \hline \end{array}

The following conditions must be met:

  • The curve must go through all the points $$ s_{1}(-1)=-\dfrac{1}{2},\;s_{1}(1)=\dfrac{1}{2},\;s_{2}(1)=\dfrac{1}{2},\;s_{2}(3)=1,\;s_{3}(3)=1,\;s_{3}(5)=1/2. $$

  • The first and second derivatives must be the same for the intermediate points $$ s'_{1}(1)=s'_{2}(1),\;s'_{2}(3)=s'_{3}(3),\; s''_{1}(1)=s''_{2}(1),\;s''_{2}(3)=s''_{3}(3) $$

  • Since there are 12 unknowns and we only have 10 conditions (equations), we impose two more on the extremes. As the spline is natural the additional conditions are:$$ s''_{1}(-1)=0,\;s''_{3}(5)=0. $$

We have

$$s(x)=\begin{cases} s_{1}(x)=a\,(x+1)^{3}+b\,(x+1)^{2}+c\,(x+1)+d & \mathrm{if\;}x\in\left[-1,1\right]\\ s_{2}(x)=e\,(x-1)^{3}+f\,(x-1)^{2}+g\,(x-1)+h & \mathrm{if\;}x\in\left[1,3\right]\\ s_{3}(x)=i\,(x-3)^{3}+j\,(x-3)^{2}+k\,(x-3)+l & \mathrm{if\;}x\in\left[3,5\right] \end{cases}$$

Let's calculate the first derivatives

$$ s'(x)=\begin{cases} s'_{1}(x)=3a\;(x+1)^{2}+2b\;(x+1)+c & \mathrm{if\;}x\in\left[-1,1\right]\\ s'_{2}(x)=3e\left(x-1\right)^{2}+2f\left(x-1\right)+g & \mathrm{if\;}x\in\left[1,3\right]\\ s'_{3}(x)=3i\left(x-3\right)^{2}+2j\left(x-3\right)+k & \mathrm{if\;}x\in\left[3,5\right] \end{cases} $$

and the second ones

$$ s''(x)=\begin{cases} s''_{1}(x)=6a\;(x+1)+2b & \mathrm{if\;}x\in\left[-1,1\right]\\ s''_{2}(x)=6e\left(x-1\right)+2f & \mathrm{if\;}x\in\left[1,3\right]\\ s''_{3}(x)=6i\left(x-3\right)+2j & \mathrm{if\;}x\in\left[3,5\right] \end{cases} $$

and the equations are:

$$\begin{array}{|l|c|c|} \hline 1 & s_{1}(-1)=-\dfrac{1}{2} & d=-\dfrac{1}{2}\\ 2 & s_{1}(1)=\dfrac{1}{2} & 8a + 4b + 2c + d=\dfrac{1}{2}\\ 3 & s_{2}(1)=\dfrac{1}{2} & h=\dfrac{1}{2}\\ 4 & s_{2}(3)=1 & 8e+4f+2g+h=1\\ 5 & s_{3}(3)=1 & l=1\\ 6 & s_{3}(5)=\dfrac{1}{2}& 8i + 4j + 2k + l=\dfrac{1}{2}\\ \hline 7 & s'_{1}(1)=s'_{2}(1)& 12 a + 4 b + c = g\\ 8 & s'_{2}(3)=s'_{3}(3) & 12 e + 4 f + g = k\\ \hline 9 & s''_{1}(1)=s''_{2}(1)& 12 a + 2 b = 2 f\\ 10 & s''_{2}(3)=s''_{3}(3)& 12 e + 2 f = 2 j\\ \hline 11 & s''_{1}(-1)=0& 2b=0\\ 12 & s''_{3}(5)=0& 12 i + 2 j = 0\\ \hline \end{array}$$

And we have a linear system of 12 equations to calculate 12 unknowns, which expressed in matrix form is

$$ \left(\begin{array}{rcrccrrccrrc} 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 8 & 4 & 2 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 8 & 4 & 2 & 1 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 8 & 4 & 2 & 1\\ 12 & 4 & 1 & 0 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 12 & 4 & 1 & 0 & 0 & 0 & -1 & 0\\ 12 & 2 & 0 & 0 & 0 & -2 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 12 & 2 & 0 & 0 & 0 & -2 & 0 & 0\\ 0 & 2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 12 & 2 & 0 & 0 \end{array}\right)\left(\begin{array}{c} a\\ b\\ c\\ d\\ e\\ f\\ g\\ h\\ i\\ j\\ k\\ l \end{array}\right)=\left(\begin{array}{c} -1/2\\ 1/2\\ 1/2\\ 1\\ 1\\ 1/2\\ 0\\ 0\\ 0\\ 0\\ 0\\ 0 \end{array}\right) $$

The solution of this system is $a=-1/120$, $b=0$, $c=8/15$, $d=-1/2$, $e=-1/48$, $f=-1/20$, $g=13/30$, $h=1/2$, $i=7/240$, $j=-7/40$, $k=-1/60$ and $l=1$. Therefore the cubic spline is

$$s(x)=\begin{cases} s_{1}(x)=-\dfrac{1}{120}\,(x+1)^{3}+\dfrac{8}{15}\,(x+1)-\dfrac{1}{2} & \mathrm{if\;}x\in\left[-1,1\right]\\ s_{2}(x)=-\dfrac{1}{48}\,(x-1)^{3}-\dfrac{1}{20}\,(x-1)^{2}+\dfrac{13}{30}\,(x-1)+\dfrac{1}{2} & \mathrm{if\;}x\in\left[1,3\right]\\ s_{3}(x)=\dfrac{7}{240}\,(x-3)^{3}-\dfrac{7}{40}\,(x-3)^{2}-\dfrac{1}{60}\,(x-3)+1 & \mathrm{if\;}x\in\left[3,5\right] \end{cases}$$

This is an example of what a cubic spline is, it is not an example of an efficient spline construction algorithm. In the usual algorithm, the shape of the polynomials of degree 3 in each subinterval is different from the one here and it seeks that the linear system is tridiagonal, that is, the matrix of coefficients only has elements in three diagonals: the main one, the one above the main diagonal and the one that is below. Tridiagonal systems have specific storage and resolution algorithms that are simpler than the ones for full coefficient matrices.

Regardless of the number of nodes, when constructing a cubic spline, two equations are always missing. Two additional equations are needed that are usually applied to the nodes of the edges of the interval and for this reason they are boundary conditions. Depending on them the spline is called

  • Natural: $s_1''(x_0)=0$ y $s_n''(x_n)=0$
  • Clamped: $s_1'(x_0)=y'_0$ y $s_n'(x_n)=y'_n$
  • Periodic: We must have $s_1(x_0)=s_n(x_n)$ and then we impose $s_1'(x_0)=s_n'(x_n)$ y $s_1''(x_0)=s_n''(x_n)$

Approximate the value of $f$ in $x=2$ using the spline

Since the point $2$ is in the interval $[1,3]$ we will use the polynomial

$$s_{2}(x)=-\dfrac{1}{48}\,(x-1)^{3}-\dfrac{1}{20}\,(x-1)^{2}+\dfrac{13}{30}\,(x-1)+\dfrac{1}{2} $$
$$s(2)=s_{2}(2)=-\dfrac{1}{48}\,(2-1)^{3}-\dfrac{1}{20}\,(2-1)^{2}+\dfrac{13}{30}\,(2-1)+\dfrac{1}{2}=-\dfrac{1}{48}-\dfrac{1}{20}+\dfrac{13}{30}+\dfrac{1}{2}=0.8625$$

As $f(2)=\sin\left(\dfrac{\pi}{6}(2)\right)=\sin\left(\dfrac{\pi}{3}\right)=0.86603$

$$\mathrm{Error} = |f(2)-s(2)|=|0.86603-0.86250|=0.00353$$

And, with the same number of nodes, in this case, the error is less than for the Lagrange interpolation polynomial with 4 nodes.

Let's also compare the result for a function where the Lagrange interpolation fails because the degree of the polynomial is very high and the interpolation polynomial oscillates.

We can see that the cubic spline gives here a very good approximation