Cubic Spline Questions
5. Find natural cubic splines which interpolate the following dataset of (x,y)
points: (3.0,2.5), (4.5,1.0), (7.0,2.5), (9.0,0.5); estimate the value for x=5.
Solution:
Step 1: Use the n-2 cubic spline equations to find the second derivatives y ′′ (slide 80):
x j − x j −1 x j +1 − x j −1 x j +1 − x j y j +1 − y j y j − y j −1
y ′j′−1 + y ′j′ + y ′j′+1 = −
6 3 6 x j +1 − x j x j − x j −1
4.5 − 3 .0 7. 0 − 3 .0 7. 0 − 4 .5 2. 5 − 1.0 1 .0 − 2. 5
j = 2: y 1′′ + y ′2′ + y 3′ = −
6 3 6 7. 0 − 4.5 4 .5 − 3. 0
1 4 5 8
y ′′ + y ′′ + y ′′ =
4 1 3 2 12 3 5
7. 0 − 4 .5 9. 0 − 4 .5 9.0 − 7. 0 0 .5 − 2 .5 2 .5 − 1 .0
j = 3: y ′2 + y ′3 + y ′4 = −
6 3 6 9. 0 − 7 .0 7. 0 − 4 .5
5 3 1 8
y 2′′ + y 3′′ + y ′4′ = −
12 2 3 5
Natural splines have y 1′′ = y ′4′ = 0 so the system simplifies to:
y 1′ = 0
4 5 8 y 1′′ = 0
y ′2 + y ′3′ =
3 12 5 y ′2′ = 1.6791
⇒
5 3 8 y ′3′ = −1 .5331
y ′2′ + y ′3′ = −
12 2 5 y ′4′ = 0
y ′4′ = 0
Step 2: Apply these solutions to obtain the cubic spline equations from formulae for A,
B, C, D (slides 75 & 77) for the three subintervals (i.e., with j = 1, 2, 3). For j = 1:
x2 − x 4. 5 − x 2
A= = =3− x
x 2 − x 1 4. 5 − 3. 0 3
2 2
B = 1 − A = 1 − (3 − x) = x − 2
3 3
1 1 2 2
C = ( A 3 − A)(x 2 − x 1) 2 = [(3 − x) 3 − (3 − x )]( 4. 5 − 3) 2
6 6 3 3
1 3 1 2 2
D= (B − B)( x 2 − x1) 2 = [( x − 2) 3 − ( x − 2)]( 4 .5 − 3) 2
6 6 3 3
The spline equation, which applied for the first subinterval 3.0 < x < 4.5, is then given
by:
y = Ay 1 + By 2 + Cy 1′′ + Dy ′2′
2 2 2 .25 2 2
= (3 − x)(2 .5) + ( x − 2)(1. 0) + 0 + [( x − 2) 3 − ( x − 2)](1 .6791)
3 3 6 3 3
= 0 .1866 (x − 3) 3 + 0. 2469 (x − 3) − 1. 6667 ( x − 4. 5) S1
For j = 2:
x3 − x 7. 0 − x 14 2
A= = = − x
x 3 − x 2 7. 0 − 4.5 5 5
14 2 2 9
B = 1 − A = 1 − ( − x) = x −
5 5 5 5
1 1 14 2 14 2
C = ( A 3 − A)(x 3 − x 2 )2 = [( − x )3 − ( − x)]( 7. 0 − 4 .5)2
6 6 5 5 5 5
1 3 1 2 9 2 9
D= (B − B)( x 3 − x 2 ) 2 = [( x − ) 3 − ( x − )]( 7.0 − 4 .5)2
6 6 5 5 5 5
The spline equation, which applied for the second subinterval 4.5 < x < 7.0, is then
given by:
y = Ay 2 + By 3 + C y 2′′ + Dy ′3′
14 2 2 9 6. 25 14 2 3 14 2
= ( − x)(1 .0) + ( x − )( 2.5) + [( − x) − ( − x)](1. 6791)
5 5 5 5 6 5 5 5 5
6 .25 2 9 2 9
+ [( x − ) 3 − ( x − )]( −1 .5331)
6 5 5 5 5
= −0 .1022 ( x − 4 .5) 3 − 0.1119 ( x − 7)3 + 1 .6388 (x - 4.5) + 0.2996(x - 7) S2
For j = 3:
x4 − x 9 .0 − x 9 1
A= = = − x
x 4 − x 3 9 .0 − 7 .0 2 2
9 1 1 7
B = 1 − A = 1 − ( − x) = x −
2 2 2 2
1 1 9 1 9 1
C = ( A 3 − A)( x 4 − x 3 ) 2 = [( − x )3 − ( − x )](9 .0 − 7. 0) 2
6 6 2 2 2 2
1 3 1 1 7 1 7
D= (B − B)( x 4 − x 3 ) 2 = [( x − )3 − ( x − )]( 9. 0 − 7. 0) 2
6 6 2 2 2 2
The spline equation, which applied for the third subinterval 7.0 < x < 9.0, is then given
by:
y = Ay 3 + By 4 + Cy 3′′ + Dy ′4′
9 1 1 7 2 9 1 9 1
= ( − x)(2 .5) + ( x − )( 0. 5) + [( − x )3 − ( − x )]( −1 .5331) + 0
2 2 2 2 3 2 2 2 2
= 0 .1278 ( x − 9) 3 + 0. 25(x − 7) − 1 .7610 (x − 9) S3
The three equations S1, S2 and S3 constitute the natural cubic spline equations for y
over the interval 3.0 < x < 9.0…
2.5
1.5
0.5
3 4 5 6 7 8 9
x
To estimate the y value at x = 5, use the spline equation for the second sub-interval
(S2):
y x =5 = −0. 1022 (x − 4.5) 3 − 0 .1119 ( x − 7 )3 + 1 .6388 (x - 4.5) + 0.2996(x - 7)
= − 0 .1022 (5 − 4 .5) 3 − 0. 1119 (5 − 7) 3 + 1. 6388 (5 - 4.5) + 0.2996(5 - 7)
= 1.1026
6. Find the interpolating cubic splines for the five logarithmic breakpoints (1,ln 1),
(2,ln 2), (3,ln 3), (4,ln 4), and (6,ln 6). Use [i.e. three exercises to do here]: (a)
natural endpoint conditions; (b) specified endslopes of 1 at x=1 and 1/6 at x=6;
and (c) not-a-knot conditions. Compare the graphs corresponding to the three
different endpoint conditions. For whichever of these endpoint conditions can be
used with the built -in Matlab function, compare your solution to that found with
Matlab.
Solution:
Step 1: Use the n-2 cubic spline equations to find the second derivatives y ′′ . We end
up with the following system of equations (denoted “System 1”):
x j − x j −1 x j +1 − x j −1 x j +1 − x j y j +1 − y j y j − y j −1
y ′j′−1 + y ′j′ + y ′j′+1 = −
6 3 6 x j +1 − x j x j − x j −1
2 −1 3 −1 3−2 ln 3 − ln 2 ln 2 − ln 1
j = 2: y ′1′ + y ′2′ + y ′3′ = −
6 3 6 3 −2 2 −1
1 2 1 3
y ′′ + y ′′ + y ′′ = ln
6 1 3 2 6 3 4
3−2 4−2 4 −3 ln 4 − ln 3 ln 3 − ln 2
j = 3: y ′2 + y 3′ + y ′4′ = −
6 3 6 4−3 3−2
1 2 1 8
y ′′ + y ′ + y ′′ = ln
6 2 3 3 6 4 9
4−3 6 −3 6−4 ln 6 − ln 4 ln 4 − ln 3
j=4: y ′3′ + y ′4′ + y ′5′ = −
6 3 6 6−4 4−3
1 1 3 6
y ′ + y ′4′ + y ′5′ = ln
6 3 3 8
Step 2: Find A, B, C, D (slides 75 & 77) for the four subintervals. For j = 1:
x2 − x 2−x
A1 = = = 2−x
x 2 − x1 2 −1
B1 = 1 − A1 = 1 − (2 − x) = x − 1
1 3 1
C1 = (A1 − A1)( x 2 − x 1) 2 = [( 2 − x )3 − (2 − x)]( 2 − 1) 2
6 6
1 1
D1 = (B13 − B1)(x 2 − x 1) 2 = [( x − 1) 3 − ( x − 1)]( 2 − 1) 2
6 6
For j = 2:
x3 − x 3−x
A2 = = =3−x
x3 − x 2 3 − 2
B2 = 1 − A2 = 1 − (3 − x) = x − 2
1 1
C2 = ( A23 − A2 )(x 3 − x 2 ) 2 = [(3 − x )3 − (3 − x )](3 − 2) 2
6 6
1 1
D 2 = (B 32 − B2 )( x 3 − x 2 ) 2 = [( x − 2) 3 − (x − 2)](3 − 2) 2
6 6
For j = 3:
x4 − x 4−x
A3 = = = 4−x
x4 − x3 4 − 3
B3 = 1 − A3 = 1 − (4 − x ) = x − 3
1 1
C3 = ( A33 − A3 )( x 4 − x 3 )2 = [( 4 − x) 3 − ( 4 − x)]( 4 − 3) 2
6 6
1 3 1
D3 = (B3 − B3 )( x 4 − x 3 ) 2 = [( x − 3) 3 − ( x − 3)]( 4 − 3) 2
6 6
For j = 4:
x5 − x 6−x 1
A4 = = =3− x
x5 − x 4 6 − 4 2
1 1
B 4 = 1 − A4 = 1 − (3 − x) = x − 2
2 2
1 3 1 1 1
C4 = ( A 4 − A 4 )( x 5 − x 4 ) 2 = [(3 − x )3 − (3 − x)]( 6 − 4) 2
6 6 2 2
1 3 1 1 1
D4 = (B 4 − B 4 )( x 5 − x 4 )2 = [( x − 2) 3 − ( x − 2)](6 − 4) 2
6 6 2 2
(a) Using natural endpoint conditions…
Natural splines have y 1′′ = y 5′ = 0 so “System 1” simplifies to:
y 1′′ = 0
2 1 3 y ′1′ = 0
y ′′ + y ′3′ = ln
3 2 6 4 y ′2′ = −0 .4182
1 2 1 8
y ′2′ + y ′3′ + y ′4 = ln ⇒ y ′3′ = −0. 05310
6 3 6 9
y ′4′ = −0 .07610
1 3 6
y ′3′ + y 4′ = ln y ′5′ = 0
6 8
y 5′ = 0
The spline equation, which applied for the first subinterval 1 < x < 2, is then given by:
y = A1y 1 + B1y 2 + C1y 1′′ + D1y ′2′
1
= ( 2 − x )(ln 1) + ( x − 1)(ln 2) + 0 + [( x − 1) 3 − ( x − 1)]( −0 .4182 )
6
= −0 .0697 (x − 1) 3 + 0.7628 (x − 1) S1
The spline equation, which applied for the second subinterval 2 < x < 3, is then given
by:
y = A2 y 2 + B2 y 3 + C 2 y ′2′ + D 2 y 3′
1 1
= (3 − x)(ln 2) + (x − 2)(ln 3) +[(3 − x) 3 − (3 − x)]( −0 .4182 ) + [( x − 2) 3 − (x − 2)]( −0.05310 )
6 6
3 3
= −0 .00885 (x − 2) + 0.0697 ( x − 3) + 1 .1075 (x - 2) − 0.7628(x - 3) S2
The spline equation, which applied for the third subinterval 3 < x < 4, is then given by:
y = A3 y 3 + B3 y 4 + C 3 y ′3′ + D3 y ′4
1 1
= ( 4 − x)(ln 3) + ( x − 3)(ln 4) + [( 4 − x )3 − (4 − x )]( −0 .05310 ) + [( x − 3) 3 − ( x − 3)]( −0. 07610 )
6 6
= −0 .01268 ( x − 3)3 + 0 .00885 (x − 4) 3 + 1. 3990 ( x − 3) − 1 .1075 ( x − 4) S3
The spline equation, which applied for the fourth subinterval 4 < x < 6, is then given
by:
y = A4 y 4 + B 4 y 5 + C 4 y ′4′ + D 4 y ′5′
1 1 2 1 1
= (3 − x)(ln 4 ) + ( x − 2)(ln 6) + [(3 − x) 3 − (3 − x )]( −0.07610 ) + 0
2 2 3 2 2
3
= 0 .006342 (x − 6) + 0 .8959 ( x − 4) − 0.7185 ( x − 6) S4
(b) Using specified endslopes of 1 at x = 1 and 1/6 at x = 6 (NOTE: There was a typo on
the Web, the endslope at x = 1 is not zero!)…
We need to set the values of y 1′′ & y 5′ to values calculated from equation [2] from slide
78 in the notes so that y ′1 & y 5′ have the desired values. For the left endpoint, use x =
x j. For the right endpoint, use x = x j+1 .
dy y j +1 − y j 3 A 2 − 1 3B 2 − 1
= − ( x j +1 − x j )y ′j′ + (x j +1 − x j )y ′j′+1
dx x j +1 − x j 6 6
x j +1 − x
where A = and B = 1 − A
x j +1 − x j
ln 2 − ln 1 3(1) 2 − 1 3(0) 2 − 1
1= − (2 − 1)y 1′′ + ( 2 − 1)y ′2′
dy 2 −1 6 6
At x = 1, = 1:
dx 1 1
1 = ln 2 − y 1′′ − y ′2
3 6
1 ln 6 − ln 4 3(0) 2 − 1 3(1) 2 − 1
= − (6 − 4 )y ′4′ + (6 − 4) y ′5′
dy 1 6 6−4 6 6
At x = 6, = :
dx 6 1 1 3 1 2
= ln + y ′4′ + y ′5′
6 2 2 3 3
With these conditions, “System 1” simplifies to:
1 1
y ′1′ + y ′2′ = ln 2 − 1
3 6
1 2 1 3 y ′1′ = −0 .8216
y 1′ + y 2′ + y 3′′ = ln
6 3 6 4 y ′2′ = −0 .1979
1 2 1 8
y ′2′ + y ′3′ + y ′4′ = ln ⇒ y ′3′ = −0. 1128
6 3 6 9
y ′4′ = −0 .05775
1 1 3 6
y 3′′ + y 4′ + y 5′ = ln y ′5′ = −0. 02522
6 3 8
1 2 1 3
y ′′ + y ′ = − ln
3 4 3 5 6 2
The spline equation, which applied for the first subinterval 1 < x < 2, is then given by:
y = A1y 1 + B1y 2 + C1y 1′′ + D1y ′2′
1 1
= ( 2 − x )(ln 1) + ( x − 1)(ln 2) + [(2 − x) 3 − (2 − x )]( −0.8216 ) + [( x − 1) 3 − ( x − 1)]( −0 .1979 )
6 6
3 3
= −0.03298 ( x − 1) + 0. 1369( x − 2) + 0 .7261( x − 1) − 0.1369 ( x − 2) S1
The spline equation, which applied for the second subinterval 2 < x < 3, is then given
by:
y = A2 y 2 + B2 y 3 + C 2 y ′2′ + D 2 y ′3′
1 1
= (3 − x)(ln 2) + ( x − 2)(ln 3) + [( 3 − x )3 − (3 − x )]( −0 .1979 ) + [( x − 2)3 − ( x − 2)]( −0 .1128 )
6 6
= −0.0188 (x − 2) 3 + 0 .03298 ( x − 3) 3 + 1 .1174 (x - 2) − 0. 7261(x - 3) S2
The spline equation, which applied for the third subinterval 3 < x < 4, is then given by:
y = A3 y 3 + B3 y 4 + C 3 y ′3′ + D 3 y ′4
1 1
[( 4 − x) 3 − (4 − x)]( −0 .1128 ) + [( x − 3) 3 − ( x − 3)]( −0.05775 )
= ( 4 − x)(ln 3) + ( x − 3)(ln 4 ) +
6 6
3 3
= −0 .009625 ( x − 3) + 0 .0188 ( x − 4) + 1 .3959 (x − 3) − 1 .1174 ( x − 4) S3
The spline equation, which applied for the fourth subinterval 4 < x < 6, is then given
by:
y = A4 y 4 + B 4 y 5 + C 4 y ′4′ + D 4 y 5′′
1 1 2 1 1
= (3 − x)(ln 4 ) + ( x − 2)(ln 6) + [(3 − x )3 − (3 − x )]( −0 .05775 )
2 2 3 2 2
2 1 3 1
+ [( x − 2) − ( x − 2)]( −0 .02522 )
3 2 2
= −0 .002102 ( x − 4) + 0. 004813 (x − 6) 3 + 0 .9043 ( x − 4) − 0 .7124 (x − 6)
3
S4
(c) Using not-a-knot conditions…
We wish to enforce the continuity of the third derivative at the first and last internal
knots (i.e., j = 2 and j = 4). To begin, we take the derivative of the second derivative
(found on slide 79):
d 3y d y ′j+1 − y ′j′
3
= ( Ay ′j′ + By ′j′+1) =
dx dx x j+1 − x j
Continuity at these internal knots mean:
y ′2 − y ′1′ y 3′ − y ′2′ y 2′ − y ′1′ y ′3 − y ′2′
At j = 2: = ⇒ = ⇒ y ′1′ − 2 y ′2′ + y ′3′ = 0
x 2 − x1 x3 − x 2 2−1 3−2
y ′4′ − y ′3 y 5′ − y ′4′ y 4′′ − y ′3′ y ′5′ − y ′4′ 3 1
At j = 4: = ⇒ = ⇒ y ′3′ − y ′4 + y ′5′ = 0
x 4 − x 3 x5 − x 4 4 −3 6−4 2 2
With these conditions, “System 1” simplifies to:
y 1′′ − 2 y ′2′ + y ′3′ =0
1 2 1 3 y ′1′ = −0 .4868
y 1′ + y ′2′ + y ′3′ = ln
6 3 6 4 y ′2′ = −0 .2877
1 2 1 8
y ′′ + y ′′ + y ′ = ln ⇒ y ′3′ = −0. 08860
6 2 3 3 6 4 9
y ′4′ = −0 .06462
1 1 3 6
y ′3′ + y ′4 + y ′5′ = ln y ′5′ = −0. 01668
6 3 8
3 1
y ′3′ − y ′4′ + y ′5′ =0
2 2
The spline equation, which applied for the first subinterval 1 < x < 2, is then given by:
y = A1y 1 + B1y 2 + C1y 1′′ + D1y ′2′
1 1
[(2 − x) 3 − ( 2 − x )]( −0 .4868 ) + [( x − 1)3 − (x − 1)]( −0. 2877 )
= ( 2 − x )(ln 1) + ( x − 1)(ln 2) +
6 6
= −0.04795 (x − 1) 3 + 0 .08113 ( x − 2)3 + 0 .7411( x − 1) − 0 .08113 ( x − 2) S1
The spline equation, which applied for the second subinterval 2 < x < 3, is then given
by:
y = A2 y 2 + B2 y 3 + C 2 y ′2′ + D 2 y ′3′
1 1
= (3 − x)(ln 2) + ( x − 2)(ln 3) + [( 3 − x )3 − (3 − x )]( −0 .2877 ) + [( x − 2) 3 − (x − 2)]( −0. 0886 )
6 6
3 3
= −0.01477 (x − 2) + 0 .04795 (x − 3) + 1 .1134 (x - 2) − 0.7411(x - 3) S2
The spline equation, which applied for the third subinterval 3 < x < 4, is then given by:
y = A3 y 3 + B3 y 4 + C 3 y ′3′ + D3 y ′4
1 1
= ( 4 − x)(ln 3) + ( x − 3)(ln 4) +[( 4 − x) 3 − (4 − x)]( −0 .0886 ) + [( x − 3) 3 − (x − 3)]( −0 .06462 )
6 6
= −0 .01077 ( x − 3) 3 + 0. 01477 (x − 4) 3 + 1. 3971(x − 3) − 1 .1134 ( x − 4) S3
The spline equation, which applied for the fourth subinterval 4 < x < 6, is then given
by:
y = A4 y 4 + B 4 y 5 + C 4 y ′4 + D 4 y 5′′
1 1 2 1 1
= (3 − x)(ln 4) + ( x − 2)(ln 6) + [(3 − x )3 − (3 − x)]( −0 .06462 )
2 2 3 2 2
2 1 1
+ [( x − 2) 3 − ( x − 2)]( −0 .01668 )
3 2 2
= −0 .00139 (x − 4 ) + 0 .005385 (x − 6) 3 + 0 .9014 (x − 4) − 0 .7147 (x − 6)
3
S4
Please note that with this last case, since we are using the not-a-knot conditions, S1
and S2 are in fact the same cubic function, as is the case with S3 and S4. Therefore, if
you are confident in your results, you actually only need to find two spline equations
for this question when using the not-a-knot conditions! J
In each case, the four equations S1, S2, S3 and S4 constitute the natural cubic spline
equations for y over the interval 1 < x < 6.
The following are the plots of these splines in Matlab. The built -in spline function in
Matlab is capable of finding splines using the fixed endslopes condition and the not-a-
knot condition.
Cublic spline using natural endpoints
1.8
1.6
1.4
1.2
y
0.8
0.6
0.4
0.2
0
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6
x
Cublic spline using fixed endslopes
1.8
1.6
1.4
1.2
y
0.8
0.6
0.4 By hand (dotted)
Matlab spline function (solid)
0.2
0
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6
x
Cublic spline using not-a-knot conditions
1.8
1.6
1.4
1.2
y
0.8
0.6
0.4 By hand (dotted)
Matlab spline function (solid)
0.2
0
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6
x