0% found this document useful (0 votes)
8 views8 pages

Spline Interpolation Methods Explained

The document discusses spline interpolation, focusing on linear and cubic spline methods. It defines spline functions, their properties, and provides examples of how to calculate linear and cubic splines using given data sets. Additionally, it introduces MATLAB functions for implementing spline interpolation, including the 'spline' and 'csape' functions.

Uploaded by

nasif nahid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Spline Interpolation Methods Explained

The document discusses spline interpolation, focusing on linear and cubic spline methods. It defines spline functions, their properties, and provides examples of how to calculate linear and cubic splines using given data sets. Additionally, it introduces MATLAB functions for implementing spline interpolation, including the 'spline' and 'csape' functions.

Uploaded by

nasif nahid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Summer 2022-2023

Numerical Methods For Science and Engineering


Lecture Note 5
Spline Interpolation

5.1 Introduction

Spline interpolation function is a piecewise polynomial function joined together with certain
conditions satisfied by them.
A function 𝑓(𝑥) of the form
𝑓1 (𝑥 ), 𝑥1 ≤ 𝑥 < 𝑥2
𝑓 (𝑥 ), 𝑥2 ≤ 𝑥 < 𝑥3
𝑓 (𝑥 ) = { 2

𝑓𝑛−1 (𝑥 ), 𝑥𝑛−1 ≤ 𝑥 ≤ 𝑥𝑛

is called a spline of degree m if


(i) the domain of 𝑓(𝑥) is the interval [𝑥1 , 𝑥𝑛 ]
(ii) 𝑓 (𝑥 ), 𝑓 ′ (𝑥 ), 𝑓 ′′ (𝑥 ) … 𝑓 (𝑚−1) are all continuous functions on [𝑥1 , 𝑥𝑛]
(iii) 𝑓(𝑥) is a polynomial of degree less than equal to m on each subinterval
[𝑥𝑘 , 𝑥𝑘+1 ], 𝑘 = 1, 2, ⋯ , 𝑛.

5.1.1 Linear Spline Interpolation

For a linear spline through (𝑥𝑘 , 𝑦𝑘 ) we may take 𝑓𝑘 (𝑥) is of the form
𝑓𝑘 (𝑥 ) = 𝑎𝑘 (𝑥 − 𝑥𝑘 ) + 𝑏𝑘 , for 𝑥𝑘 ≤ 𝑥 ≤ 𝑥𝑘+1 ′
Since the line passes through (𝑥𝑘 , 𝑦𝑘 ) and (𝑥𝑘+1 , 𝑦𝑘+1 ) we have
𝑏𝑘 = 𝑦𝑘
𝑦𝑘+1 −𝑦𝑘 ∆𝑦𝑘
and 𝑎𝑘 = =
𝑥𝑘+1 −𝑥𝑘 ℎ𝑘

where ∆𝑦𝑘 = 𝑦𝑘+1 − 𝑦𝑘 and ℎ𝑘 = 𝑥𝑘+1 − 𝑥𝑘.

The resulting linear spline curve 𝑓𝑘 (𝑥 ) in [𝑥𝑘 , 𝑥𝑘+1 ] can be written as


∆𝑦𝑘
𝑓𝑘 (𝑥 ) = 𝑦𝑘 + (𝑥 − 𝑥 𝑘 ), (𝑘 = 1, 2, … , 𝑛 − 1).
ℎ𝑘

Page 1 of Lecture 3
Summer 2022-2023

Example 5.1

Find the linear spline for the following data set

X 1 1 2 5
Y 2.2 3.5 5.4 1.5
Hence estimate the value of 𝑦(1.5).
Solution
Linear spline functions in different intervals are
3.5 − 2.2
𝑓1 (𝑥 ) = 2.2 + (𝑥 + 1) = 2.2 + 0.65(𝑥 + 1) , −1 ≤𝑥 ≤1
1 − (−1)
5.4 − 3.5
𝑓2 (𝑥 ) = 3.5 + (𝑥 − 1) = 3.5 + 1.9(𝑥 − 1), 1≤𝑥≤2
2−1
1.5 − 5.4
𝑓3 (𝑥 ) = 5.4 + (𝑥 − 2) = 5.4 − 1.3(𝑥 − 2), 2≤𝑥≤5
5−2
Linear spline function is
2.2 + 0.65(𝑥 + 1) , −1 ≤𝑥 ≤1
𝑓 (𝑥 ) = { 3.5 + 1.9(𝑥 − 1), 1≤𝑥≤2
5.4 − 1.3(𝑥 − 2) 2≤𝑥≤5
The value 𝑥 = 1.5 is in 1 ≤ 𝑥 ≤ 2. Thus
𝑦 (1.5) = 3.5 + 1.9(1.5 − 1) = 4.45.

5.2 Cubic Spline Interpolation

Cubic spline interpolation is used very often. It gives smoother curves than other types. To
determine the cubic spline, we need to use cubic polynomial for each subintervals.
Consider the cubic polynomial 𝑓𝑘 (𝑥) in each subinterval [𝑥𝑘 , 𝑥𝑘+1 ], 𝑘 = 1 ,2, … , 𝑛 − 1 of
the form
𝑓𝑘 (𝑥 ) = 𝑎𝑘 (𝑥 − 𝑥𝑘−1 )3 + 𝑏𝑘 (𝑥 − 𝑥𝑘−1 )2 + 𝑐𝑘 (𝑥 − 𝑥𝑘−1 ) + 𝑑𝑘 , (𝑎𝑘 ≠ 0).
where 𝑎𝑘 , 𝑏𝑘 , 𝑐𝑘 and 𝑑𝑘 are to be determined.
Since the spline passes through ( xk , yk ) and f k (x) ,

𝑓1 (𝑥0 ) = 𝑦0 , and 𝑓𝑘 (𝑥𝑘 ) = 𝑦𝑘 , 𝑘 = 1, 2, 3, ⋯ , 𝑛.


𝑓𝑘 (𝑥𝑘 ) = 𝑓𝑘+1 (𝑥𝑘 ), 𝑘 = 1, 2, 3, ⋯ , 𝑛 − 1
𝑓𝑘 ′(𝑥𝑘 ) = 𝑓𝑘+1 ′(𝑥𝑘 ), 𝑘 = 1, 2, 3, ⋯ , 𝑛 − 1
𝑓𝑘 ′′(𝑥𝑘 ) = 𝑓𝑘+1 ′′(𝑥𝑘 ), 𝑘 = 1, 2, 3, ⋯ , 𝑛 − 1
We can see that there are
1 + 𝑛 + 3(𝑛 − 1) = 4𝑛 − 2
conditions but we need to determine 4𝑛 constants. So we need to add two boundary
conditions to get unique solution.
Page 2 of Lecture 3
Summer 2022-2023

Normally we use three types of boundary conditions:


1. Second derivatives at end points are known
𝑓1′′ (𝑥0) = 𝑀0 and 𝑓𝑛′′ (𝑥𝑛 ) = 𝑀𝑛 .
The special case
𝑓1′′ (𝑥0 ) = 0 and 𝑓𝑛′′ (𝑥𝑛) = 0
give spline called natural cubic spline.
2. First derivatives at end points are known
𝑓1′ (𝑥0) = 𝑑0 and 𝑓𝑛′ (𝑥𝑛) = 𝑑𝑛 .
give spline called clampedcubic spline.
3. Automatically adjusted boundary conditions known as not-a-knot cubic spline.
This condition assumes that 𝑓 ′′′(𝑥 ) are continuous at the second and last but one points.
′′′(𝑥 )
𝑓1 1 = 𝑓2′′′ (𝑥1) and 𝑓𝑛−1 ′′′
(𝑥𝑛−1 ) = 𝑓𝑛′′′(𝑥𝑛−1 ).
Note that minimum number of data points is four for this condition to be used..

MATLAB Spline Interpolation Functions

(1) MATLAB function spline

yy=spline(x, Y, xx) for not-a-knot cubic spline

x, Y are inputs and xx expolant.

yy=spline(x, [dY0, Y, dYn], xx) for clamped cubic spline

` 𝑑𝑌0 = 𝑌 (𝑥0)and 𝑑𝑌𝑛 = 𝑌′(𝑥𝑛)

(2) csape spline interpolation with various end conditions

Syntax: sp=csape(X, Y, conds)

some of the conds are

‘second’ adjusted second derivatives if not mentioned it uses [0, 0]


‘clamped’ adjusted first derivatives
‘not-a-knot’ uses not-a-knot condtion

Example 5.2
A natural cubic spline is defined by
𝐴(𝑥 + 1)3 + 𝐵(𝑥 + 1)2 + 𝐶 (𝑥 + 1) + 1, − 1 ≤ 𝑥 < 1
𝑓 (𝑥 ) = {
𝐷 (𝑥 − 1) 3 + 6(𝑥 − 1) 2 + 𝐸 (𝑥 − 1) − 1, 1≤𝑥≤2
(i) Use continuity and boundary conditions to estimate 𝐴, 𝐵, 𝐶, 𝐷 and 𝐸.
(ii) Find the value of 𝑓(1.4) from the spline curve’
(iii) Use MATLB function “sp=csape(x, y, ‘conds’)” to construct natural cubic spline
for the data set (−1, 1), (1, −1) and (2, 10). Find 𝑓(1.4) using “fnval(sp,x)”’
Plot the spline curve using “fnplt(sp)” along with the data points.

Page 3 of Lecture 3
Summer 2022-2023

Solution
Let 𝑓1 (𝑥 ) = 𝐴 (𝑥 + 1)3 + 𝐵 (𝑥 + 1)2 + 𝐶 (𝑥 + 1) + 1,
and 𝑓2 (𝑥 ) = 𝐷 (𝑥 − 1) 3 + 6(𝑥 − 1)2 + 𝐸 (𝑥 − 1) − 1,
Then
𝑓1′ (𝑥 ) = 3𝐴 (𝑥 + 1)2 + 2𝐵 (𝑥 + 1) + 𝐶
𝑓2′ (𝑥 ) = 3𝐷 (𝑥 − 1)2 + 12(𝑥 − 1) + 𝐸
and
𝑓1′′ (𝑥 ) = 6𝐴 (𝑥 + 1) + 2𝐵
𝑓2′′ (𝑥 ) = 6𝐷 (𝑥 − 1) + 12
(i) Conditions at the interior point 𝑥 = 1 give
𝑓1 (1) = 𝑓2 (1) ⟹ 8𝐴 + 4𝐵 + 2𝐶 + 1 = −1 (1)
𝑓1′ (1) = 𝑓2′ (1) ⟹ 12 𝐴 + 4𝐵 + 𝐶 = 𝐸 (2)
𝑓1′′ (1) = 𝑓2′′ (1) ⟹ 12𝐴 + 2𝐵 = 12 (3)
For natural cubic spline the boundary conditions give
𝑓1′′ (−1) = 2𝐵 = 0 or 𝐵=0
𝑓2′′ (2) = 6𝐷 + 12 = 0 or 𝐷 = −2
From (3), 12𝐴 = 12 or 𝐴=1
From (1), 8(1)+2𝐶 + 1 = −1 or C= −5
From (2), 12(1) + (−5) = 𝐸 or 𝐸=7

The natural cubic spline function is


(𝑥 + 1) 3 − 5(𝑥 + 1) + 1 , −1 ≤𝑥 < 1
𝑓 (𝑥 ) = { 3 2
( ) ( ) ( )
−2 𝑥 − 1 + 6 𝑥 − 1 + 7 𝑥 − 1 − 1, 1≤𝑥≤2
(ii) 𝑓(1.4) = 𝑓2 (1.4) = −2(0.4)3 + 6(0.4) 2 + 7(0.4) = 2.632.

(ii)
>> clear
>> x=[-1 1 2];
>> y=[1 -1 10];
>> sp=csape(x,y,'second');
>> y1=fnval(sp,1.4)

y1 =
2.6320

>> fnplt(sp, 2, [-2, 4])


>> hold on % used to plot in the same figure
>> plot(x, y,'O')
>> hold off
Page 4 of Lecture 3
Summer 2022-2023

Example:5.3
A cubic spline 𝑓(𝑥) which interpolates the data (1, −10) , (2, −6), (4, 2), (5, 18) is defined
(𝑥 − 1)3 + 𝑏1 (𝑥 − 1)2 + 𝑐1 (𝑥 − 1) − 10, 1 ≤ 𝑥 < 2
by 𝑓 (𝑥 ) = { 𝑎2 (𝑥 − 2)3 − (𝑥 − 2)2 + 𝑐2 (𝑥 − 2) − 6, 2 ≤ 𝑥 < 4
𝑎3 (𝑥 − 4) 3 + 𝑏3 (𝑥 − 4)2 + 10(𝑥 − 4) + 2, 4 ≤ 𝑥 ≤ 5

(i) If the spline satisfies the not-a-knot boundary conditions, find 𝑎2 and 𝑎3 .
(ii) Using continuity of 𝑓′′(𝑥) at 𝑥 = 2 and 4, find 𝑏1 and 𝑏3 .
(iii)Using continuity of 𝑓′(𝑥) at 𝑥 = 2 and 4, find 𝑐1 and 𝑐2 .
(iv) Show that 𝑓(𝑥 ) passes through (5, 18).
(v) Use MATLB function “sp=spline(x, y)” to construct the spline curve and find
coefficients by “[Link]”.
(vi) Write down MATLAB codes using “fnval(sp,x)” to estimate the values of 𝑓(𝑥)
for 𝑥 = 1.4, 2.5 and 4.8 from the spline curve.
(vii) Write down MATLAB codes using “fnplt(sp)” to plot the spline curve and the
given data points.

𝑓1 (𝑥), 1 ≤ 𝑥 < 2
( )
Solution : Let the cubic spline be 𝑓 𝑥 = {𝑓2 (𝑥), 2 ≤ 𝑥 < 4
𝑓3 (𝑥), 4 ≤ 𝑥 ≤ 5
where
𝑓1 (𝑥 ) = (𝑥 − 1)3 + 𝑏1 (𝑥 − 1)2 + 𝑐1 (𝑥 − 1) − 10,
𝑓2 (𝑥 ) = 𝑎2 (𝑥 − 2)3 − (𝑥 − 2)2 + 𝑐2 (𝑥 − 2) − 6,
𝑓3 (𝑥 ) = 𝑎3 (𝑥 − 4)3 + 𝑏3 (𝑥 − 4)2 + 10(𝑥 − 4) + 2.

Then 𝑓1 ′(𝑥 ) = 3(𝑥 − 1) 2 + 2𝑏1 (𝑥 − 1) + 𝑐1 ,


𝑓2 ′(𝑥 ) = 3𝑎2 (𝑥 − 2) 2 − 2(𝑥 − 2) + 𝑐2 ,
𝑓3 ′(𝑥 ) = 3𝑎3 (𝑥 − 4)2 + 2𝑏3 (𝑥 − 4) + 10.

And 𝑓1′′ (𝑥 ) = 6(𝑥 − 1) + 2𝑏1 ,


𝑓2 ′′(𝑥 ) = 6𝑎2 (𝑥 − 2) − 2,
𝑓3′′ (𝑥 ) = 6𝑎3 (𝑥 − 4) + 2𝑏3 .

Also 𝑓1′′′ (𝑥 ) = 6, 𝑓2′′′ (𝑥 ) = 6𝑎2 , 𝑓3′′′ (𝑥) = 6𝑎3 .


i. Spline curve satisfies not-a-knot boundary conditions. Thus
𝑓1′′′ (2) = 𝑓2′′′ (2) ⇒ 6 = 6𝑎2 or 𝑎2 = 1.
𝑓3′′′ (4) = 𝑓2′′′(4) 𝑜𝑟 6𝑎3 = 6𝑎2 = 6 𝑜𝑟 𝑎3 = 1.
ii. Continuity of 𝑓 ′′ (𝑥 ) at 𝑥 = 2 and 4 gives
𝑓1′′ (2) = 𝑓2′′ (2) ⇒ 6(1) + 2𝑏1 = −2 or 𝑏1 = −4.
1
And 𝑓2′′ (4) = 𝑓3′′ (4) ⇒ 6𝑎2 (2) − 2 = 2𝑏3 or 𝑏3 = [6(1)(2) − 2] = 5.
2

Page 5 of Lecture 3
Summer 2022-2023

iii. Continuity of 𝑓′(𝑥 ) at 𝑥 = 2 and 4 gives


𝑓1′ (2) = 𝑓2′ (2) ⇒ 3 + 2𝑏1 + 𝑐1 = 𝑐2 .
And 𝑓2′ (4) = 𝑓3′ (4) ⇒ 3𝑎2 (4) − 2(2) + 𝑐2 = 10
𝑐2 = 10 − 3(1)(4) + 4 = 2
and 𝑐1 = 2 − 3 − 2(−4) = 7.
iv. 𝑓(5) = 𝑓3 (5) = 1(1) + 5(1) + 10(1) + 2 = 18.
v.
>> x=[1 2 4 5];
>> y=[-10 -6 2 18];
>> sp=spline(x,y);
>> format short g
>> Coefficients = [Link]

Coefficients =
1 -4 7 -10
1 -1 2 -6
1 5 10 2
vi.
>> x1=[1.4, 2.5, 4.8];
>> y1=fnval(sp,x1);
>> xy_value =[x1', y1']

xy_value =
1.4 -7.776
2.5 -5.125
4.8 13.712

vii.
>> fnplt(sp, [0, 6])
>> hold on
>> plot(x,y,'o')

Page 6 of Lecture 3
Summer 2022-2023

Exercise 5

1. In a chemical reaction the concentration level y of the product at time t (minute) was
measured every half hour. The following results were found:

t 1.0 1.5 2.0 2.5


y 0.24 0.27 0.31 0.36

Construct a linear spline interpolation to estimate the concentration level at 2.25


minute.

2. Use the portion of the given steam table for superheated H 2O at 200 MPa to find the
corresponding entropy, s, for a specific volume, v, of 0.118 m3/kg with linear spline.
V (m3/kg) 0.2037 0.2114 0.32547 0.33213
S (kJ/kg K) 6.5147 6.6453 6.8664 6.9513
3. Given the following set of values of 𝑥 and 𝑓(𝑥):
(−1, 6), (1, 2) and (2, 12) .
Find the linear spline passing through the above points.
A natural cubic spline through the above points are defined by
𝐴(𝑥 + 1)3 + 𝐵(𝑥 + 1)2 + 𝐶 (𝑥 + 1) + 6, − 1 ≤ 𝑥 < 1
𝑓 (𝑥 ) = {
𝐷 (𝑥 − 1) 3 + 6(𝑥 − 1) 2 + 𝐸 (𝑥 − 1) + 2, 1≤𝑥≤2
Use continuity and boundary conditions to find equations satisfied by 𝐴, 𝐵, 𝐶, 𝐷 and 𝐸.
Solve for 𝐴, 𝐵, 𝐶, 𝐷 and 𝐸.
Find the value of 𝑓(0.4) from linear spline and 𝑓(1.4) from cubic curve.
Write MATLAB codes using “sp=csape(x, y, ‘second’)” to construct natural cubic spline
for the above data set. Plot the spline curve using “fnplt(sp)” along with the data points.

4. A natural cubic spline𝑓(𝑥) is defined by


𝐴(𝑥 + 1)3 + 𝐵(𝑥 + 1)2 + 𝐶 (𝑥 + 1) + 7, −1 ≤𝑥 ≤2
𝑓 (𝑥 ) = {
𝐷 (𝑥 − 2)3 + 5(𝑥 − 2) 2 + 𝐸 (𝑥 − 2) + 2, 2≤𝑥≤4
i. Use continuity and boundary conditions to find the values of A, B, C, D, and E.
ii. Estimate the value of 𝑓(3.5) .
Iii. Write a MATLB code using function “spline(x, y)” to construct the spline curve and
“fnval(function,x)” to estimate the values of 𝑓(𝑥) for 𝑥 = −0.5, and 3.9.

5. A natural cubic spline𝑓(𝑥) is defined by


𝐴 (𝑥 + 1)3 + 𝐵 (𝑥 + 1)2 + 𝐶 (𝑥 + 1) − 6, −1 ≤𝑥 ≤ 1
𝑓 (𝑥 ) = {
𝐷 (𝑥 − 1) 3 + 12(𝑥 − 1)2 + 𝐸 (𝑥 − 1) + 2, 1≤𝑥≤3
i. Use continuity and boundary conditions to find the values of A, B, C, D and E.
ii. Estimate 𝑓(2)from the spline curve.
iii. Use MATLAB function “sp=spline(x, y)” and “fnval(sp,x)” to construct the
spline curve from the data (1, −10) , (2, −6), (4, 2), (6, 15) and find the values
of 𝑓(𝑥) for 𝑥 = 1.4, 2.5 and 4.8 from the spline curve.

Page 7 of Lecture 3
Summer 2022-2023

6. A clamped cubic spline function f (x ) through (1,2), (2, 3), (3, 5) is defined by
1.5(𝑥 − 1)3 + 𝐴(𝑥 − 1)2 + 𝐵(𝑥 − 1) + 2, 1≤𝑥 ≤2
𝑓(𝑥) = { 3 2
𝐶(𝑥 − 2) + 2(𝑥 − 2) + 𝐷(𝑥 − 2) + 3, 2≤𝑥≤3
Given that 𝑓 ′ (1) = 2 and f ′ (3) = 1.
i. Use continuity and boundary conditions to find the values of A, B, C and D.
ii. Estimate 𝑓(2.5)from the spline curve.
iii. Use MATLB function “sp=spline(x, y)” and “fnval(sp,x)” to construct the spline curve
from the data (1,2), (2, 3), (3, 5) and find the values of 𝑓(𝑥) for 𝑥 = 1.4 and 2.5
from the spline curve.

Page 8 of Lecture 3

You might also like