Cubic Spline Conditions A natural cubic spline through the nodes (0, 0), (1, 1), (2, 0) and (3, 1), made of three cubic pieces. At the interior knots x1 and x2 the pieces share the same height, slope and curvature. y x x₀ x₁ x₂ x₃ same slope S″ = 0 S″ = 0 S₀ S₁ S₂ S₀ , S₁ and
S₂ are the three cubic pieces, one on each
interval between nodes. At each interior knot x₁ and x₂ the two adjacent
pieces meet at the same height, have the same slope (dashed line) and the
same curvature. At the ends the natural condition sets S″ = 0.
A cubic spline is a piecewise cubic function that interpolates ordered data
points and has continuous value, slope and curvature at every interior node.
Let
( x 0 , y 0 ) , … , ( x n , y n ) , x 0 < x 1 < ⋯ < x n (x_0,y_0),\ldots,(x_n,y_n),
\qquad x_0<x_1<\cdots<x_n ( x 0 , y 0 ) , … , ( x n , y n ) , x 0 < x 1 < ⋯ < x n
The interpolant is defined on [ x 0 , x n ] [x_0,x_n] [ x 0 , x n ] . Use y j = f ( x j ) y_j=f(x_j) y j = f ( x j ) when the data come
from a known function f f f .
On the interval [ x j , x j + 1 ] [x_j,x_{j+1}] [ x j , x j + 1 ] , write
At every interior node x j x_j x j , where j = 1 , … , n − 1 j=1,\ldots,n-1 j = 1 , … , n − 1 , adjacent pieces preserve
3 quantities.
Value
S j − 1 ( x j ) = S j ( x j ) S_{j-1}(x_j)=S_j(x_j) S j − 1 ( x j ) = S j ( x j ) .
Slope
S j − 1 ′ ( x j ) = S j ′ ( x j ) S_{j-1}'(x_j)=S_j'(x_j) S j − 1 ′ ( x j ) = S j ′ ( x j ) .
Curvature
S j − 1 ′ ′ ( x j ) = S j ′ ′ ( x j ) S_{j-1}''(x_j)=S_j''(x_j) S j − 1 ′′ ( x j ) = S j ′′ ( x j ) .
Each piece passes through its 2 endpoint data points.
There are 4 n 4n 4 n coefficient unknowns. The n + 1 n+1 n + 1 interpolation conditions, the
2 ( n − 1 ) 2(n-1) 2 ( n − 1 ) independent smoothness conditions and 2 boundary conditions determine
them. Value continuity is already enforced by interpolation at each node.
Boundary Conditions
Natural Spline
S ′ ′ ( x 0 ) = S ′ ′ ( x n ) = 0 S''(x_0)=S''(x_n)=0 S ′′ ( x 0 ) = S ′′ ( x n ) = 0 . Used when endpoint slopes are unavailable.
Clamped Spline
S ′ ( x 0 ) = f ′ ( x 0 ) S'(x_0)=f'(x_0) S ′ ( x 0 ) = f ′ ( x 0 ) and S ′ ( x n ) = f ′ ( x n ) S'(x_n)=f'(x_n) S ′ ( x n ) = f ′ ( x n ) . Used when endpoint slopes are known.
Construction
Set
The system can be solved in this order:
Compute h j h_j h j which are mostly constant.
Solve for a j a_j a j using the interpolation points.
Calculate δ j \delta_j δ j .
Use the boundary condition and the interior smoothness equations to solve for all c j c_j c j .
Use the slope and curvature equations to compute all b j b_j b j and d j d_j d j .
Substitute a j a_j a j , b j b_j b j , c j c_j c j and d j d_j d j into each S j ( x ) S_j(x) S j ( x ) .
Equations Required
The continuity conditions reduce to a tridiagonal system for the c j c_j c j values.
Proof Hint
Start with the value, slope and curvature conditions on [ x j , x j + 1 ] [x_j,x_{j+1}] [ x j , x j + 1 ] :
a j = y j , a j + b j h j + c j h j 2 + d j h j 3 = y j + 1 a_j=y_j,
\qquad
a_j+b_jh_j+c_jh_j^2+d_jh_j^3=y_{j+1} a j = y j , a j + b j h j + c j h j 2 + d j h j 3 = y j + 1 b j + 2 c j h j + 3 d j h j 2 = b j + 1 c j + 3 d j h j = c j + 1 \begin{aligned}
b_j+2c_jh_j+3d_jh_j^2 &= b_{j+1} \\
c_j+3d_jh_j &= c_{j+1}
\end{aligned} b j + 2 c j h j + 3 d j h j 2 c j + 3 d j h j = b j + 1 = c j + 1 Use the curvature equation to eliminate d j d_j d j . Use the value and slope equations on adjacent intervals to eliminate b j b_j b j and d j d_j d j .
For each interior node x j x_j x j , solve
h j − 1 c j − 1 + 2 ( h j − 1 + h j ) c j + h j c j + 1 = 3 ( δ j − δ j − 1 ) , j = 1 , … , n − 1 h_{j-1}c_{j-1}+2(h_{j-1}+h_j)c_j+h_jc_{j+1} =3(\delta_j-\delta_{j-1}),
\qquad j=1,\ldots,n-1 h j − 1 c j − 1 + 2 ( h j − 1 + h j ) c j + h j c j + 1 = 3 ( δ j − δ j − 1 ) , j = 1 , … , n − 1
The c j c_j c j values satisfy n − 1 n-1 n − 1 interior equations and 2 boundary equations.
The boundary equations depend on the spline type.
For a natural spline, c 0 = c n = 0 c_0=c_n=0 c 0 = c n = 0 . And for a clamped spline:
The above equations for c i c_i c i can be expressed with a n + 1 n+1 n + 1 -variable tridiagonal matrix system.
The c \mathbf{c} c is the vector of all c i c_i c i (i i i from 0 0 0 to n n n ).
Only ( α 0 , β 0 , r 0 ) (\alpha_0,\beta_0,r_0) ( α 0 , β 0 , r 0 ) and ( α n , β n , r n ) (\alpha_n,\beta_n,r_n) ( α n , β n , r n ) change between the
2 spline types.
Coefficient Recovery
After solving for c 0 , … , c n c_0,\ldots,c_n c 0 , … , c n , recover the remaining coefficients on each
interval:
Error Bound
For f ∈ C 4 [ x 0 , x n ] f\in C^4[x_0,x_n] f ∈ C 4 [ x 0 , x n ] , let
If S S S is the clamped cubic spline interpolant and f ∈ C 4 [ x 0 , x n ] f\in C^4[x_0,x_n] f ∈ C 4 [ x 0 , x n ] , then
The natural spline also has fourth-order convergence, but its bound has a less
simple constant and depends on the endpoint behaviour.
Smaller intervals reduce the clamped error at the 4th-power rate.
Worked Examples
Example 1
Construct the natural cubic spline through ( 1 , 2 ) (1,2) ( 1 , 2 ) , ( 2 , 3 ) (2,3) ( 2 , 3 ) and ( 3 , 5 ) (3,5) ( 3 , 5 ) .
Solution Reveal Hide h 0 = h 1 = 1 , a 0 = 2 , a 1 = 3 , a 2 = 5 , c 0 = c 2 = 0 h_0=h_1=1,
\qquad
a_0=2,
a_1=3,
a_2=5,
\qquad
c_0=c_2=0 h 0 = h 1 = 1 , a 0 = 2 , a 1 = 3 , a 2 = 5 , c 0 = c 2 = 0 Direct Method
The only interior equation is
4 c 1 = 3 ( ( 5 − 3 ) − ( 3 − 2 ) ) = 3 4c_1=3\bigl((5-3)-(3-2)\bigr)=3 4 c 1 = 3 ( ( 5 − 3 ) − ( 3 − 2 ) ) = 3 Therefore
c 1 = 3 4 , c_1=\frac{3}{4}, c 1 = 4 3 , For [ 1 , 2 ] [1,2] [ 1 , 2 ] ,
b 0 = 1 − 1 3 ( 0 + 3 4 ) = 3 4 , d 0 = 3 / 4 − 0 3 = 1 4 b_0=1-\frac{1}{3}\left(0+\frac{3}{4}\right)=\frac{3}{4},
\qquad
d_0=\frac{3/4-0}{3}=\frac{1}{4} b 0 = 1 − 3 1 ( 0 + 4 3 ) = 4 3 , d 0 = 3 3/4 − 0 = 4 1 For [ 2 , 3 ] [2,3] [ 2 , 3 ] ,
b 1 = 2 − 1 3 ( 2 ⋅ 3 4 + 0 ) = 3 2 , d 1 = 0 − 3 / 4 3 = − 1 4 b_1=2-\frac{1}{3}\left(2\cdot\frac{3}{4}+0\right)=\frac{3}{2},
\qquad
d_1=\frac{0-3/4}{3}=-\frac{1}{4} b 1 = 2 − 3 1 ( 2 ⋅ 4 3 + 0 ) = 2 3 , d 1 = 3 0 − 3/4 = − 4 1 The spline is
S ( x ) = { 2 + 3 4 ( x − 1 ) + 1 4 ( x − 1 ) 3 , 1 ≤ x ≤ 2 , 3 + 3 2 ( x − 2 ) + 3 4 ( x − 2 ) 2 − 1 4 ( x − 2 ) 3 , 2 ≤ x ≤ 3. S(x)=
\begin{cases}
2+\frac{3}{4}(x-1)+\frac{1}{4}(x-1)^3, & 1\le x\le2,\\[4pt]
3+\frac{3}{2}(x-2)+\frac{3}{4}(x-2)^2-\frac{1}{4}(x-2)^3, & 2\le x\le3.
\end{cases} S ( x ) = { 2 + 4 3 ( x − 1 ) + 4 1 ( x − 1 ) 3 , 3 + 2 3 ( x − 2 ) + 4 3 ( x − 2 ) 2 − 4 1 ( x − 2 ) 3 , 1 ≤ x ≤ 2 , 2 ≤ x ≤ 3. Matrix Method
The natural matrix system is
[ 1 0 0 1 4 1 0 0 1 ] [ c 0 c 1 c 2 ] = [ 0 3 0 ] \begin{bmatrix}
1 & 0 & 0 \\
1 & 4 & 1 \\
0 & 0 & 1
\end{bmatrix}
\begin{bmatrix}
c_0\\c_1\\c_2
\end{bmatrix}
=
\begin{bmatrix}
0\\3\\0
\end{bmatrix} 1 1 0 0 4 0 0 1 1 c 0 c 1 c 2 = 0 3 0 Therefore
c 0 = 0 , c 1 = 3 4 , c 2 = 0 c_0=0,
\qquad
c_1=\frac{3}{4},
\qquad
c_2=0 c 0 = 0 , c 1 = 4 3 , c 2 = 0 The coefficient recovery gives
b 0 = 3 4 , d 0 = 1 4 , b 1 = 3 2 , d 1 = − 1 4 b_0=\frac{3}{4},
\qquad
d_0=\frac{1}{4},
\qquad
b_1=\frac{3}{2},
\qquad
d_1=-\frac{1}{4} b 0 = 4 3 , d 0 = 4 1 , b 1 = 2 3 , d 1 = − 4 1 Therefore
S ( x ) = { 2 + 3 4 ( x − 1 ) + 1 4 ( x − 1 ) 3 , 1 ≤ x ≤ 2 , 3 + 3 2 ( x − 2 ) + 3 4 ( x − 2 ) 2 − 1 4 ( x − 2 ) 3 , 2 ≤ x ≤ 3. S(x)=
\begin{cases}
2+\frac{3}{4}(x-1)+\frac{1}{4}(x-1)^3, & 1\le x\le2,\\[4pt]
3+\frac{3}{2}(x-2)+\frac{3}{4}(x-2)^2-\frac{1}{4}(x-2)^3, & 2\le x\le3.
\end{cases} S ( x ) = { 2 + 4 3 ( x − 1 ) + 4 1 ( x − 1 ) 3 , 3 + 2 3 ( x − 2 ) + 4 3 ( x − 2 ) 2 − 4 1 ( x − 2 ) 3 , 1 ≤ x ≤ 2 , 2 ≤ x ≤ 3.
Example 2
Construct the clamped cubic spline through ( 0 , 1 ) (0,1) ( 0 , 1 ) , ( 1 , 2 ) (1,2) ( 1 , 2 ) and ( 2 , 1 ) (2,1) ( 2 , 1 ) , given
the endpoint slopes S ′ ( 0 ) = 1 S'(0)=1 S ′ ( 0 ) = 1 and S ′ ( 2 ) = − 1 S'(2)=-1 S ′ ( 2 ) = − 1 .
Solution Reveal Hide h 0 = h 1 = 1 , a 0 = 1 , a 1 = 2 , a 2 = 1 , δ 0 = 1 , δ 1 = − 1 h_0=h_1=1,
\qquad
a_0=1,
a_1=2,
a_2=1,
\qquad
\delta_0=1,
\qquad
\delta_1=-1 h 0 = h 1 = 1 , a 0 = 1 , a 1 = 2 , a 2 = 1 , δ 0 = 1 , δ 1 = − 1 Direct Method
The clamped boundary equations are
2 c 0 + c 1 = 3 ( 1 − 1 ) = 0 , c 1 + 2 c 2 = 3 ( − 1 + 1 ) = 0 2c_0+c_1=3(1-1)=0,
\qquad
c_1+2c_2=3(-1+1)=0 2 c 0 + c 1 = 3 ( 1 − 1 ) = 0 , c 1 + 2 c 2 = 3 ( − 1 + 1 ) = 0 The interior equation is
c 0 + 4 c 1 + c 2 = 3 ( − 1 − 1 ) = − 6 c_0+4c_1+c_2=3(-1-1)=-6 c 0 + 4 c 1 + c 2 = 3 ( − 1 − 1 ) = − 6 Therefore
c 0 = 1 , c 1 = − 2 , c 2 = 1 c_0=1,
\qquad
c_1=-2,
\qquad
c_2=1 c 0 = 1 , c 1 = − 2 , c 2 = 1 The remaining coefficients are
b 0 = 1 , d 0 = − 1 , b 1 = 0 , d 1 = 1 b_0=1,
\qquad
d_0=-1,
\qquad
b_1=0,
\qquad
d_1=1 b 0 = 1 , d 0 = − 1 , b 1 = 0 , d 1 = 1 The spline is
S ( x ) = { 1 + x + x 2 − x 3 , 0 ≤ x ≤ 1 , 2 − 2 ( x − 1 ) 2 + ( x − 1 ) 3 , 1 ≤ x ≤ 2. S(x)=
\begin{cases}
1+x+x^2-x^3, & 0\le x\le1,\\[4pt]
2-2(x-1)^2+(x-1)^3, & 1\le x\le2.
\end{cases} S ( x ) = { 1 + x + x 2 − x 3 , 2 − 2 ( x − 1 ) 2 + ( x − 1 ) 3 , 0 ≤ x ≤ 1 , 1 ≤ x ≤ 2. Matrix Method
The clamped matrix system is
[ 2 1 0 1 4 1 0 1 2 ] [ c 0 c 1 c 2 ] = [ 0 − 6 0 ] \begin{bmatrix}
2 & 1 & 0 \\
1 & 4 & 1 \\
0 & 1 & 2
\end{bmatrix}
\begin{bmatrix}
c_0\\c_1\\c_2
\end{bmatrix}
=
\begin{bmatrix}
0\\-6\\0
\end{bmatrix} 2 1 0 1 4 1 0 1 2 c 0 c 1 c 2 = 0 − 6 0 Therefore
c 0 = 1 , c 1 = − 2 , c 2 = 1 c_0=1,
\qquad
c_1=-2,
\qquad
c_2=1 c 0 = 1 , c 1 = − 2 , c 2 = 1 The remaining coefficients are
b 0 = 1 , d 0 = − 1 , b 1 = 0 , d 1 = 1 b_0=1,
\qquad
d_0=-1,
\qquad
b_1=0,
\qquad
d_1=1 b 0 = 1 , d 0 = − 1 , b 1 = 0 , d 1 = 1 The spline is
S ( x ) = { 1 + x + x 2 − x 3 , 0 ≤ x ≤ 1 , 2 − 2 ( x − 1 ) 2 + ( x − 1 ) 3 , 1 ≤ x ≤ 2. S(x)=
\begin{cases}
1+x+x^2-x^3, & 0\le x\le1,\\[4pt]
2-2(x-1)^2+(x-1)^3, & 1\le x\le2.
\end{cases} S ( x ) = { 1 + x + x 2 − x 3 , 2 − 2 ( x − 1 ) 2 + ( x − 1 ) 3 , 0 ≤ x ≤ 1 , 1 ≤ x ≤ 2.
Example 3
For f ( x ) = x 4 f(x)=x^4 f ( x ) = x 4 on [ 0 , 1 ] [0,1] [ 0 , 1 ] , use a clamped cubic spline with nodes
x 0 = 0 x_0=0 x 0 = 0 , x 1 = 1 2 x_1=\tfrac12 x 1 = 2 1 and x 2 = 1 x_2=1 x 2 = 1 . Find the error bound.
Solution Reveal Hide Since
f ( 4 ) ( x ) = 24 , f^{(4)}(x)=24, f ( 4 ) ( x ) = 24 , we have
M = 24 , h = max ( h 0 , h 1 ) = 1 2 M=24,
\qquad
h=\max(h_0,h_1)=\frac12 M = 24 , h = max ( h 0 , h 1 ) = 2 1 The clamped error bound is
∣ f ( x ) − S ( x ) ∣ ≤ 5 M 384 h 4 = 5 ( 24 ) 384 ( 1 2 ) 4 = 5 256 \lvert f(x)-S(x)\rvert
\le\frac{5M}{384}h^4
=\frac{5(24)}{384}\left(\frac12\right)^4
=\frac{5}{256} ∣ f ( x ) − S ( x )∣ ≤ 384 5 M h 4 = 384 5 ( 24 ) ( 2 1 ) 4 = 256 5