You are on page 1of 11

3.

4 - Cubic Spline Interpolation


Cubic Spline Approximation:
Problem: Given n 1 pairs of data points x i , y i , i 0, 1, . . . , n, find a piecewise-cubic polynomial
Sx
S 0 x a 0 b 0 x x i c 0 x x 0 2 d 0 x x 0 3 if x 0 x x 1
S 1 x a 1 b 1 x x 1 c 1 x x 1 d 1 x x 1
2 3
if x 1 x x 2
Sx
: :
S n1 x a n1 b n1 x x n1 c n1 x x n1 2 d n1 x x n1 3 if x n1 x x n
and Sx i y i , i 0, 1, . . . , n.
Cubic Splines:
S i x a i b i x x i c i x x i 2 d i x x i 3 , i 0, 1, . . . , n 1.
Conditions:
(1) S i x i y i , i 0, 1, 2, . . , n - interpolating data x i , y i
(2) S i x i1 S i1 x i1 , i 0, 1, . . . , n 2 continuity at interior points

(3) S i x i1 S i1 x i1 , i 0, 1, . . . , n 2 - continuous slope at interior points

(4) S i x i1 S i1 x i1 , i 0, 1, . . . , n 2 - continuous curvature at interior points

(5) S 0 x 0 0, and S n1 x n 0 free spline or natural spline

S 0 x 0 and S n1 x n - clamped spline
Totally we have 4n unknowns: a i , b i , c i , and d i , i 0, 1, . . . , n 1 and there are n 1 equation in
(1), n 1 equations in (2), n 1 equations in (3) n 1 conditions in (4) and 2 equations in (5). So, again we
can solve a i , b i , c i and d i uniquely.
From (1): S i x i y i , we have
S i x i a i y i , i 0, 1, 2, . . , n 1,
and S n1 x n y n , we have an equation in b n1 , c n1 and d n1 :
y n1 b n1 x n x n1 c n1 x n x n1 2 d n1 x n x n1 3 y n . (1.1)
From (2): S i x i1 S i1 x i1 , i 0, 1, . . . , n 2, we have n 1 equations in b i , c i and d i ,
y i b i x i1 x i c i x i1 x i 2 d i x i1 x i 3 y i1 , i 0, 1, . . . , n 2. (2.1)
S i x b i 2c i x x i 3d i x x i 2 . From (3): S i x i1 S i1 x i1 , i 0, 1, . . . , n 2, we have
n 1 equations in b i , c i and d i :
b i c i x i1 x i 3d i x i1 x i 2 b i1 , i 0, 1, . . . , n 2. (3.1)

S i x 6d i x x i 2c i . From (4): S i x i1 S i1 x i1 , i 0, 1, . . . , n 2, we have n 1 equations
in c i and d i :
2c i 6d i x i1 x i 2c i1 , i 0, 1, . . . , n 2. (4.1)

For a free spline or natural spline, from conditions: S 0 x 0 0, and S n1 x n 0, we have an
equation in c 0 and an equation in c n1 and d n1 :
2c 0 0, and 6d n1 x n x n1 2c n1 0. (5.1)

For a clamped spline, from conditions: S 0 x 0 and S n x n , we have a condition in b 0 and an
equation in b n1 , c n1 and d n1 :
b 0 0, and b n1 2c n1 x n x n1 3d n1 x n x n1 2 0. (5.2)
To solve a i , b i , c i and d i from equations in (1.1)-(5.1), we first let
h i x i1 x i , i 0, 1, . . . , n 1.

1
c0
y0 b0 d0
c1
y1 b1 d1

a ,
b , c : , c n 0,
d
: : :
c n1
y n1 b n1 d n1
cn
For a free spline or natural spline :
(I) Set
1 0 0 0 0 ... 0 0 0
h 0 2h 0 h 1 h1 0 0 ... 0 0 0
0 h1 2h 1 h 2 h 2 0 . . . 0 0 0
A
: : : : : ... 0 : :
0 0 0 0 0 . . . h n2 2h n2 h n1 h n1
0 0 0 0 0 ... 0 0 1
n1n1

0
3 1 y 2 y 1 1 y y 0
h1 h0 1
3 1 y 3 y 2 1 y y 1
v h2 h1 2
:
3 1 y n y n1 1 y n1 y n2
h n1 h n2
0
n11

Solve Ac v for c.
(II) Evaluate:
b i 1 a i1 a i h i 2c i c i1 , i 0, 1, . . . , n 1.
hi 3
di 1 c i1 c i , i 0, 1, . . . , n 1.
3h i
For a clamped spline:
(I) Set
2h 0 h0 0 0 0 ... 0 0 0
h0 2h 0 h 1 h1 0 0 ... 0 0 0
0 h1 2h 1 h 2 h 2 0 . . . 0 0 0
A
: : : : : ... 0 : :
0 0 0 0 0 . . . h n2 2h n2 h n1 h n1
0 0 0 0 0 ... 0 h n1 2h n1
n1n1

2
3 1 y 1 y 0
h0
3 1 y 2 y 1 1 y 1 y 0
h1 h0
3 1 y 3 y 2 1 y 2 y 1
v h2 h1
:
3 1 y n y n1 1 y n1 y n2
h n1 h n2
3 1 y n y n1
h n1
Solve Ac v for c.
(II) Same as for the Free Spline.

Example Let fx x 1 . Given 0, 1, 3, 2, 8, 3, construct a free cubic spline and a clamped


cubic spline.
1. Free cubic spline:
(I) Set up the 3 3 matrix A and the 3 1 vector v : h 0 3, h 1 5
1 0 0 1 0 0
A 3 23 5 5 3 16 5
0 0 1 0 0 1

0 0
v 3 1
5
3 2 1
3
2 1 25
0 0
Solve the vector c :
1
1 0 0 0 0
A c v, c A 1v 3 16 5 25 401
0 0 1 0 0

(II) Compute the vectors


b and
d:
0
1

a , c 401
2
0
1
2 1 3
20 401 43

b 3 3
120
1
5
3 2 5
3
2 401 0 17
60

1
401 0 360
1

d
33

1
35
0 401 1
600

3
S 0 x 1 43
120
x 0 360
1
x 3 for 0 x 3
Sx
S 1 x 2 17
60
x 3 401 x 3 2 1
600
x 3 3 for 3 x 8

2. Clamped cubic spline f x 1
2
x 1 1/2 , f 0 1
2
, f 8 1
2
1
1
6
9
(I) Set up the 3 3 matrix A and the 3 1 vector v : h 0 3, h 1 5
23 3 0 6 3 0
A 3 23 5 5 3 16 5
0 5 25 0 5 10

3 1 2 1 12 12
3
v 3 1 3 2 1 2 1 25
5 3
3 16 1 3 2 101
5
Solve the vector c :
1
6 3 0 12 240
19

A c v, c A 1v 3 16 5 25 120
1

0 5 10 101 1200
7

(II) Compute the vectors


b and
d : 0, 1, 3, 2, 8, 3,
b i 1 a i1 a i h i 2c i c i1 , i 0, 1, . . . , n 1.
hi 3
d i 1 c i1 c i , i 0, 1, . . . , n 1.
3h i
240
19

1

a , c 120
1
2
1200
7

1
2 1 3
2 240
19
120
1 1

b 3 3
2
1
5
3 2 5
3
2 120
1
1200
7 19
80

1
120
1
240
19 17

d
33
2160
1
35
1200
7
120
1 1
6000

S 0 x 1 12 x 19
240
x2 17
2160
x 3 for 0 x 3
Sx
S 1 x 2 19
80
x 3 120
1
x 3 2 1
6000
x 3 3 for 3 x 8

4
3.0 3.0
y y

2.5 2.5

2.0 2.0

1.5 1.5

1.0 1.0
0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8
x x

free cubic spline clamped cubic spline

MatLab programs: cbspfun.m and cbspclamp.m generate a i , b i , c i and d i (av,bv,cv,dv) with input x i , y i
(xv,yv). Now we use these two MatLab program for the above example.
Free cubic spline:
xv[0;3;8]; yv[1;2;3];
[av,bv,cv,dv]cbspfun(xv,yv);
[A|r], where Acr for solving c_i:

1.000000000000000 0 0 0
3.000000000000000 16.000000000000000 5.000000000000000 -0.400000000000000
0 0 1.000000000000000 0

[av bv cv dv]
1.000000000000000 0.358333333333333 0 -0.002777777777778
2.000000000000000 0.283333333333333 -0.025000000000000 0.001666666666667
Clamped cubic spline:
xv[0;3;8]; yv[1;2;3];
[av,bv,cv,dv]cbspcl(xv,yv,1/2,1/6);
Matrix A and Vector b:
6.000000000000000 3.000000000000000 0 -0.500000000000000
3.000000000000000 16.000000000000000 5.000000000000000 -0.400000000000000
0 5.000000000000000 10.000000000000000 -0.100000000000000

[av bv cv dv]
1.000000000000000 0.500000000000000 -0.079166666666667 0.007870370370370
2.000000000000000 0.237500000000000 -0.008333333333333 0.000166666666667

5
Example Let fx cosx 2 , x 0 0, x 1 0. 6, and x 2 0. 9. Find a free cubic spline and a
clamped cubic spline.
1. Free cubic spline:
(I) Set up the 3 3 matrix A and the 3 1 vector v : h 0 0. 6, h 1 0. 3
1 0 0 1 0 0
A 0. 6 20. 6 0. 3 0. 3 0. 6 1. 8 0. 3
0 0 1 0 0 1

0 0
v 3 1
0.3
cos0. 81 cos0. 36 1
0.6
cos0. 36 1 2. 143 468
0 0

Solve the vector c :


1
1 0 0 0 0
A c v, c A 1v 0. 6 1. 8 0. 3 2. 143 468 1. 190 82
0 0 1 0 0

(II) Compute the vectors


b and
d:
0
cos0

a , c 1. 190 82
cos0. 6 2
0
1
cos0. 6 2 1 0.6
20 1. 190 82 0. 131 325

b 0.6 3

1
0.3
cos0. 9 2 cos0. 6 2 0.3
3
21. 190 82 0 0. 583 164
1
1. 190 82 0 0. 661 57

d
30.6

1
30.3
0 1. 190 82 1. 323 13

S 0 x 1 0. 131 325x 0 0. 661 57x 3 for 0 x 0. 6


Sx S 1 x cos0. 6 2 0. 583 164x 0. 6 1. 190 82x 0. 6 2 1. 323 13x 0. 6 3
for 0. 6 x 0. 9
2. Clamped cubic spline:

f x 2x sinx 2 , f 0 0 , f 0. 9 20. 9 sin0. 9 2 1. 303 71
(I) Set up the 3 3 matrix A and the 3 1 vector v : h 0 0. 6, h 1 0. 3

6
20. 6 0. 6 0 1. 2 0. 6 0
A 0. 6 20. 6 0. 3 0. 3 0. 6 1. 8 0. 3
0 0. 3 20. 3 0 0. 3 0. 6

3 1
0.6
cos0. 6 2 1 0 0. 320 52
v 3 1
0.3
cos0. 9 2 cos0. 6 2 1
0.6
cos0. 6 2 cos0 2. 143 5
3 1. 303 71 1
0.3
cos0. 9 2 cos0. 6 2 1. 447 14

Solve the vector c :


1
1. 2 0. 6 0 0. 320 52 0. 199 44
A c v, c A 1v 0. 6 1. 8 0. 3 2. 143 5 0. 933 09
0 0. 3 0. 6 1. 447 14 1. 945 36

(II) Compute the vectors


b and
d:
0. 199 44
cos0

a , c 0. 933 09
cos0. 6 2
1. 945 36
1
cos0. 6 2 1 0.6
20. 199 44 0. 933 09 0. 0000033728

b 0.6 3

1
0.3
cos0. 9 2 cos0. 6 2 0.3
3
20. 933 09 1. 945 36 0. 44017
1
0. 933 09 0. 199 44 0. 629 183

d
30.6

1
30.3
1. 945 36 0. 933 09 1. 124 74

S 0 x 1 0. 0000033728x 0. 199 44x 2 0. 629 183 x 3 for 0 x 0. 6


Sx S 1 x cos0. 6 2 0. 44017 x 0. 6 0. 933 09 x 0. 6 2 1. 124 74x 0. 6 3
for 0. 6 x 0. 9

y 1.0 y 1.0

0.9 0.9

0.8 0.8

0.7 0.7
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
x x

Sx - a free spline Sx - a clamped spline


7
Use MatLab programs: cbspfun.m and cbspcl.m, for above example
Free spline:
xv[0;0.3;0.9];
yvcos(xv.^2);
[av,bv,cv,dv]cbspfun(xv,yv);
[A|r], where Acr for solving c_i:

1.000000000000000 0 0 0
0.300000000000000 1.800000000000000 0.600000000000000 -1.491798830421179
0 0 1.000000000000000 0

[av bv cv dv]:

1.000000000000000 0.069386822841158 0 -0.920863475568629


0.995952733011994 -0.179246315562372 -0.828777128011766 0.460431737784314
Clamped spline:
[av,bv,cv,dv]cbspcl(xv,yv,0,-1.30371);
Matrix A and Vector b

0.600000000000000 0.300000000000000 0 -0.040472669880057


0.300000000000000 1.800000000000000 0.600000000000000 -1.491798830421179
0 0.600000000000000 1.200000000000000 -2.378858499698763

[av bv cv dv]

1.000000000000000 -0.000000000000000 0.037039344878338 -0.273363260261337


0.995952733011994 -0.051584473343558 -0.208987589356866 -0.927167055026113

Applications of Spline Interpolations:


When fx i y i , i 0, 1, . . . , n, we can use Sx to approximate fx :
a. fx Sx;

b. f x S x;
b b
c. fxdx Sxdx.
a a
Approximation error:
Let f 4 be continuous on a, b with max axb |f 4 x| M. If S is the unique clamped cubic spline
interpolant to f with respect to the nodes a x 0 x 1 . . . x n b, then
max fx Sx 5M max x j1 x j 4
axb 384 0 j n1
The free spline will generally give less accurate results than the clamped conditions near the ends of the

interval x 0 , x n unless the function f happens to nearly satisfy f x 0 f x n 0.

Example Flying ruddy duck: To approximate the top profile of the duck, we have chosen points along
the curve through which we want the approximating curve to pass. Notice that more points
are used when the curve is changing rapidly than when it is changing slowly.
Free Spline vs Lagrangian Interpolating Polynomial:

8
4
Natural Cubic Spline
3

0
0 2 4 6 8 10 12 14

4
Interpolating Polynomial using the Neweton Divided Difference Formula
3

0
0 2 4 6 8 10 12 14

Free Spline vs Linear Spline:


4
Natural Cubic Spline
3

0
0 2 4 6 8 10 12 14

4
linear spline
3

0
0 2 4 6 8 10 12 14

S 0 x 1 Bx 2x 2 2x 3 if 0 x 1
Example Let Sx . Suppose that
S 1 x 1 bx 1 4x 1 2 7x 1 3 if 1 x 2

Sx interpolates fx at x 0 0, x 1 1 and x 2 2 by a clamped cubic spline. Find f 0 and

f 2.

9

f 0 S 0 0 B

f 2 S 1 2 b 8x 1 21x 1| x2 b 8 21 b 13
Find B and b. By the conditions for a clamped cubic spline:
S 0 1 S 1 1 1 B 2 2 1 B 1, B 0.

S 0 1 S 1 1 S 0 1 B 4x 6x 2 | x1 4 6 2 S 1 1 b, b 2

Therefore, f 0 0 and f 2 2 13 11.

Example Let fx sine x 2 and S be the clamped cubic spline which interpolates fx at
0, 0. 8415 , 0. 2, 0. 7032 , 0. 5, 0. 3441 , 0. 8, 0. 2236 , 1, 0. 6581

(1) Estimate the approximation error for fx Sx for x in 0, 1 .


1 1
(2) Estimate the approximation error for fxdx Sxdx
0 0

f x cose x 2e x , f x sine x 2 cose x 2e x

f x cose x 2e 3x 3 sine x 2e 2x cose x 2e x
4
f x sine x 2e 4x 6cose x 2e 3x 7sine x 2e 2x cose x 2e x

y 80

60

4
40
f 4 x f 1 86. 8 87
20

0
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x

y |f 4
x|, x in 0, 1
587 587
Error fx Sx max h 4 0. 3 4 0. 009 18
384 0 j 4 j 384
1 1 1
Error 0 fxdx 0 Sxdx 0 fx Sx dx 1 0
587
max h 4 0. 009 18
384 0 j 4 j

Exercises:
i 0 1 2 3
1. Given the following data: xi 0 0. 25 0. 5 1
y i fx i 1 1. 4 1. 6 2
a. Consider the problem of constructing a free cubic spline Sx. Form the matrix A and vector v
which are used to solve the vector c containing all coefficients c i s. (Do not solve c. )

b. Consider the problem of constructing a clamped cubic spline Sx if we know f 0 1 and
10

f 1 1. 2. Form the matrix A and vector v which are used to solve the vector c containing all
coefficients c i s. (Do not solve c. )

2. Given a cubic spline interpolation:


S 0 x 1 2x x 3 if 0 x 1
Sx ,
S 1 x 2 bx 1 cx 1 2 dx 1 3 if 1 x 2
determine constants b, c, and d so that all conditions for a natural cubic spline hold.

3. Given a cubic spline interpolation:


S 0 x 3x 1 2x 1 2 x 1 3 if 1 x 2
Sx
S 1 x a bx 2 cx 2 2 dx 2 3 if 2 x 3

and f 1 f 3, determine constants a, b, c, and d so that all conditions for a clamped cubic spline
hold.

4. Given x i , y i 0, 1, 1, 2, 2, 1 where y i fx i for some fx, construct a free cubic spline


Sx that agrees with fx at x 0 , x 1 and x 2 (with or without using the MatLab program cbspfun.m).
a. Approximate f 12 by S 12 .

b. Approximate f 1 by S 1 and f 32 by S 32 .
1 1
c. Extra points: Approximate fxdx by Sxdx.
0 0

5. The clamped cubic spline Sx is constructed which agrees with the


function fx x cosx 2x 2 3x 1 at data points
x i , y i : 0. 1, 0. 6205, 0. 2, 0. 2840, 0. 3, 0. 0066, 0. 4, 0. 2484
and satisfies conditions: S 0. 1 3. 585, and S 0. 4 2. 1653. Find an upper bound as small as
possible for the approximation error when fx is approximated by Sx for x in 0. 1, 0. 4. (Dont
compute Sx. )

6. Suppose we are given the following data:


i 0 1 2 3 4
xi 0 0. 5 1. 0 1. 5 2. 0
y i fx i 0. 5 1. 425639 2. 640859 4. 009155 5. 305472

f x i 1. 5 2. 305472
a. Use the MatLab program cbspfun.m to construct a free spline.
b. Approximate f0. 25 by S0. 25 and approximate f 1. 25 by S1. 25.
c. Use the MatLab program cbspcl.m to construct a clamped spline.
d. Approximate f0. 25 by S0. 25 and approximate f 1. 25 by S1. 25.
e. Suppose we know fx x 1 2 0. 5e x . Compute the true errors to determine with spline gives a
better approximation at each point.

11

You might also like