Trisha Shetty (Editor)

Spline interpolation

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

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.

Contents

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.

The approach to mathematically model the shape of such elastic rulers fixed by n + 1 knots { ( x i , y i ) : i = 0 , 1 , , n } is to interpolate between all the pairs of knots ( x i 1 , y i 1 ) and ( x i , y i ) with polynomials y = q i ( x ) , i = 1 , 2 , , n .

The curvature of a curve y = f ( x ) is given by:

κ = 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 ) q i ( x i ) = q i + 1 ( x i ) 1 i 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

A third order polynomial q ( x ) for which

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

where

As

q = d q d x = d q d t d t d x = d q d t 1 x 2 x 1

one gets that:

Setting x = x1 and x = x2 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

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

where i = 1, 2, ..., n and t = x x i 1 x i x i 1 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

for i = 1, ..., n where

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

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 one gets that for "Natural Splines" one in addition to the n − 1 linear equations (15) should have that

q 1 ( x 0 ) = 2 3 ( y 1 y 0 ) ( k 1 + 2 k 0 ) ( x 1 x 0 ) ( x 1 x 0 ) 2 = 0 , q n ( x n ) = 2 3 ( y n y n 1 ) ( 2 k n + k n 1 ) ( x n x n 1 ) ( x n x n 1 ) 2 = 0 ,

i.e. that

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", 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 ) 1 Δ x 1 2 k 0 + ( 1 Δ x 1 2 1 Δ x 2 2 ) k 1 1 Δ x 2 2 k 2 = 2 ( Δ y 1 Δ x 1 3 Δ y 2 Δ x 2 3 ) q n 1 ( x n 1 ) = q n ( x n 1 ) 1 Δ x n 1 2 k n 2 + ( 1 Δ x n 1 2 1 Δ x n 2 ) k n 1 1 Δ x n 2 k n = 2 ( Δ y n 1 Δ x n 1 3 Δ y n Δ x n 3 )

where Δ x i = x i x i 1 , Δ y i = y i y i 1 .

Example

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

[ a 11 a 12 0 a 21 a 22 a 23 0 a 32 a 33 ] [ k 0 k 1 k 2 ] = [ b 1 b 2 b 3 ]

with

a 11 = 2 x 1 x 0 a 12 = 1 x 1 x 0 a 21 = 1 x 1 x 0 a 22 = 2   ( 1 x 1 x 0 + 1 x 2 x 1 ) a 23 = 1 x 2 x 1 a 32 = 1 x 2 x 1 a 33 = 2 x 2 x 1 b 1 = 3   y 1 y 0 ( x 1 x 0 ) 2 b 2 = 3   ( y 1 y 0 ( x 1 x 0 ) 2 + y 2 y 1 ( x 2 x 1 ) 2 ) b 3 = 3   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 (10) and (11) 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 (9) is displayed

Computer code

TinySpline: Open source C-library for splines which implements cubic spline interpolation

References

Spline interpolation Wikipedia