Cubic Spline Interpolation - MATH 375, Numerical Analysis

Cubic Spline Interpolation
MATH 375, Numerical Analysis
J. Robert Buchanan
Department of Mathematics
Fall 2013
J. Robert Buchanan
Cubic Spline Interpolation
History
Given nodes and data {(x0 , f (x0 )), (x1 , f (x1 )), . . . , (xn , f (xn ))}
we have interpolated using:
Lagrange interpolating polynomial of degree n, with n + 1
coefficients,
unfortunately,
J. Robert Buchanan
Cubic Spline Interpolation
History
Given nodes and data {(x0 , f (x0 )), (x1 , f (x1 )), . . . , (xn , f (xn ))}
we have interpolated using:
Lagrange interpolating polynomial of degree n, with n + 1
coefficients,
unfortunately,
such polynomials can possess large oscillations, and
the error term can be difficult to construct and estimate.
J. Robert Buchanan
Cubic Spline Interpolation
History
Given nodes and data {(x0 , f (x0 )), (x1 , f (x1 )), . . . , (xn , f (xn ))}
we have interpolated using:
Lagrange interpolating polynomial of degree n, with n + 1
coefficients,
unfortunately,
such polynomials can possess large oscillations, and
the error term can be difficult to construct and estimate.
An alternative is piecewise polynomial approximation, but of
what degree polynomial?
J. Robert Buchanan
Cubic Spline Interpolation
History
Given nodes and data {(x0 , f (x0 )), (x1 , f (x1 )), . . . , (xn , f (xn ))}
we have interpolated using:
Lagrange interpolating polynomial of degree n, with n + 1
coefficients,
unfortunately,
such polynomials can possess large oscillations, and
the error term can be difficult to construct and estimate.
An alternative is piecewise polynomial approximation, but of
what degree polynomial?
Piecewise linear results are not differentiable at xi ,
i = 0, 1, . . . , n.
Piecewise quadratic results are not twice differentiable at
xi , i = 0, 1, . . . , n.
Piecewise cubic!
J. Robert Buchanan
Cubic Spline Interpolation
Cubic Splines
A cubic polynomial p(x) = a + bx + cx 2 + dx 3 is specified
by 4 coefficients.
The cubic spline is twice continuously differentiable.
The cubic spline has the flexibility to satisfy general types
of boundary conditions.
While the spline may agree with f (x) at the nodes, we
cannot guarantee the derivatives of the spline agree with
the derivatives of f .
J. Robert Buchanan
Cubic Spline Interpolation
Cubic Spline Interpolant (1 of 2)
Given a function f (x) defined on [a, b] and a set of nodes
a = x0 < x1 < x2 < · · · < xn = b,
a cubic spline interpolant, S, for f is a piecewise cubic
polynomial, Sj on [xj , xj+1 ] for j = 0, 1, . . . , n − 1.
S(x) =



















a0 + b0 (x − x0 ) + c0 (x − x0 )2 + d0 (x − x0 )3
a1 + b1 (x − x1 ) + c1 (x − x1 )2 + d1 (x − x1 )3
..
.
if x0 ≤ x ≤ x1
if x1 ≤ x ≤ x2
..
.
ai + bi (x − xi ) + ci (x − xi )2 + di (x − xi )3
..
.
if xi ≤ x ≤ xi+1
..
.
an−1 + bn−1 (x − xn−1 ) + cn−1 (x − xn−1 )2 + dn−1 (x − xn−1 )3 if xn−1 ≤ x ≤ xn
J. Robert Buchanan
Cubic Spline Interpolation
Cubic Spline Interpolant (2 of 2)
The cubic spline interpolant will have the following properties.
S(xj ) = f (xj ) for j = 0, 1, . . . , n.
Sj (xj+1 ) = Sj+1 (xj+1 ) for j = 0, 1, . . . , n − 2.
0 (x
Sj0 (xj+1 ) = Sj+1
j+1 ) for j = 0, 1, . . . , n − 2.
00 (x
Sj00 (xj+1 ) = Sj+1
j+1 ) for j = 0, 1, . . . , n − 2.
One of the following boundary conditions (BCs) is satisfied:
S 00 (x0 ) = S 00 (xn ) = 0 (free or natural BCs).
S 0 (x0 ) = f 0 (x0 ) and S 0 (xn ) = f 0 (xn ) (clamped BCs).
J. Robert Buchanan
Cubic Spline Interpolation
Example (1 of 7)
Construct a piecewise cubic spline interpolant for the curve
passing through
{(5, 5), (7, 2), (9, 4)},
with natural boundary conditions.
y
6
5
4
3
2
1
4
5
6
J. Robert Buchanan
7
8
9
Cubic Spline Interpolation
10
x
Example (2 of 7)
This will require two cubics:
S0 (x) = a0 + b0 (x − 5) + c0 (x − 5)2 + d0 (x − 5)3
S1 (x) = a1 + b1 (x − 7) + c1 (x − 7)2 + d1 (x − 7)3
Since there are 8 coefficients, we must derive 8 equations to
solve.
J. Robert Buchanan
Cubic Spline Interpolation
Example (2 of 7)
This will require two cubics:
S0 (x) = a0 + b0 (x − 5) + c0 (x − 5)2 + d0 (x − 5)3
S1 (x) = a1 + b1 (x − 7) + c1 (x − 7)2 + d1 (x − 7)3
Since there are 8 coefficients, we must derive 8 equations to
solve.
The splines must agree with the function (the y -coordinates) at
the nodes (the x-coordinates).
5 = S0 (5) = a0
2 = S0 (7) = a0 + 2b0 + 4c0 + 8d0
2 = S1 (7) = a1
4 = S1 (9) = a1 + 2b1 + 4c1 + 8d1
J. Robert Buchanan
Cubic Spline Interpolation
Example (3 of 7)
The first and second derivatives of the cubics must agree at
their shared node x = 7.
S00 (7) = b0 + 4c0 + 12d0 = b1 = S10 (7)
S000 (7) = 2c0 + 12d0 = 2c1 = S100 (7)
J. Robert Buchanan
Cubic Spline Interpolation
Example (3 of 7)
The first and second derivatives of the cubics must agree at
their shared node x = 7.
S00 (7) = b0 + 4c0 + 12d0 = b1 = S10 (7)
S000 (7) = 2c0 + 12d0 = 2c1 = S100 (7)
The final two equations come from the natural boundary
conditions.
S000 (5) = 0 = 2c0
S100 (9) = 0 = 2c1 + 12d1
J. Robert Buchanan
Cubic Spline Interpolation
Example (4 of 7)
All eight linear equations together form the system:
5 = a0
2 = a0 + 2b0 + 4c0 + 8d0
2 = a1
4 = a1 + 2b1 + 4c1 + 8d1
0 = b0 + 4c0 + 12d0 − b1
0 = 2c0 + 12d0 − 2c1
0 = 2c0
0 = 2c1 + 12d1
J. Robert Buchanan
Cubic Spline Interpolation
Example (5 of 7)
The solution is:
i
ai
bi
ci
di
0
5
−
17
8
0
5
32
1
2
−
1
4
15
16
J. Robert Buchanan
−
5
32
Cubic Spline Interpolation
Example (6 of 7)
The natural cubic spline can be expressed as:

17
5

 5−
(x − 5) +
(x − 5)3
if 5 ≤ x ≤ 7


8
32
S(x) =



 2 − 1 (x − 7) + 15 (x − 7)2 − 5 (x − 7)3 if 7 ≤ x ≤ 9
4
16
32
y
6
5
4
3
2
1
4
5
6
J. Robert Buchanan
7
8
9
Cubic Spline Interpolation
10
x
Example (7 of 7)
We can verify the continuity of the first and second derivatives
from the following graphs.
S''HxL
S'HxL
1.5
1.5
1.0
0.5
6
7
8
9
x
1.0
-0.5
0.5
-1.0
-1.5
-2.0
6
J. Robert Buchanan
Cubic Spline Interpolation
7
8
9
x
General Construction Process
Given n + 1 nodal/data values:
{(x0 , f (x0 )), (x1 , f (x1 )), . . . , (xn , f (xn ))} we will create n cubic
polynomials.
J. Robert Buchanan
Cubic Spline Interpolation
General Construction Process
Given n + 1 nodal/data values:
{(x0 , f (x0 )), (x1 , f (x1 )), . . . , (xn , f (xn ))} we will create n cubic
polynomials.
For j = 0, 1, . . . , n − 1 assume
Sj (x) = aj + bj (x − xj ) + cj (x − xj )2 + dj (x − xj )3 .
We must find aj , bj , cj and dj (a total of 4n unknowns) subject to
the conditions specified in the definition.
J. Robert Buchanan
Cubic Spline Interpolation
First Set of Equations
Let hj = xj+1 − xj then
Sj (xj ) = aj = f (xj )
Sj+1 (xj+1 ) = aj+1 = Sj (xj+1 ) = aj + bj hj + cj hj2 + dj hj3 .
So far we know aj for j = 0, 1, . . . , n − 1 and have n equations
and 3n unknowns.
a1 = a0 + b0 h0 + c0 h02 + d0 h03
..
.
aj+1 = aj + bj hj + cj hj2 + dj hj3
..
.
2
3
an = an−1 + bn−1 hn−1 + cn−1 hn−1
+ dn−1 hn−1
J. Robert Buchanan
Cubic Spline Interpolation
First Derivative
The continuity of the first derivative at the nodal points
produces n more equations.
For j = 0, 1, . . . , n − 1 we have
Sj0 (x) = bj + 2cj (x − xj ) + 3dj (x − xj )2 .
Thus
Sj0 (xj ) = bj
0
Sj+1
(xj+1 ) = bj+1 = Sj0 (xj+1 ) = bj + 2cj hj + 3dj hj2
Now we have 2n equations and 3n unknowns.
J. Robert Buchanan
Cubic Spline Interpolation
Equations Derived So Far
aj+1 = aj + bj hj + cj hj2 + dj hj3
b1 = b0 + 2c0 h0 +
..
.
(for j = 0, 1, . . . , n − 1)
3d0 h02
bj+1 = bj + 2cj hj + 3dj hj2
..
.
2
bn = bn−1 + 2cn−1 hn−1 + 3dn−1 hn−1
The unknowns are bj , cj , and dj for j = 0, 1, . . . , n − 1.
J. Robert Buchanan
Cubic Spline Interpolation
Second Derivative
The continuity of the second derivative at the nodal points
produces n more equations.
For j = 0, 1, . . . , n − 1 we have
Sj00 (x) = 2cj + 6dj (x − xj ).
Thus
Sj00 (xj ) = 2cj
00
Sj+1
(xj+1 ) = 2cj+1 = Sj00 (xj+1 ) = 2cj + 6dj hj
Now we have 3n equations and 3n unknowns.
J. Robert Buchanan
Cubic Spline Interpolation
Equations Derived So Far
aj+1 = aj + bj hj + cj hj2 + dj hj3
bj+1 = bj + 2cj hj + 3dj hj2
(for j = 0, 1, . . . , n − 1)
(for j = 0, 1, . . . , n − 1)
2c1 = 2c0 + 6d0 h0
..
.
2cj+1 = 2cj + 6dj hj
..
.
2cn = 2cn−1 + 6dn−1 hn−1
The unknowns are bj , cj , and dj for j = 0, 1, . . . , n − 1.
J. Robert Buchanan
Cubic Spline Interpolation
Summary of Equations
For j = 0, 1, . . . , n − 1 we have
aj+1 = aj + bj hj + cj hj2 + dj hj3
bj+1 = bj + 2cj hj + 3dj hj2
cj+1 = cj + 3dj hj .
Note: The quantities aj and hj are known.
J. Robert Buchanan
Cubic Spline Interpolation
Summary of Equations
For j = 0, 1, . . . , n − 1 we have
aj+1 = aj + bj hj + cj hj2 + dj hj3
bj+1 = bj + 2cj hj + 3dj hj2
cj+1 = cj + 3dj hj .
Note: The quantities aj and hj are known.
Solve the third equation for dj and substitute into the other two
equations.
cj+1 − cj
dj =
3hj
This eliminates n equations of the third type.
J. Robert Buchanan
Cubic Spline Interpolation
Solving the Equations (1 of 3)
aj+1 = aj + bj hj +
+
cj+1 − cj
3hj
hj2
(2cj + cj+1 )
3
cj+1 − cj
= bj + 2cj hj + 3
hj2
3hj
= bj + hj (cj + cj+1 )
= aj + bj hj +
bj+1
cj hj2
J. Robert Buchanan
Cubic Spline Interpolation
hj3
Solving the Equations (1 of 3)
aj+1 = aj + bj hj +
+
cj+1 − cj
3hj
hj2
(2cj + cj+1 )
3
cj+1 − cj
= bj + 2cj hj + 3
hj2
3hj
= bj + hj (cj + cj+1 )
= aj + bj hj +
bj+1
cj hj2
Solve the first equation for bj .
bj =
hj
1
(aj+1 − aj ) − (2cj + cj+1 )
hj
3
J. Robert Buchanan
Cubic Spline Interpolation
hj3
Solving the Equations (2 of 3)
Replace index j by j − 1 to obtain
bj−1 =
1
hj−1
(aj − aj−1 ) −
J. Robert Buchanan
hj−1
(2cj−1 + cj ).
3
Cubic Spline Interpolation
Solving the Equations (2 of 3)
Replace index j by j − 1 to obtain
bj−1 =
1
hj−1
(aj − aj−1 ) −
hj−1
(2cj−1 + cj ).
3
We can also re-index the earlier equation
bj+1 = bj + hj (cj + cj+1 )
to obtain
bj = bj−1 + hj−1 (cj−1 + cj ).
Substitute the equations for bj−1 and bj into the remaining
equation. This step eliminate n equations of the first type.
J. Robert Buchanan
Cubic Spline Interpolation
Solving the Equations (3 of 3)
hj
1
(aj+1 − aj ) − (2cj + cj+1 )
hj
3
hj−1
1
(aj − aj−1 ) −
(2cj−1 + cj ) + hj−1 (cj−1 + cj )
=
hj−1
3
Collect all terms involving c to one side.
hj−1 cj−1 +2cj (hj−1 +hj )+hj cj+1 =
3
3
(aj+1 −aj )−
(aj −aj−1 )
hj
hj−1
for j = 1, 2, . . . , n − 1.
J. Robert Buchanan
Cubic Spline Interpolation
Solving the Equations (3 of 3)
hj
1
(aj+1 − aj ) − (2cj + cj+1 )
hj
3
hj−1
1
(aj − aj−1 ) −
(2cj−1 + cj ) + hj−1 (cj−1 + cj )
=
hj−1
3
Collect all terms involving c to one side.
hj−1 cj−1 +2cj (hj−1 +hj )+hj cj+1 =
3
3
(aj+1 −aj )−
(aj −aj−1 )
hj
hj−1
for j = 1, 2, . . . , n − 1.
Remark: we have n − 1 equations and n + 1 unknowns.
J. Robert Buchanan
Cubic Spline Interpolation
Natural Boundary Conditions
If S 00 (x0 ) = S000 (x0 ) = 2c0 = 0 then c0 = 0 and if
00 (x ) = 2c = 0 then c = 0.
S 00 (xn ) = Sn−1
n
n
n
J. Robert Buchanan
Cubic Spline Interpolation
Natural Boundary Conditions
If S 00 (x0 ) = S000 (x0 ) = 2c0 = 0 then c0 = 0 and if
00 (x ) = 2c = 0 then c = 0.
S 00 (xn ) = Sn−1
n
n
n
Theorem
If f is defined at a = x0 < x1 < · · · < xn = b then f has a unique
natural cubic spline interpolant.
J. Robert Buchanan
Cubic Spline Interpolation
Natural BC Linear System (1 of 3)
In matrix form the system of n + 1 equations has the form
Ac = y where

1
0
0
0
···
0
 h0 2(h0 + h1 )
h1
0
···
0

 0
h
2(h
+
h
)
h
·
·
·
0
1
1
2
2

A= .
.
.
.
..
..
..
..
 ..
.

 0
0
0
hn−2 2(hn−2 + hn−1 ) hn−1
0
0
0
0
···
1
Note: A is a tridiagonal matrix.
J. Robert Buchanan
Cubic Spline Interpolation









Natural BC Linear System (2 of 3)
The vector y on the right-hand side is formed as

0
3
3

(a
−
a
)
−
2
1

h1
h0 (a1 − a0 )

3
3

h2 (a3 − a2 ) − h1 (a2 − a1 )

y=
..
.

 3
 h (an − an−1 ) − h 3 (an−1 − an−2 )
n−1
n−2
0
Note: A is a tridiagonal matrix.
J. Robert Buchanan
Cubic Spline Interpolation










Natural BC Linear System (3 of 3)

c0
c1
c2
..
.




A


 cn−1
cn


 
 
 
 
=
 
 
 

0
3
3
h1 (a2 − a1 ) − h0 (a1 − a0 )
3
3
h2 (a3 − a2 ) − h1 (a2 − a1 )
..
.
3
3
(a
−
a
)
−
n
n−1
hn−1
hn−2 (an−1 − an−2 )
0
We solve this linear system of equations using a common
algorithm for handling tridiagonal systems.
J. Robert Buchanan
Cubic Spline Interpolation










Natural Cubic Spline Algorithm
INPUT {(x0 , f (x0 )), (x1 , f (x1 )), . . . , (xn , f (xn ))}
STEP 1 For i = 0, 1, . . . , n − 1 set ai = f (xi ); set
hi = xi+1 − xi .
STEP 2 For i = 1, 2, . . . , n − 1 set
3
3
(ai − ai−1 ).
αi = (ai+1 − ai ) −
hi
hi−1
STEP 3 Set l0 = 1; set µ0 = 0; set z0 = 0.
STEP 4 For i = 1, 2, . . . , n − 1 set
hi
li = 2(xi+1 − xi−1 ) − hi−1 µi−1 ; set µi = ; set
li
αi − hi−1 zi−1
zi =
.
li
STEP 5 Set ln = 1; set cn = 0; set zn = 0.
STEP 6 For j = n − 1, n − 2, . . . , 0 set cj = zj − µj cj+1 ; set
hj (cj+1 + 2cj )
cj+1 − cj
aj+1 − aj
bj =
−
; set dj =
.
hj
3
3hj
STEP 7 For j = 0, 1, . . . , n − 1 OUTPUT aj , bj , cj , dj .
J. Robert Buchanan
Cubic Spline Interpolation
Example (1 of 4)
Construct the natural cubic spline interpolant for
f (x) = ln(ex + 2) with nodal values:
x
−1.0
−0.5
0.0
0.5
f (x)
0.86199480
0.95802009
1.0986123
1.2943767
Calculate the absolute error in using the interpolant to
approximate f (0.25) and f 0 (0.25).
J. Robert Buchanan
Cubic Spline Interpolation
Example (2 of 4)
In this case n = 3 and
h0 = h1 = h2 = 0.5
with
a0 = 0.86199480, a1 = 0.95802009,
a2 = 1.0986123, a3 = 1.2943767.
The linear system resembles,

1.0 0.0 0.0 0.0
 0.5 2.0 0.5 0.0
Ac = 
 0.0 0.5 2.0 0.5
0.0 0.0 0.0 1.0
J. Robert Buchanan

 
c0
0.0
  c1   0.267402

 
  c2  =  0.331034
c3
0.0
Cubic Spline Interpolation


=y

Example (3 of 4)
The coefficients of the piecewise cubics:
i
0
1
2
ai
0.861995
0.95802
1.09861
bi
0.175638
0.224876
0.344563
J. Robert Buchanan
ci
0.0
0.0984763
0.140898
di
0.0656509
0.028281
−0.093918
Cubic Spline Interpolation
Example (3 of 4)
The coefficients of the piecewise cubics:
i
0
1
2
ai
0.861995
0.95802
1.09861
bi
0.175638
0.224876
0.344563
ci
0.0
0.0984763
0.140898
di
0.0656509
0.028281
−0.093918
The cubic spline:

0.861995 + 0.175638(x + 1) if −1 ≤ x ≤ −0.5




+ 0.0656509(x + 1)3




 0.95802 + 0.224876(x + 0.5)
+ 0.0984763(x + 0.5)2
if −0.5 ≤ x ≤ 0
S(x) =

3


+
0.028281(x
+
0.5)




1.09861 + 0.344563x
if 0 ≤ x ≤ 0.5


2
3
+ 0.140898x − 0.093918x
J. Robert Buchanan
Cubic Spline Interpolation
Example (4 of 4)
y
1.3
1.2
1.1
1.0
-1.0
f (0.25)
1.18907
-0.8
S(0.25)
1.19209
-0.6
-0.4
Abs. Err.
3.02154 × 10−3
J. Robert Buchanan
-0.2
f 0 (0.25)
0.390991
0.2
S 0 (0.25)
0.3974
Cubic Spline Interpolation
0.4
x
Abs. Err.
6.40839 × 10−3
Clamped Boundary Conditions (1 of 2)
If S 0 (a) = S00 (a) = f 0 (a) = b0 then
f 0 (a) =
1
h0
(a1 − a0 ) − (2c0 + c1 )
h0
3
which is equivalent to
h0 (2c0 + c1 ) =
3
(a1 − a0 ) − 3f 0 (a).
h0
This replaces the first equation in our system of n equations.
J. Robert Buchanan
Cubic Spline Interpolation
Clamped Boundary Conditions (2 of 2)
Likewise if S 0 (b) = Sn0 (b) = f 0 (b) = bn then
bn = bn−1 + hn−1 (cn−1 + cn )
hn−1
1
(an − an−1 ) −
(2cn−1 + cn ) + hn−1 (cn−1 + cn )
=
hn−1
3
1
hn−1
=
(an − an−1 ) +
(cn−1 + 2cn )
hn−1
3
which is equivalent to
hn−1 (cn−1 + 2cn ) = 3f 0 (b) −
3
hn−1
(an − an−1 ).
This replaces the last equation in our system of n equations.
J. Robert Buchanan
Cubic Spline Interpolation
Clamped BC Linear System (1 of 2)
Theorem
If f is defined at a = x0 < x1 < · · · < xn = b and differentiable at
x = a and at x = b, then f has a unique clamped cubic spline
interpolant.
In matrix form the system of n equations has the form Ac = y
where

2h0
h0
0
0
···
0
 h0 2(h0 + h1 )
h
0
·
·
·
0
1

 0
h1
2(h1 + h2 ) h2
···
0

A= .
..
..
..
..
.
 .
.
.
.
.

 0
0
0
hn−2 2(hn−2 + hn−1 ) hn−1
0
0
0
···
hn−1
2hn−1
Note: A is a tridiagonal matrix.
J. Robert Buchanan
Cubic Spline Interpolation









Clamped BC Linear System (2 of 2)

c0
c1
c2
..
.




A


 cn−1
cn



 
 
 
 
=
 
 
 

3
h0 (a1
− a0 ) − 3f 0 (a)
3
3
h1 (a2 − a1 ) − h0 (a1 − a0 )
3
3
h2 (a3 − a2 ) − h1 (a2 − a1 )
..
.
3
3
hn−1 (an − an−1 ) − hn−2 (an−1 − an−2 )
3
3f 0 (b) − hn−1
(an − an−1 )
J. Robert Buchanan
Cubic Spline Interpolation











Coefficients of the Cubic Splines
Since aj for j = 0, 1, . . . , n is known, once we solve the linear
system for cj (again for j = 0, 1, . . . , n) then
bj
=
dj
=
hj
1
(aj+1 − aj ) − (cj+1 + 2cj )
hj
3
1
(cj+1 − cj )
3hj
for j = 0, 1, . . . , n − 1.
J. Robert Buchanan
Cubic Spline Interpolation
Clamped Cubic Spline Algorithm (1 of 2)
INPUT {(x0 , f (x0 )), (x1 , f (x1 )), . . . , (xn , f (xn ))}, f 0 (x0 ), and
f 0 (xn ).
STEP 1 For i = 0, 1, . . . , n − 1 set ai = f (xi ); set
hi = xi+1 − xi .
3(a1 − a0 )
− 3f 0 (x0 );
STEP 2 Set α0 =
h0
3(an − an−1 )
αn = 3f 0 (xn ) −
.
hn−1
STEP 3 For i = 1, 2, . . . , n − 1 set
3
3
αi = (ai+1 − ai ) −
(ai − ai−1 ).
hi
hi−1
α0
STEP 4 Set l0 = 2h0 ; µ0 = 0.5; z0 =
.
l0
J. Robert Buchanan
Cubic Spline Interpolation
Clamped Cubic Spline Algorithm (2 of 2)
STEP 5 For i = 1, 2, . . . , n − 1 set
li = 2(xi+1 − xi−1 ) − hi−1 µi−1 ; µi =
zi =
αi − hi−1 zi−1
.
li
STEP 6 Set ln = hn−1 (2 − µn−1 ); zn =
cn = zn .
hi
;
li
αn − hn−1 zn−1
;
ln
STEP 7 For j = n − 1, n − 2, . . . , 0 set cj = zj − µj cj+1 ;
cj+1 − cj
aj+1 − aj
hj (cj+1 + 2cj )
; dj =
bj =
−
.
hj
3
3hj
STEP 8 For j = 0, 1, . . . , n − 1 OUTPUT aj , bj , cj , dj .
J. Robert Buchanan
Cubic Spline Interpolation
Example (1 of 4)
Construct the clamped cubic spline interpolant for
f (x) = ln(ex + 2) with nodal values:
x
−1.0
−0.5
0.0
0.5
f (x)
0.86199480
0.95802009
1.0986123
1.2943767
Calculate the absolute error in using the interpolant to
approximate f (0.25) and f 0 (0.25).
J. Robert Buchanan
Cubic Spline Interpolation
Example (2 of 4)
In this case n = 3 and
h0 = h1 = h2 = 0.5
with
a0 = 0.86199480, a1 = 0.95802009,
a2 = 1.0986123, a3 = 1.2943767.
Note that f 0 (−1) ≈ 0.155362 and f 0 (0.5) ≈ 0.451863.
The linear system resembles,


1.0 0.5 0.0 0.0
 0.5 2.0 0.5 0.0  

Ac = 
 0.0 0.5 2.0 0.5  
0.0 0.0 0.5 1.0
J. Robert Buchanan
 
c0
0.110064
 0.267402
c1 
=
c2   0.331034
c3
0.181001
Cubic Spline Interpolation


 = y.

Example (3 of 4)
The coefficients of the piecewise cubics:
i
0
1
2
ai
0.861995
0.95802
1.09861
bi
0.155362
0.23274
0.333384
J. Robert Buchanan
ci
0.0653748
0.0893795
0.11191
di
0.0160031
0.0150207
0.00875717
Cubic Spline Interpolation
Example (3 of 4)
The coefficients of the piecewise cubics:
i
0
1
2
ai
0.861995
0.95802
1.09861
bi
0.155362
0.23274
0.333384
ci
0.0653748
0.0893795
0.11191
di
0.0160031
0.0150207
0.00875717
The cubic spline:

0.861995 + 0.155362(x + 1)




+ 0.0653748(x + 1)2
if −1 ≤ x ≤ −0.5



3

+ 0.0160031(x + 1)



0.95802 + 0.23274(x + 0.5)
S(x) =
+ 0.0893795(x + 0.5)2
if −0.5 ≤ x ≤ 0



3


+ 0.0150207(x + 0.5)




1.09861
+ 0.333384x + 0.11191x 2 if 0 ≤ x ≤ 0.5


+ 0.00875717x 3
J. Robert Buchanan
Cubic Spline Interpolation
Example (4 of 4)
y
1.3
1.2
1.1
1.0
-1.0
f (0.25)
1.18907
-0.8
S(0.25)
1.18991
-0.6
-0.4
Abs. Err.
1.97037 × 10−5
J. Robert Buchanan
-0.2
f 0 (0.25)
0.390991
0.2
0.4
S 0 (0.25)
0.390982
Cubic Spline Interpolation
x
Abs. Err.
9.67677 × 10−6
Error Analysis
Theorem
Let f ∈
C 4 [a, b]
(4) with max f (x) = M. If S is the unique
a≤x≤b
clamped cubic spline interpolant to f with respect to nodes
a = x0 < x1 < · · · < xn = b, then for all x ∈ [a, b],
|f (x) − S(x)| ≤
5M
max (xj+1 − xj )4 .
384 0≤j≤n−1
J. Robert Buchanan
Cubic Spline Interpolation
Example
Earlier we found the clamped cubic spline interpolant for
f (x) = ln(ex + 2). In this example xj+1 − xj = 0.5 for all j.
Note that
max
−1≤x≤0.5
2ex (4 − 8ex + e2x )
f (4) (x) =
(2 + ex )4
(4) f (x) ≈ 0.120398
|f (0.25) − S(0.25)| = 1.97037 × 10−5
5(0.120398)
≤
(0.5)4
384
≈ 9.798 × 10−5 .
J. Robert Buchanan
Cubic Spline Interpolation
Natural Cubic Spline Example (1 of 3)
Consider the following data:
x
−0.5
−0.25
0.0
f (x)
−0.02475
0.334938
1.101
The linear system takes the form


Ac = y



1.00 0.00 0.00
c0
0.00
 0.25 1.00 0.25   c1  =  4.8765 
0.00 0.00 1.00
c2
0.00
J. Robert Buchanan
Cubic Spline Interpolation
Natural Cubic Spline Example (2 of 3)
The coefficients of the natural cubic spline interpolant are
ai
−0.02475
0.334938
bi
1.03238
2.2515
ci
0.0
4.8765
di
6.502
−6.502
and the piecewise cubic is
S(x) =
−0.02475 + 1.03238(x + 0.5) + 6.502(x + 0.05)3
if −0.5 ≤ x ≤ −0.25
0.334938 + 2.2515(x + 0.25) + 4.8765(x + 0.25)2 − 6.502(x + 0.25)3 if −0.25 ≤ x ≤ 0.
J. Robert Buchanan
Cubic Spline Interpolation
Natural Cubic Spline Example (3 of 3)
fHxL
1
0.8
0.6
0.4
0.2
x
-0.5
-0.4
-0.3
J. Robert Buchanan
-0.2
-0.1
Cubic Spline Interpolation
Clamped Cubic Spline Example (1 of 4)
Here we will find the
√ clamped cubic spline interpolant to the
function f (x) = J0 ( x) at the nodes xi = 5i for i = 0, 1, . . . , 10.
x
0.0
5.0
10.0
..
.
f (x)
1.0
0.0904053
−0.310045
..
.
50.0
0.299655
Note: f 0 (0) = −0.25 and f 0 (50) = −0.00117217.
J. Robert Buchanan
Cubic Spline Interpolation
Clamped Cubic Spline Example (2 of 4)
The tridiagonal linear system takes the following form












10 5 0 0 · · ·
5 20 5 0 · · ·
0 5 20 5 · · ·
..
..
.
.
0 0 0 0 ···
0 0 0 0 ···
0 0 0 0 ···
0
0
0
0
0
0
0
0
0
0
0
0
..
.

c0
c1
c2
..
.








5 20 5 0 
  c8
 c9

0 5 20 5
c10
0 0 5 10
J. Robert Buchanan




 
 
 
 
 
=
 
 
 
 





Cubic Spline Interpolation
0.204243
0.305487
0.184846
0.100749
0.044242
0.008211
−0.012944
−0.023582
−0.027056
−0.025905
−0.011808










.








Clamped Cubic Spline Example (3 of 4)
The coefficients of the clamped cubic spline interpolant are
ai
1
0.09040533
−0.3100448
−0.4024176
−0.3268753
−0.1775968
−0.0146336
0.12675676
0.22884382
0.28583684
bi
−0.25
−0.1230843
−0.0436897
0.00214934
0.02499404
0.03276697
0.03128308
0.02471281
0.01595213
0.00692671
J. Robert Buchanan
ci
0.0154655
0.009917643
0.005961278
0.003206529
0.001362412
0.000192174
−0.00048895
−0.00082510
−0.00092703
−0.00087805
di
−0.00036986
−0.0002637577
−0.0001836499
−0.0001229411
−0.0000780158
−0.0000454083
−0.0000224102
−6.79522 × 10−6
3.265389 × 10−6
9.088463 × 10−6
Cubic Spline Interpolation
Clamped Cubic Spline Example (4 of 4)
y
1.0
0.8
0.6
0.4
0.2
10
20
30
40
-0.2
-0.4
J. Robert Buchanan
Cubic Spline Interpolation
50
x
Homework
Read Section 3.5
Exercises: 1, 3d, 5d, 7d, 31
J. Robert Buchanan
Cubic Spline Interpolation