Spline interpolation

From HandWiki
Short description: Mathematical method

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.[1] Spline interpolation also avoids the problem of Runge's phenomenon, in which oscillation can occur between points when interpolating using high-degree polynomials.

Introduction

Interpolation with cubic splines between eight points. Hand-drawn technical drawings for shipbuilding are a historical example of spline interpolation; drawings were constructed using flexible rulers that were bent to follow pre-defined points.

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]\displaystyle{ n + 1 }[/math] knots, [math]\displaystyle{ (x_0, y_0) }[/math] through [math]\displaystyle{ (x_n, y_n) }[/math]. There will be a cubic polynomial [math]\displaystyle{ q_i(x)=y }[/math] between each successive pair of knots [math]\displaystyle{ (x_{i-1}, y_{i-1}) }[/math] and [math]\displaystyle{ (x_i, y_i) }[/math] connecting to both of them, where [math]\displaystyle{ i = 1, 2, \dots, n }[/math]. So there will be [math]\displaystyle{ n }[/math] polynomials, with the first polynomial starting at [math]\displaystyle{ (x_0, y_0) }[/math], and the last polynomial ending at [math]\displaystyle{ (x_n, y_n) }[/math].

The curvature of any curve [math]\displaystyle{ y = y(x) }[/math] is defined as

[math]\displaystyle{ \kappa = \frac{y''}{(1 + y'^2)^{3/2}}, }[/math]

where [math]\displaystyle{ y' }[/math] and [math]\displaystyle{ y'' }[/math] are the first and second derivatives of [math]\displaystyle{ y(x) }[/math] with respect to [math]\displaystyle{ 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]\displaystyle{ y' }[/math] and [math]\displaystyle{ 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]\displaystyle{ \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]\displaystyle{ 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]\displaystyle{ q'_1(x_0) = f'(x_0) }[/math] and [math]\displaystyle{ q'_n(x_n) = f'(x_n) }[/math] where [math]\displaystyle{ 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]\displaystyle{ q'''_1(x_1) = q'''_2(x_1) }[/math] and [math]\displaystyle{ q'''_{n-1}(x_{n-1}) = q'''_{n}(x_{n-1}) }[/math].[2]

Algorithm to find the interpolating cubic spline

We wish to find each polynomial [math]\displaystyle{ q_i(x) }[/math] given the points [math]\displaystyle{ (x_0, y_0) }[/math] through [math]\displaystyle{ (x_n, y_n) }[/math]. To do this, we will consider just a single piece of the curve, [math]\displaystyle{ q(x) }[/math], which will interpolate from [math]\displaystyle{ (x_1, y_1) }[/math] to [math]\displaystyle{ (x_2, y_2) }[/math]. This piece will have slopes [math]\displaystyle{ k_1 }[/math] and [math]\displaystyle{ k_2 }[/math] at its endpoints. Or, more precisely,

[math]\displaystyle{ q(x_1) = y_1, }[/math]
[math]\displaystyle{ q(x_2) = y_2, }[/math]
[math]\displaystyle{ q'(x_1) = k_1, }[/math]
[math]\displaystyle{ q'(x_2) = k_2. }[/math]

The full equation [math]\displaystyle{ q(x) }[/math] can be written in the symmetrical form

[math]\displaystyle{ q(x) = \big(1 - t(x)\big)\,y_1 + t(x)\,y_2 + t(x)\big(1 - t(x)\big)\Big(\big(1 - t(x)\big)\,a + t(x)\,b\Big), }[/math]

 

 

 

 

(1)

where

[math]\displaystyle{ t(x) = \frac{x - x_1}{x_2 - x_1}, }[/math]

 

 

 

 

(2)

[math]\displaystyle{ a = k_1 (x_2 - x_1) - (y_2 - y_1), }[/math]

 

 

 

 

(3)

[math]\displaystyle{ b = -k_2 (x_2 - x_1) + (y_2 - y_1). }[/math]

 

 

 

 

(4)

But what are [math]\displaystyle{ k_1 }[/math] and [math]\displaystyle{ k_2 }[/math]? To derive these critical values, we must consider that

[math]\displaystyle{ q' = \frac{dq}{dx} = \frac{dq}{dt} \frac{dt}{dx} = \frac{dq}{dt} \frac{1}{x_2 - x_1}. }[/math]

It then follows that

[math]\displaystyle{ q' = \frac{y_2 - y_1}{x_2 - x_1} + (1 - 2t) \frac {a(1 - t) + bt}{x_2 - x_1} + t(1 - t) \frac{b - a}{x_2 - x_1}, }[/math]

 

 

 

 

(5)

[math]\displaystyle{ q'' = 2 \frac{b - 2a + (a - b)3t}{{(x_2 - x_1)}^2}. }[/math]

 

 

 

 

(6)

Setting t = 0 and t = 1 respectively in equations (5) and (6), one gets from (2) that indeed first derivatives q′(x1) = k1 and q′(x2) = k2, and also second derivatives

[math]\displaystyle{ q''(x_1) = 2 \frac{b - 2a}{{(x_2 - x_1)}^2}, }[/math]

 

 

 

 

(7)

[math]\displaystyle{ q''(x_2) = 2 \frac{a - 2b}{{(x_2 - x_1)}^2}. }[/math]

 

 

 

 

(8)

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

[math]\displaystyle{ q_i = (1 - t)\,y_{i-1} + t\,y_i + t(1 - t)\big((1 - t)\,a_i + t\,b_i\big), }[/math]

 

 

 

 

(9)

where i = 1, 2, ..., n, and [math]\displaystyle{ t = \tfrac{x - x_{i-1}}{x_i - x_{i-1}} }[/math] are n third-degree polynomials interpolating y in the interval xi−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

[math]\displaystyle{ a_i = k_{i-1}(x_i - x_{i-1}) - (y_i - y_{i-1}), }[/math]

 

 

 

 

(10)

[math]\displaystyle{ b_i = -k_i(x_i - x_{i-1}) + (y_i - y_{i-1}) }[/math]

 

 

 

 

(11)

for i = 1, ..., n, where

[math]\displaystyle{ k_0 = q_1'(x_0), }[/math]

 

 

 

 

(12)

[math]\displaystyle{ k_i = q_i'(x_i) = q_{i+1}'(x_i), \qquad i = 1, \dots, n - 1, }[/math]

 

 

 

 

(13)

[math]\displaystyle{ k_n = q_n'(x_n). }[/math]

 

 

 

 

(14)

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

From (7), (8), (10) and (11) follows that this is the case if and only if

[math]\displaystyle{ \frac{k_{i-1}}{x_i - x_{i-1}} + \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]

 

 

 

 

(15)

for i = 1, ..., n − 1. The relations (15) 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, "natural splines" in addition to the n − 1 linear equations (15) should have

[math]\displaystyle{ 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]\displaystyle{ 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

[math]\displaystyle{ \frac{2}{x_1 - x_0} k_0 + \frac{1}{x_1 - x_0} k_1 = 3 \frac{y_1 - y_0}{(x_1 - x_0)^2}, }[/math]

 

 

 

 

(16)

[math]\displaystyle{ \frac{1}{x_n - x_{n-1}}k_{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]

 

 

 

 

(17)

Eventually, (15) together with (16) and (17) constitute n + 1 linear equations that uniquely define the n + 1 parameters k0, k1, ..., kn.

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 x1 and xn−1 points. For the "not-a-knot" spline, the additional equations will read:

[math]\displaystyle{ 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]\displaystyle{ 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]\displaystyle{ \Delta x_i = x_i - x_{i-1},\ \Delta y_i = y_i - y_{i-1} }[/math].

Example

Interpolation with cubic "natural" splines between three points

In case of three points the values for [math]\displaystyle{ k_0, k_1, k_2 }[/math] are found by solving the tridiagonal linear equation system

[math]\displaystyle{ \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]\displaystyle{ a_{11} = \frac{2}{x_1 - x_0}, }[/math]
[math]\displaystyle{ a_{12} = \frac{1}{x_1 - x_0}, }[/math]
[math]\displaystyle{ a_{21} = \frac{1}{x_1 - x_0}, }[/math]
[math]\displaystyle{ a_{22} = 2 \left(\frac{1}{x_1 - x_0} + \frac{1}{{x_2 - x_1}}\right), }[/math]
[math]\displaystyle{ a_{23} = \frac{1}{{x_2 - x_1}}, }[/math]
[math]\displaystyle{ a_{32} = \frac{1}{x_2 - x_1}, }[/math]
[math]\displaystyle{ a_{33} = \frac{2}{x_2 - x_1}, }[/math]
[math]\displaystyle{ b_1 = 3 \frac{y_1 - y_0}{(x_1 - x_0)^2}, }[/math]
[math]\displaystyle{ 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]\displaystyle{ b_3 = 3 \frac{y_2 - y_1}{(x_2 - x_1)^2}. }[/math]

For the three points

[math]\displaystyle{ (-1,0.5),\ (0,0),\ (3,3), }[/math]

one gets that

[math]\displaystyle{ k_0 = -0.6875,\ k_1 = -0.1250,\ k_2 = 1.5625, }[/math]

and from (10) and (11) that

[math]\displaystyle{ a_1 = k_0(x_1 - x_0) - (y_1 - y_0) = -0.1875, }[/math]
[math]\displaystyle{ b_1 = -k_1(x_1 - x_0) + (y_1 - y_0) = -0.3750, }[/math]
[math]\displaystyle{ a_2 = k_1(x_2 - x_1) - (y_2 - y_1) = -3.3750, }[/math]
[math]\displaystyle{ 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]\displaystyle{ q_1(x) }[/math] and [math]\displaystyle{ q_2(x) }[/math] given by (9) is displayed.

See also

Computer code

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

References

  1. Hall, Charles A.; Meyer, Weston W. (1976). "Optimal Error Bounds for Cubic Spline Interpolation". Journal of Approximation Theory 16 (2): 105–122. doi:10.1016/0021-9045(76)90040-X. 
  2. Burden, Richard; Faires, Douglas (2015). Numerical Analysis (10th ed.). Cengage Learning. pp. 142–157. ISBN 9781305253667. 
  • Schoenberg, Isaac J. (1946). "Contributions to the Problem of Approximation of Equidistant Data by Analytic Functions: Part A.—On the Problem of Smoothing or Graduation. A First Class of Analytic Approximation Formulae". Quarterly of Applied Mathematics 4 (2): 45–99. doi:10.1090/qam/15914. 
  • Schoenberg, Isaac J. (1946). "Contributions to the Problem of Approximation of Equidistant Data by Analytic Functions: Part B.—On the Problem of Osculatory Interpolation. A Second Class of Analytic Approximation Formulae". Quarterly of Applied Mathematics 4 (2): 112–141. doi:10.1090/qam/16705. 

External links