Cubic Spline Interpolation

 
对于 n + 1 给定点的数据集 { x i } , i = 0 , 1 , , n ,我们可以用 n 段三次多项式在数据点之间构建一个三次样条.如果 S ( x ) = { S 0 ( x ) ,   x [ x 0 , x 1 ] S 1 ( x ) ,   x [ x 1 , x 2 ] S n 1 ( x ) ,   x [ x n 1 , x n ] 表示对函数 f 进行插值的样条函数,那么需要:
  • 插值特性, S ( x i ) = f ( x i ) , i = 0 , 1 , , n
  • 样条相连, S i 1 ( x i ) = S i ( x i ) , i = 1 , , n 1
  • 二阶可导, S i 1 ( x i ) = S i ( x i ) 以及 S i 1 ( x i ) = S i ( x i ) , i = 1 , , n 1 .
由于每个三次多项式需要四个条件才能确定曲线形状,所以对于组成 S n 个三次多项式来说,这就意味着需要 4 n 个条件才能确定这些多项式.但是,插值特性只给出了 n + 1 个条件,内部数据点给出 n + 1 2 = n 1 个条件,总计是 4 n 2 个条件.我们还需要另外两个条件,根据不同的因素我们可以使用不同的条件.
其中一项选择条件可以得到给定 u v 的钳位三次样条(clamped cubic spline), S ( x 0 ) = u S ( x k ) = v 另外,我们可以设 S ( x 0 ) = S ( x n ) = 0 这样就得到自然三次样条.自然三次样条几乎等同于样条设备生成的曲线.
在这些所有的二次连续可导函数中,钳位与自然三次样条可以得到相对于待插值函数 f 的最小震荡.
如果选择另外一些条件, S ( x 0 ) = S ( x n ) S ( x 0 ) = S ( x n ) S ( x 0 ) = S ( x n ) 可以得到周期性的三次样条.
如果选择, S ( x 0 ) = S ( x n ) S ( x 0 ) = S ( x n ) S ( x 0 ) = f ( x 0 ) , S ( x n ) = f ( x n ) 可以得到complete三次样条.

Algorithm to find the interpolating cubic spline
We wish to find each polynomial q i ( x ) given the points ( x 0 , y 0 ) through ( x n , y n ) . To do this, we will consider just a single piece of the curve, q ( x ) , which will interpolate from ( x 1 , y 1 ) to ( x 2 , y 2 ) . This piece will have slopes k 1 and k 2 at its endpoints. Or, more precisely,

q ( x 1 ) = y 1 , q ( x 2 ) = y 2 , q ( x 1 ) = k 1 , q ( x 2 ) = k 2 . The full equation q ( x ) can be written in the symmetrical form (1) q ( x ) = ( 1 t ( x ) ) y 1 + t ( x ) y 2 + t ( x ) ( 1 t ( x ) ) ( ( 1 t ( x ) ) a + t ( x ) b ) , (1) where (2) t ( x ) = x x 1 x 2 x 1 , (2) (3) a = k 1 ( x 2 x 1 ) ( y 2 y 1 ) , (3) (4) b = k 2 ( x 2 x 1 ) + ( y 2 y 1 ) . (4)

But what are k 1 and k 2 ? To derive these critical values, we must consider that q = d q d x = d q d t d t d x = d q d t 1 x 2 x 1 . It then follows that (5) q = y 2 y 1 x 2 x 1 + ( 1 2 t ) a ( 1 t ) + b t x 2 x 1 + t ( 1 t ) b a x 2 x 1 , (5) (6) q = 2 b 2 a + ( a b ) 3 t ( x 2 x 1 ) 2 . (6) Setting t = 0 and t = 1 respectively in equations (5) and (6) , one gets from (2) that indeed first derivatives q ( x 1 ) = k 1 and q ( x 2 ) = k 2 , and also second derivatives (7) q ( x 1 ) = 2 b 2 a ( x 2 x 1 ) 2 , (7) (8) q ( x 2 ) = 2 a 2 b ( x 2 x 1 ) 2 . (8) If now ( x i , y i ) , i = 0 , 1 , . . . , n are n + 1 points, and (9) q i = ( 1 t ) y i 1 + t y i + t ( 1 t ) ( ( 1 t ) a i + t b i ) , (9) 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 x i 1 x x i for i = 1 , . . . , n such that q i ( x i ) = q i + 1 ( x i ) for i = 1 , . . . , n 1 , then the n polynomials together define a differentiable function in the interval x 0 x x n , and (10) a i = k i 1 ( x i x i 1 ) ( y i y i 1 ) , (10) (11) b i = k i ( x i x i 1 ) + ( y i y i 1 ) (11) for i = 1 , . . . , n , where (12) k 0 = q 1 ( x 0 ) , (12) (13) k i = q i ( x i ) = q i + 1 ( x i ) , i = 1 , , n 1 , (13) (14) k n = q n ( x n ) . (14) If the sequence k 0 , k 1 , , k n is such that, in addition, q i ( x i ) = q i + 1 ( x i ) holds for i = 1 , 2 , , 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 (15) k i 1 x i x i 1 + ( 1 x i x i 1 + 1 x i + 1 x i ) 2 k i + k i + 1 x i + 1 x i = 3 ( y i y i 1 ( x i x i 1 ) 2 + y i + 1 y i ( x i + 1 x i ) 2 ) (15) for i = 1 , 2 , , n 1 . The relations (15) are n 1 linear equations for the n + 1 values k 0 , k 1 , . . . , k n .
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 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
(16) 2 x 1 x 0 k 0 + 1 x 1 x 0 k 1 = 3 y 1 y 0 ( x 1 x 0 ) 2 , (16) (17) 1 x n x n 1 k n 1 + 2 x n x n 1 k n = 3 y n y n 1 ( x n x n 1 ) 2 . (17) Eventually, (15) together with (16) and (17) constitute n + 1 linear equations that uniquely define the n + 1 parameters k 0 , k 1 , , k n .
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 x 1 and x N 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 .
The spline turns into a straight line here (y''=0) The spline turns into a straight line here (y''=0)
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 the figure, the spline function consisting of the two cubic polynomials q 1 ( x ) and q 2 ( x ) given by (9) is displayed.