Template:Short description Template:Broader Template:More footnotes 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. That is, instead of fitting a single, high-degree polynomial to all of the values at once, spline interpolation fits low-degree polynomials to small subsets of the values, for example, fitting nine cubic polynomials between each of the pairs of ten points, instead of fitting a single degree-nine polynomial to all of them. 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.<ref>Template:Cite journal</ref> Spline interpolation also avoids the problem of Runge's phenomenon, in which oscillation can occur between points when interpolating using high-degree polynomials.
IntroductionEdit
Originally, spline was a term for elastic rulers that were bent to pass through a number of predefined points, or knots. These were used to make technical drawings for shipbuilding and construction by hand, as illustrated in the figure.
We wish to model similar kinds of curves using a set of mathematical equations. Assume we have a sequence of <math>n + 1</math> knots, <math>(x_0, y_0)</math> through <math>(x_n, y_n)</math>. There will be a cubic polynomial <math>q_i(x)=y</math> between each successive pair of knots <math>(x_{i-1}, y_{i-1})</math> and <math>(x_i, y_i)</math> connecting to both of them, where <math>i = 1, 2, \dots, n</math>. So there will be <math>n</math> polynomials, with the first polynomial starting at <math>(x_0, y_0)</math>, and the last polynomial ending at <math>(x_n, y_n)</math>.
The curvature of any curve <math>y = y(x)</math> is defined as
- <math>\kappa = \frac{y}{(1 + y'^2)^{3/2}},</math>
where <math>y'</math> and <math>y</math> are the first and second derivatives of <math>y(x)</math> with respect to <math>x</math>. To make the spline take a shape that minimizes the bending (under the constraint of passing through all knots), we will define both <math>y'</math> and <math>y</math> to be continuous everywhere, including at the knots. Each successive polynomial must have equal values (which are equal to the y-value of the corresponding datapoint), derivatives, and second derivatives at their joining knots, which is to say that
- <math>\begin{cases}
q_i(x_i) = q_{i+1}(x_i) = y_i \\ q'_i(x_i) = q'_{i+1}(x_i) \\ q_i(x_i) = q_{i+1}(x_i)
\end{cases} \qquad 1 \le i \le n - 1.</math>
This can only be achieved if polynomials of degree 3 (cubic polynomials) or higher are used. The classical approach is to use polynomials of exactly degree 3 — cubic splines.
In addition to the three conditions above, a natural cubic spline has the condition that <math>q_1(x_0) = q_n(x_n) = 0</math>.
In addition to the three main conditions above, a clamped cubic spline has the conditions that <math>q'_1(x_0) = f'(x_0)</math> and <math>q'_n(x_n) = f'(x_n)</math> where <math>f'(x)</math> is the derivative of the interpolated function.
In addition to the three main conditions above, a not-a-knot spline has the conditions that <math>q_1(x_1) = q_2(x_1)</math> and <math>q_{n-1}(x_{n-1}) = q_{n}(x_{n-1})</math>.<ref>Template:Cite book</ref>
Algorithm to find the interpolating cubic splineEdit
We wish to find each polynomial <math>q_i(x)</math> given the points <math>(x_0, y_0)</math> through <math>(x_n, y_n)</math>. To do this, we will consider just a single piece of the curve, <math>q(x)</math>, which will interpolate from <math>(x_1, y_1)</math> to <math>(x_2, y_2)</math>. This piece will have slopes <math>k_1</math> and <math>k_2</math> at its endpoints. Or, more precisely,
- <math>q(x_1) = y_1,</math>
- <math>q(x_2) = y_2,</math>
- <math>q'(x_1) = k_1,</math>
- <math>q'(x_2) = k_2.</math>
The full equation <math>q(x)</math> can be written in the symmetrical form Template:NumBlk where Template:NumBlk Template:NumBlk Template:NumBlk
But what are <math>k_1</math> and <math>k_2</math>? To derive these critical values, we must consider that
- <math>q' = \frac{dq}{dx} = \frac{dq}{dt} \frac{dt}{dx} = \frac{dq}{dt} \frac{1}{x_2 - x_1}.</math>
It then follows that Template:NumBlk Template:NumBlk + \left(\frac{1}{x_i - x_{i-1}} + \frac{1}{{x_{i+1} - x_i}}\right) 2k_i + \frac{k_{i+1}}{{x_{i+1} - x_i}} = 3 \left(\frac{y_i - y_{i-1}}{{(x_i - x_{i-1})}^2} + \frac{y_{i+1} - y_i}{{(x_{i+1} - x_i)}^2}\right)</math>|Template:EquationRef}}
for i = 1, ..., n − 1. The relations (Template:EquationNote) are Template:Math linear equations for the Template:Math values Template:Math.
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 Template:Math. As Template:Mvar should be a continuous function of Template:Mvar, "natural splines" in addition to the Template:Math linear equations (Template:EquationNote) should have
- <math>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,</math>
- <math>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,</math>
i.e. that Template:NumBlk Template:NumBlkk_{n-1} + \frac{2}{x_n - x_{n-1}} k_n = 3 \frac{y_n - y_{n-1}}{(x_n - x_{n-1})^2}.</math>|Template:EquationRef}}
Eventually, (Template:EquationNote) together with (Template:EquationNote) and (Template:EquationNote) constitute Template:Math linear equations that uniquely define the Template:Math parameters Template:Math.
There exist other end conditions, "clamped spline", which specifies the slope at the ends of the spline, and the popular "not-a-knot spline", which requires that the third derivative is also continuous at the Template:Math and Template:Math points. For the "not-a-knot" spline, the additional equations will read:
- <math>q_1(x_1) = q_2(x_1) \Rightarrow \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),</math>
- <math>q_{n-1}(x_{n-1}) = q_n(x_{n-1}) \Rightarrow \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),</math>
where <math>\Delta x_i = x_i - x_{i-1},\ \Delta y_i = y_i - y_{i-1}</math>.
ExampleEdit
In case of three points the values for <math>k_0, k_1, k_2</math> are found by solving the tridiagonal linear equation system
- <math>
\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} </math> with
- <math>a_{11} = \frac{2}{x_1 - x_0},</math>
- <math>a_{12} = \frac{1}{x_1 - x_0},</math>
- <math>a_{21} = \frac{1}{x_1 - x_0},</math>
- <math>a_{22} = 2 \left(\frac{1}{x_1 - x_0} + \frac{1}Template:X 2 - x 1\right),</math>
- <math>a_{23} = \frac{1}Template:X 2 - x 1,</math>
- <math>a_{32} = \frac{1}{x_2 - x_1},</math>
- <math>a_{33} = \frac{2}{x_2 - x_1},</math>
- <math>b_1 = 3 \frac{y_1 - y_0}{(x_1 - x_0)^2},</math>
- <math>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),</math>
- <math>b_3 = 3 \frac{y_2 - y_1}{(x_2 - x_1)^2}.</math>
For the three points
- <math>(-1,0.5),\ (0,0),\ (3,3),</math>
one gets that
- <math>k_0 = -0.6875,\ k_1 = -0.1250,\ k_2 = 1.5625,</math>
and from (Template:EquationNote) and (Template:EquationNote) that
- <math>a_1 = k_0(x_1 - x_0) - (y_1 - y_0) = -0.1875,</math>
- <math>b_1 = -k_1(x_1 - x_0) + (y_1 - y_0) = -0.3750,</math>
- <math>a_2 = k_1(x_2 - x_1) - (y_2 - y_1) = -3.3750,</math>
- <math>b_2 = -k_2(x_2 - x_1) + (y_2 - y_1) = -1.6875.</math>
In the figure, the spline function consisting of the two cubic polynomials <math>q_1(x)</math> and <math>q_2(x)</math> given by (Template:EquationNote) is displayed.
See alsoEdit
- Akima spline
- Circular interpolation
- Cubic Hermite spline
- Centripetal Catmull–Rom spline
- Discrete spline interpolation
- Monotone cubic interpolation
- Non-uniform rational B-spline
- Multivariate interpolation
- Polynomial interpolation
- Smoothing spline
- Spline wavelet
- Thin plate spline
- Polyharmonic spline
ReferencesEdit
Further readingEdit
External linksEdit
- Cubic Spline Interpolation Online Calculation and Visualization Tool (with JavaScript source code)
- Template:Springer
- Dynamic cubic splines with JSXGraph
- Lectures on the theory and practice of spline interpolation
- Paper which explains step by step how cubic spline interpolation is done, but only for equidistant knots.
- Numerical Recipes in C, Go to Chapter 3 Section 3-3
- A note on cubic splines
- Information about spline interpolation (including code in Fortran 77)
- TinySpline:Open source C-library for splines which implements cubic spline interpolation
- SciPy Spline Interpolation:a Python package that implements interpolation
- Cubic Interpolation:Open source C#-library for cubic spline interpolation