Cubic Spline
Cubic Spline
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.
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
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
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
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
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
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
1.000000000000000 0 0 0
0.300000000000000 1.800000000000000 0.600000000000000 -1.491798830421179
0 0 1.000000000000000 0
[av bv cv dv]:
[av bv cv dv]
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
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
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. )
11