# Spline interpolation

{{#invoke:see also|seealso}} In the mathematical field of numerical analysis, spline interpolation is a form of interpolation where the interpolant is a special type of piecewise polynomial called a spline. Spline interpolation is often preferred over polynomial interpolation because the interpolation error can be made small even when using low degree polynomials for the spline. Spline interpolation avoids the problem of Runge's phenomenon, in which oscillation can occur between points when interpolating using high degree polynomials.

## Introduction

Originally, spline was a term for elastic rulers that were bent to pass through a number of predefined points ("knots"). These were used to make technical drawings for shipbuilding and construction by hand, as illustrated by Figure 1. Figure 1: Interpolation with cubic splines between eight points. Hand-drawn technical drawings were made for shipbuilding etc. using flexible rulers that were bent to follow pre-defined points

The curvature of a curve

$y=f(x)$ is

$\kappa ={\frac {y''}{(1+y'^{2})^{3/2}}}$ As the spline will take a shape that minimizes the bending (under the constraint of passing through all knots) both $y'$ and $y''$ will be continuous everywhere and at the knots. To achieve this one must have that

$q'_{i}(x_{i})=q'_{i+1}(x_{i})$ and that

$q''_{i}(x_{i})=q''_{i+1}(x_{i})$ for all i, $1\leq i\leq n-1$ . This can only be achieved if polynomials of degree 3 or higher are used. The classical approach is to use polynomials of degree 3 — the case of cubic splines.

## Algorithm to find the interpolating cubic spline

$q(x_{1})=y_{1}$ $q(x_{2})=y_{2}$ $q'(x_{1})=k_{1}$ $q'(x_{2})=k_{2}$ can be written in the symmetrical form

1. Template:NumBlk

where Template:NumBlk and

Setting x = x1 and x = x2 respectively in equations (Template:EquationNote) and (Template:EquationNote) one gets from (Template:EquationNote) that indeed first derivatives q'(x1) = k1 and q'(x2) = k2 and also second derivatives

If now (xi, yi) where i = 0, 1, ... , n are n+1 points and

1. Template:NumBlk

where i = 1, 2, ..., n and $t={\tfrac {x-x_{i-1}}{x_{i}-x_{i-1}}}$ are n third degree polynomials interpolating y in the interval x i-1xxi for i = 1,... , n such that q'i (xi) = q' i+1 (xi) for i = 1, ... , n-1 then the n polynomials together define a differentiable function in the interval x0xxn and

for i = 1, ..., n where

If the sequence k0, k1, ..., kn is such that in addition q"i(xi) = q"i+1(xi) for i = 1, ..., n-1 the resulting function will even have a continuous second derivative.

From (Template:EquationNote), (Template:EquationNote), (Template:EquationNote) and (Template:EquationNote) follows that this is the case if and only if

1. Template:NumBlk

for i = 1, ..., n-1. The relations (Template:EquationNote) are n-1 linear equations for the n+1 values k0, k1, ..., kn.

For the elastic rulers being the model for the spline interpolation one has that to the left of the left-most "knot" and to the right of the right-most "knot" the ruler can move freely and will therefore take the form of a straight line with q" = 0 . As q" should be a continuous function of x one gets that for "Natural Splines" one in addition to the n-1 linear equations (Template:EquationNote) should have that

$q''_{1}(x_{0})\ =2\ {\frac {3(y_{1}-y_{0})-(k_{1}+2k_{0})(x_{1}-x_{0})}{{(x_{1}-x_{0})}^{2}}}=0,$ $q''_{n}(x_{n})\ =-2\ {\frac {3(y_{n}-y_{n-1})-(2k_{n}+k_{n-1})(x_{n}-x_{n-1})}{{(x_{n}-x_{n-1})}^{2}}}=0,$ i.e. that

Eventually, (Template:EquationNote) together with (Template:EquationNote) and (Template:EquationNote) constitute n+1 linear equations that uniquely define the n+1 parameters k0, k1, ..., kn.

There exists other end conditions: "Clamped spline", that specifies the slope at the ends of the spline, and the popular "not-a-knot spline", that requires that the third derivative is also continuous at the x1 and xN-1 points. For the "not-a-knot" spline, the additional equations will read:

$q'''_{1}(x_{1})=q'''_{2}(x_{1})=>{\frac {1}{\Delta x_{1}^{2}}}k_{0}+\left({\frac {1}{\Delta x_{1}^{2}}}-{\frac {1}{\Delta x_{2}^{2}}}\right)k_{1}-{\frac {1}{\Delta x_{2}^{2}}}k_{2}=2\left({\frac {\Delta y_{1}}{\Delta x_{1}^{3}}}-{\frac {\Delta y_{2}}{\Delta x_{2}^{3}}}\right)$ $q'''_{n-1}(x_{n-1})=q'''_{n}(x_{n-1})=>{\frac {1}{\Delta x_{n-1}^{2}}}k_{n-2}+\left({\frac {1}{\Delta x_{n-1}^{2}}}-{\frac {1}{\Delta x_{n}^{2}}}\right)k_{n-1}-{\frac {1}{\Delta x_{n}^{2}}}k_{n}=2\left({\frac {\Delta y_{n-1}}{\Delta x_{n-1}^{3}}}-{\frac {\Delta y_{n}}{\Delta x_{n}^{3}}}\right)$ ## Example

In case of three points the values for $k_{0},k_{1},k_{2}$ are found by solving the tridiagonal linear equation system

${\begin{bmatrix}a_{11}&a_{12}&0\\a_{21}&a_{22}&a_{23}\\0&a_{32}&a_{33}\\\end{bmatrix}}{\begin{bmatrix}k_{0}\\k_{1}\\k_{2}\\\end{bmatrix}}={\begin{bmatrix}b_{1}\\b_{2}\\b_{3}\\\end{bmatrix}}$ with

$a_{11}={\frac {2}{x_{1}-x_{0}}}$ $a_{12}={\frac {1}{x_{1}-x_{0}}}$ $a_{21}={\frac {1}{x_{1}-x_{0}}}$ $a_{22}=2\ \left({\frac {1}{x_{1}-x_{0}}}+{\frac {1}{x_{2}-x_{1}}}\right)$ $a_{23}={\frac {1}{x_{2}-x_{1}}}$ $a_{32}={\frac {1}{x_{2}-x_{1}}}$ $a_{33}={\frac {2}{x_{2}-x_{1}}}$ $b_{1}=3\ {\frac {y_{1}-y_{0}}{(x_{1}-x_{0})^{2}}}$ $b_{2}=3\ \left({\frac {y_{1}-y_{0}}{{(x_{1}-x_{0})}^{2}}}+{\frac {y_{2}-y_{1}}{{(x_{2}-x_{1})}^{2}}}\right)$ $b_{3}=3\ {\frac {y_{2}-y_{1}}{(x_{2}-x_{1})^{2}}}$ For the three points

$(-1,0.5)\ ,\ (0,0)\ ,\ (3,3)$ one gets that

$k_{0}=-0.6875\ ,\ k_{1}=-0.1250\ ,\ k_{2}=1.5625$ and from (Template:EquationNote) and (Template:EquationNote) that

$a_{1}=k_{0}(x_{1}-x_{0})-(y_{1}-y_{0})=-0.1875$ $b_{1}=-k_{1}(x_{1}-x_{0})+(y_{1}-y_{0})=-0.3750$ $a_{2}=k_{1}(x_{2}-x_{1})-(y_{2}-y_{1})=-3.3750$ $b_{2}=-k_{2}(x_{2}-x_{1})+(y_{2}-y_{1})=-1.6875$ In Figure 2 the spline function consisting of the two cubic polynomials $q_{1}(x)$ and $q_{2}(x)$ given by (Template:EquationNote) is displayed