Numerical Methods
Chapter 2: Interpolation and Approximation
2.3 Cubic Spline Interpolation
It is another way to fit a smooth polynomial to a given set of data. A cubic (3rd-degree) spline fits a
"smooth curve" through all the given points. Higher order polynomials also fit smooth curve through
the given points, but if the data set of points does not belong to a polynomial or if the given number of
points are very high, the polynomial technique becomes inappropriate. For example, if we try to
approximate the function f(x) = cos10x in the interval [-2, 2] by polynomial technique, we find that the
approximation is very different than the reality. Similarly, if the number of points is, say, 501, then the
polynomial will be of degree 500.
Degree Spline Nature of function
1 Straight spline Continuous in function, but discontinuous in slope & curvature
2 Quadratic Continuous in function and slope but discontinuous in curvature
spline
3 Cubic Spline Continuous in function, slope & curvature
In cubic spline, we write the equation for a cubic in the ith
interval which lies between the points (xi,yi) and (xi+1, yi+1).
The cubic spline equation is of the form:
𝑔 𝑥 𝑎 𝑥 𝑥 𝑏 𝑥 𝑥 𝑐 𝑥 𝑥 𝑑 1
Thus the cubic spline function we want is of the form:
𝑔 𝑥 𝑔 𝑥 𝑜𝑛 𝑡ℎ𝑒 𝑖𝑛𝑡𝑒𝑟𝑣𝑎𝑙 𝑥 , 𝑥 , 𝑓𝑜𝑟 𝑖 0, 1, 2, … , 𝑛 1
The cubic spline meets the following conditions:
𝑎 𝑔 𝑥 𝑦 . .. 2 𝑝𝑎𝑠𝑠𝑒𝑠 𝑡ℎ𝑟𝑜𝑢𝑔ℎ 𝑎𝑙𝑙 𝑑𝑎𝑡𝑎 𝑝𝑜𝑖𝑛𝑡𝑠.
𝑏 𝑔 𝑥 𝑦 𝑥 . .. 3 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑖𝑠 𝑐𝑜𝑛𝑡𝑖𝑛𝑢𝑜𝑢𝑠
𝑐 𝑔 𝑥 𝑔′ 𝑥 . .. 4 𝑠𝑙𝑜𝑝𝑒 𝑖𝑠 𝑐𝑜𝑛𝑡𝑖𝑛𝑢𝑜𝑢𝑠
𝑑 𝑔 𝑥 𝑔′′ 𝑥 . .. 5 𝑐𝑢𝑟𝑣𝑎𝑡𝑢𝑟𝑒 𝑖𝑠 𝑐𝑜𝑛𝑡𝑖𝑛𝑢𝑜𝑢𝑠
For n +1 points, there are n intervals. Therefore, there are n 4 unknowns which are
[ ai, bi, ci, and di ]
From (1) when x = xi, then di = yi ,
Compiled By: Rupesh D Shrestha Nepal Engineering College
Numerical Methods
Chapter 2: Interpolation and Approximation
Then from (1) and (3), when x = xi+1
𝑔 𝑥 𝑦 𝑎 𝑥 𝑥 𝑏 𝑥 𝑥 𝑐 𝑥 𝑥 𝑑 𝑎ℎ 𝑏ℎ 𝑐ℎ 𝑦 … 6
where ℎ 𝑥 𝑥 is the width of ith interval.
To relate slope and curvature of the joining splines, from (1) we get,
𝑔 𝑥 3𝑎 𝑥 𝑥 2𝑏 𝑥 𝑥 𝑐 7
𝑔 𝑥 6𝑎 𝑥 𝑥 2𝑏 8 2 𝑑𝑒𝑟𝑖𝑣𝑎𝑡𝑖𝑣𝑒 𝑜𝑓 𝑎 𝑐𝑢𝑏𝑖𝑐 𝑖𝑠 𝑙𝑖𝑛𝑒𝑎𝑟
𝐿𝑒𝑡 𝑆 𝑔 𝑥 𝑎𝑛𝑑 𝑆 𝑔 𝑥 , 𝑡ℎ𝑒𝑛
𝑺𝒊
𝑆 6𝑎 𝑥 𝑥 2𝑏 𝒃𝒊
𝟐
𝑺𝒊 𝟏 𝑺𝒊
𝑆 6𝑎 𝑥 𝑥 2𝑏 6𝑎 ℎ 𝑆 𝒂𝒊
𝟔𝒉𝒊
And from (6),
𝒚𝒊 𝟏 𝒚𝒊 𝟐𝒉𝒊 𝑺𝒊 𝒉𝒊 𝑺𝒊 𝟏 𝟐𝒉𝒊 𝑺𝒊 𝒉𝒊 𝑺𝒊 𝟏
𝒄𝒊 𝒇 𝒙𝒊 , 𝒙𝒊 𝟏
𝒉𝒊 𝟔 𝟔
Invoking the condition that the slope of the two cubic that join at (xi, yi) are the same, we obtain,
from (7), with x = xi,
𝑦 3𝑎 𝑥 𝑥 2𝑏 𝑥 𝑥 𝑐 𝑐
In the previous interval, from xi-1 to xi, the slope at its right end will be
𝑦 3𝑎 𝑥 𝑥 2𝑏 𝑥 𝑥 𝑐 3𝑎 ℎ 2𝑏 ℎ 𝑐
Equating these, and substituting for a, b, c, d their relationship in terms of S and y, we get
ℎ 𝑆 2ℎ 2ℎ 𝑆 ℎ𝑆 6 𝑓 𝑥 ,𝑥 𝑓𝑥 ,𝑥 10
The last part of equation (10) involves divided difference. This equation applies at each interval
point, from i = 1 to i = n-1. Thus, for n+1 points, there are n+1 Si values, but n-1 equations. Two
additional equations are obtained by using arbitrary end conditions.
Four end conditions can be used in cubic spline.
1. So = 0 and Sn = 0 Natural Spline end cubic approach linearity at their extremities.
2. f'(xo) = A and f'(xn) = B; in this condition, to calculate So and Sn, we use
at left end, 2 ho So + h1 S1 = 6 (F[xo,x1] - A)
at right end, hn-1 Sn-1 + 2 hn Sn = 6 (B - f[xn-1,xn])
3. So = S1, and Sn-1 = Sn-2 end cubic approach parabola at their extremities
4. So and Sn as linear extrapolations from (S1 and S2), and (Sn-1 and Sn-2), respectively; in this
condition, to calculate So and Sn, we use
at left end, So = [(ho+h1) S1 - ho S2] / h1
at right end, Sn = [(hn-2 + hn-1) Sn-1 - hn-1 Sn-2] / hn-2
Compiled By: Rupesh D Shrestha Nepal Engineering College
Numerical Methods
Chapter 2: Interpolation and Approximation
We will concentrate only on condition 1.
For Condition (1), So = 0 and Sn = 0, and the coefficient matrix is
2 ℎ ℎ ℎ
⎡ ⎤
ℎ 2 ℎ ℎ ℎ
⎢ ⎥
⎢ ℎ 2 ℎ ℎ ℎ ⎥
⎢ ⋯ … ⎥
⎣ ℎ 2 ℎ ℎ ⎦
𝑓 𝑥 ,𝑥 𝑓 𝑥 ,𝑥
And the right hand side (RHS) is 6 𝑓 𝑥 ,𝑥 𝑓 𝑥 ,𝑥
⋯
𝑓 𝑥 ,𝑥 𝑓 𝑥 ,𝑥
For Condition 2, f'(xo) = A and f'(xn) = B and the coefficient matrix is
2 ho h1
h 2 (h0 h1 ) h1
0
h1 2 (h1 h2 ) h2
... ...
hn 2 2hn 1
For Condition 3, So = S1 and Sn = Sn - 1 and the coefficient matrix is
(3 ho 2 h1 ) h1
h1 2 (h1 h2 ) h2
h2 2 (h2 h3 ) h3
... ...
hn 2 (2 hn 2 3hn1 )
For Condition 4, So and Sn are linear extrapolation, and the coefficient matrix is
( ho h1 )(ho 2h1 ) h1 2 ho 2
h1 h1
h1 2 (h1 h2 ) h2
h2 2 ( h2 h3 ) h3
... ...
h 2 n 2 h 2 n 1 ( hn 1 hn 2 )( hn 1 2 hn 2 )
hn 2 hn 2
# Example: Using natural cubic spline interpolation, find the interpolating polynomials between
each interval and use them to interpolate the values at x = 0.5 and x = 2.0.
i 0 1 2 3
x 0.0 1.0 1.5 2.25
f(x) 2.0000 4.4366 6.7134 13.9130
For natural cubic spline, f'' o = So = 0, f''3 = S3 = 0
Here, ho = 1.0 -0.0 = 1.0, h1 = 1.5 - 1.0 = 0.5, h2 = 2.25 - 1.5 = 0.75
f[0, 1] = (y1-y0)/h0= (4.4366-2.0000)/1 = 2.4266;
f[1, 1.5] = (y2-y1)/h1 =(6.7134-4.43660/(1.5-1) = 4.5536
Compiled By: Rupesh D Shrestha Nepal Engineering College
Numerical Methods
Chapter 2: Interpolation and Approximation
f[1.5, 2.25] = 9.5995
The coefficient matrix is:
2 ( ho h1 ) h1 2 (1 0.5) 0.5 3 0.5
h1
2 ( h1 h2 ) 0.5 2 (0.5 0.75) 0.5 2.5
f [ x , x ] f [ xo , x1 ] 4.5536 2.4366 2.1170 12.7020
RHS 6 1 2 6 6
f [ x2 , x3 ] f [ x1 , x2 ] 9.5995 4.5536 5.0459 30.2754
3 0.5 S1 12.7020
The matrix form of the equation is S1 = 2.2920; S2 = 11.6518
0.5 2.5 S2 30.2754
ai = (Si+1 - Si) / 6hi ; bi = Si / 2; ci = f[xi+1, xi] - (2 hi Si + hi Si+1) / 6; di = yi
i ai bi ci di
0 0.3820 0 2.0546 2.0
1 3.1199 1.146 3.2006 4.4366
2 -2.593 5.8259 6.6865 6.7134
We have, the interpolating cubic spline function is gi(x)=ai (x-xi)3 +bi (x-xi)2 + ci (x-xi) + di
The interpolation function for each interval is as follows.
i interval function
0 [0.0,1.0] 0.3820 (x - 0)3 + 0 (x - 0)2 + 2.0546 (x - 0) + 2
1 [1.0,1.5] 3.1199 (x - 1)3 + 1.146 (x - 1)2 + 3.2006 (x - 1) + 4.4366
2 [1.5,2.25] -2.5893 (x - 1.5)3 + 5.8259 (x - 1.5)2 + 6.6865 (x - 1.5) + 6.7134
f(0.5) = 0.3820 (0.5)3 + 2.0546 (0.5) + 2 = 3.075 (real = 3.047; error = 0.028)
f(2.0) = -2.5893 (0.5)3 + 5.8259 (0.5)2 + 6.6865 (0.5) + 6.7134 = 11.1895 (real = 10.778; error = 0.41)
Compiled By: Rupesh D Shrestha Nepal Engineering College
Numerical Methods
Chapter 2: Interpolation and Approximation
Working Procedure for Natural Cubic Spline with Constant Interval:
Step 1: At each of the interior points i, write the equation
6
Si 1 4 Si Si 1 2 [ yi 1 2 yi yi 1 ]
h
Step 2: For natural spline, So = 0, Sn = 0; Solve for S1, S2, S3, ... , Sn
Step 3: The cubic spline polynomial for each interval is
1 1 h2 1 h2
f ( xi ) [( xi 1 x ) 3 Si ( x xi ) 3 Si 1 ] [( xi 1 x ) ( yi Si )] [( x xi ) ( yi 1 Si 1 ]
3!h h 3! h 3!
Example:
x 0.0 1.0 2.0 3.0 0.5 1.5 2.25
Here, h = 1.0,
y 2.0000 4.4366 10.778 31.1711 ? ? ?
interior points are at x
= 1 and x = 2;
For natural cubic spline curvature at end points = 0; i.e., So = 0, S3 = 0
At i = 1, i.e., at the first internal point, xo = 0, yo = 2.0, x1 = 1, y1 = 4.4366, y2 = 10.778
So + 4 S1 + S2 = [6 / (12)] (yo - 2 y1 + y2)
0 + 4 S1 + S2 = 6 ( 2 - (2). 4.4366 + 10.778) = 23.4288 4 S1 + S2 =23.4288 ... (1)
At i = 2, S1 + 4 S2 + S3 = [6 / (12)] (y1 - 2 y2 + y3)
S1 + 4 S2 + 0 = 6 (4.4366 - (2). 10.778 + 31.1711) = 84.3102 S1 + 4 S2 =84.3102 ... (2)
From (1) and (2), 15 S1 = 9.405 S1 = 0.627, S2 = 20.9208
The interpolation function at each interval is
i interval function
0 [0,1.0] ((1-x)3. 0 + (x-0)3 0.627)/(6 . 1) + (1-x) (2.0000 - (1/6).0)+(x-0)(4.4322-(1/6)0.627)
= (0.627x3) / 6 + (1-x) .2 + (x . 4.3321)
1 [1.0,2.0] ((2-x)3 0.627 + (x-1)3 20.9208) / 6 + (2-x) (4.4366 - (0.627 / 6)) + (x-1) (10.778-(20.9208/6))
2 [2.0,3.0] ((3-x)3 20.9208 + (x-2)3.0) / 6 + (3-x) (10.778 - (20.9208 / 6)) + (x-2)(31.1711-(0 / 6))
f(0.5) = (0.627 . 0.53)/6 + (1-0.5) 2 + (0.5. 4.3321) = 0.013 + 1 + 2.116 = 3.179
f(1.5) = (0.627 . 0.53 + 0.53. 20.9208) / 6 + 0.5 . 4.3321 + 0.5 . 7.2912 = 0.4489 + 2.1661 + 3.6456 = 6.2606
f(2.25) = (0.753 . 20.9208) / 6 + 0.75 . 7.9212 + 0.25 (31.1711) = 1.471 + 5.4684 + 7.7928 = 14.7322
f(2.99) = (0.013. 20.9208) / 6 + 0.01 . 7.2912 + 0.99 (31.1711) = 0.00000349 + 0.07291 + 30.8594 = 30.932
Similarly, f(2.9999) = 31.1687
Example:
Fit the following four points by the cubic splines. Use the i 0 1 2 3 end
conditions y''o = y''3 = 0. Compute y(1.5). xi 1 2 3 4
Here, h = 1, So = y''o 0 S3 = y''3 = 0 yi 1 5 11 8
At i = 1, So + 4 S1 + S2 = [6 / (12)] (yo - 2 y1 + y2)
0 + 4 S1 + S2 = 6 ( 1 - 2. 5 + 11) = 12 4 S1 + S2 = 12 ... (1)
At i = 2, S1 + 4 S2 + S3 = [6 / (12)] (y1 - 2 y2 + y3)
S1 + 4 S2 + 0 = 6 (5 - 2. 11 + 8) = -54 S1 + 4 S2 = -54 ... (2)
From (1) and (2), S1 = 4.4, S2 = -14.6
The interpolation function at each interval is
i interval function
0 [0,1.0] (4.4 (1-x)3 / (6) - (1) (x-2) + (5 - (4.4/6)) (x-1)= 0.733(x-1)3 - (x-2) + 4.267 (x-1)
1 [1.0,2.0] (-14.6 (x-2)3 - 4.4 (x-3)3) / 6 - (5 - 4.4/6)) (x-3) + (11 + (14.6/6)) (x-2)
2 [2.0,3.0] (14.6 (x-4)3 / 6 - (11+(14.6) / 6)) (x-4) + 8 (x-3)
y(1.5) = (-14.6(-0.53) - 4.4(-1.53]/6 - (4.2667) (-1.5) + 13.4333 (-0.5) = (1.825+14.85)/6 + 6.4 - 6.7166 = 2.463
Compiled By: Rupesh D Shrestha Nepal Engineering College