Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
39 views72 pages

Engineering Math: Interpolation Guide

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)
39 views72 pages

Engineering Math: Interpolation Guide

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
You are on page 1/ 72

BDA34003

ENGINEERING MATHEMATICS 4

Lecture Module 4: Interpolation

Waluyo Adi Siswanto


[email protected]
Learning Outcomes

 Students have the knowledge of interpolation, and will


be able to decide when interpolation is needed in
engineering applications
 Students will be able to predict a value in a set of data
by using a quick prediction of linear interpolation
 Students will be able use various non-linear methods
in complex and complicated set of data
 Students know the advantages and the disadvantages
of each method, so they can choose the appropriate
method for a particular engineering problem

BDA34003 Engineering Mathematics IV 2


Waluyo Adi Siswanto ([email protected])
Topics

➢ Motivation: WHY we need Interpolation ?


➢ Linear Interpolation
➢ Polynomial
➢ Lagrange
➢ Newton
➢ Piecewise Polynomial
➢ Natural Cubic Spline
➢ Clamped Cubic Spline

BDA34003 Engineering Mathematics IV 3


Waluyo Adi Siswanto ([email protected])
An engineering problem here

You measured the power of an engine at various engine rotations.

90

80

70

60
Engine power (HP)

50

40

30

20

10

0
0 200 400 600 800 1000 1200 1400 1600

Engine rotation (RPM)

BDA34003 Engineering Mathematics IV 4


Waluyo Adi Siswanto ([email protected])
Then you want to know the engine power at 800 RPM

90

80

70

60
Engine power (HP)

50

40

30

20

10

0
0 200 400 600 800 1000 1200 1400 1600

Engine rotation (RPM)

BDA34003 Engineering Mathematics IV 5


Waluyo Adi Siswanto ([email protected])
But no data at 800 RPM

HOW can you predict the engine power ?


90

80

70

? 60
Engine power (HP)

50

40

30

20

10

0
0 200 400 600 800 1000 1200 1400 1600

Engine rotation (RPM)

BDA34003 Engineering Mathematics IV 6


Waluyo Adi Siswanto ([email protected])
Another problem case

Atmospheric standard data table (from Public Domain Aeronautical Software):

alt is altitude in kilometers.


sigma is density divided by sea-level density.
delta is pressure divided by sea-level pressure.
theta is temperature divided by sea-level temperature.
temp is temperature in kelvins.
press is pressure in newtons per square meter.
dens is density in kilograms per cubic meter.
a is the speed of sound in meters per second.
visc is viscosity in 10**(-6) kilograms per meter-second.
k.visc is kinematic viscosity in square meters per second.

BDA34003 Engineering Mathematics IV 7


Waluyo Adi Siswanto ([email protected])
Atmospheric standard data table (from Public Domain Aeronautical Software):

You need
information
of the air pressure
at altitude 9.5 km

But
Data is not
Available ?

HOW to get
The air pressure

BDA34003 Engineering Mathematics IV 8


Waluyo Adi Siswanto ([email protected])
Now you know that in engineering, sometimes you
need to predict a value in a set of data!

Let's see a method to do a QUICK PREDICTION,


PREDICTION
by using a linear interpolation.

BDA34003 Engineering Mathematics IV 9


Waluyo Adi Siswanto ([email protected])
Predict a data
in between two known data
Linear Interpolation
A simple assumption is the curve connecting the two known data is a straight (linear) line

90

80
f ( x2)

? 70

60 f ( x1)
f ( x)
Engine power (HP)

50

40

30

20

10

x1 x x2
0
0 200 400 600 800 1000 1200 1400 1600

Engine rotation (RPM)

BDA34003 Engineering Mathematics IV 10


Waluyo Adi Siswanto ([email protected])
f ( x2) Considering simple triangular ratio that all
of you have known this:
a c
=
b d
f ( x) d
( x− x1 ) ( f ( x)− f ( x 1 ) )
=
f ( x1) c ( x 2 − x 1 ) ( f ( x 2 )− f ( x1 ))

a ( x 2 − x 1 ) ( f ( x)− f ( x 1 ) )=( x− x 1 ) ( f ( x 2 )− f ( x 1 ) )
b ( x 2 − x 1 ) f ( x)−( x 2 − x1 ) f ( x 1 )=
( x− x 1 ) f ( x 2 )−( x− x 1 ) f ( x 1 )

( x 2 − x 1 ) f ( x)=
( x− x1 ) f ( x 2 )−( x− x 1 ) f ( x 1 )+ ( x 2− x 1 ) f ( x 1 )

( x 2 − x 1 ) f ( x)=( x− x 1 ) f ( x 2 )+ ( x 2 − x) f ( x 1 )
x1 x x2

BDA34003 Engineering Mathematics IV 11


Waluyo Adi Siswanto ([email protected])
f ( x2)
( x 2 − x 1 ) f ( x)=( x− x 1 ) f ( x 2 )+ ( x 2 − x) f ( x 1 )

( x 2 − x) ( x− x 1 )
f ( x)= f ( x1 )+ f ( x 2)
f ( x) ( x 2 − x1 ) ( x 2− x 1 )

f ( x1) ( x 2− x) f ( x 1 )+ ( x− x 1 ) f ( x 2 )
f ( x)=
( x2− x1)

and see the dancing steps


of the quick linear interpolation

x1 f ( x 1)

x1 x2 x2 f ( x2)
x

BDA34003 Engineering Mathematics IV 12


Waluyo Adi Siswanto ([email protected])
Example 4-1

Can you predict the engine power at 820 RPM

90
80
80 77

70
60 60
60 55
50 50
Engine power (HP)

50
40 40
40
30 30
30 25 25
20
20

10
0
0
0 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600

Engine rotation (RPM)

BDA34003 Engineering Mathematics IV 13


Waluyo Adi Siswanto ([email protected])
Yes, no problem, it can be predicted between 700 and 900 RPM, then Dance

(900−820)60+ (820−700) 77
Power= = 70.2
90
(900−700)
80
80 77

70
60 60
60 55
50 50
Engine power (HP)

50
40 40
40
30 30
30 25 25
20
20

10
0
0
0 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600

Engine rotation (RPM)

BDA34003 Engineering Mathematics IV 14


Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 15
Waluyo Adi Siswanto ([email protected])
Example 4-2

You need
information
of the air pressure
at altitude 9.5 km

No problem,
Between 8-10
and dance

4
x 1 =8 f ( x 1 )=3.565 10

x=9.5 4 N
Pressure=2.8788 10 2
m
4
x 2 =10 f ( x 2 )=2.650 10

BDA34003 Engineering Mathematics IV 16


Waluyo Adi Siswanto ([email protected])
SMath

m-code function
function fx = IntpLinear(x,x1,fx1,x2,fx2)
% Linear interpolation 2 points
fx = ((x2-x)*fx1+(x-x1)*fx2)/(x2-x1);

BDA34003 Engineering Mathematics IV 17


Waluyo Adi Siswanto ([email protected])
Congratulation.. you have learned a Quick Prediction
by using linear interpolation.
90
But actually, the actual curve is not s straight line
80
80 77

70
60 60
60 55
50 50
Engine power (HP)

50
40 40
40
30 30
30 25 25
20
20

10
0
0
0 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600

Engine rotation (RPM)

BDA34003 Engineering Mathematics IV 18


Waluyo Adi Siswanto ([email protected])
Your prediction is linear (straight) line
But actually, the data is continued
90
80
80 77

70
60 60
60 55
50 50
Engine power (HP)

50
40 40
40
30 30
30 25 25
20
20

10
0
0
0 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600

Engine rotation (RPM)

BDA34003 Engineering Mathematics IV 19


Waluyo Adi Siswanto ([email protected])
This is your result (linear interpolation)
90
80
80 77

70
60 60
60 55
50 50
Engine power (HP)

50
40 40
40
30 30
30 25 25
20
20

10
0
0
0 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600

Engine rotation (RPM)

BDA34003 Engineering Mathematics IV 20


Waluyo Adi Siswanto ([email protected])
and here is the correct one
90
80
80 77

70
60 60
60 55
50 50
Engine power (HP)

50
40 40
40
30 30
30 25 25
20
20

10
0
0
0 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600

Engine rotation (RPM)

BDA34003 Engineering Mathematics IV 21


Waluyo Adi Siswanto ([email protected])
You can see here that linear lines cannot represent correctly the real curve (non linear)

90
80
80 77

70
60 60
60 55
50 50
Engine power (HP)

50
40 40
40
30 30
30 25 25
20
20

10
0
0
0 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600

Engine rotation (RPM)

BDA34003 Engineering Mathematics IV 22


Waluyo Adi Siswanto ([email protected])
Nonlinear Interpolation
Linear interpolation always use 2 data and predicts a value in between 2 data with the assumption
the connection line (function) is straight (linear) line.
If the the points to consider more than 2,
then it is possible to use non straight
90
(non linear) line (function) for the prediction guideline
80
80 77

70
60 60
60 55
50 50
Engine power (HP)

50
40 40
40
30 30
30 25 25
20
20

10
0
0
0 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600

Engine rotation (RPM)

BDA34003 Engineering Mathematics IV 23


Waluyo Adi Siswanto ([email protected])
Polynomial Interpolation
Lagrange – two points

f ( x 2)
Linear interpolation:

( x 2 − x) ( x− x 1 )
f ( x)= f ( x1 )+ f ( x 2)
f ( x) ( x 2 − x1 ) ( x 2− x 1 )

f ( x1)
Lagrange writes in different way:

( x− x 2 ) ( x− x 1 )
f ( x)= f ( x1 )+ f ( x 2)
( x1 − x 2 ) ( x 2− x 1 )

L1 ( x) L 2 ( x)

Lagrange 2 points = linear interpolation

x1 x2 (use this only for QUICK PREDICTION !)


x

BDA34003 Engineering Mathematics IV 24


Waluyo Adi Siswanto ([email protected])
Polynomial Interpolation
Lagrange – three points

f ( x3)

f ( x2)
f ( x)

f ( x1) Extending Lagrange two points to three points

x1 x x2 x3

( x− x 2 )( x− x 3 ) ( x− x 1 )( x− x 3 ) ( x− x 1 )( x− x 2 )
f ( x)= f ( x 1 )+ f ( x 2 )+ f ( x3 )
( x1 − x 2 )( x 1− x 3 ) ( x 2 − x 1 )( x 2− x 3 ) ( x 3− x 1 )( x 3 − x 2 )

L1 ( x) L 2 ( x) L3 ( x)

BDA34003 Engineering Mathematics IV 25


Waluyo Adi Siswanto ([email protected])
Example 4-3

f ( x4 )
Learning the pattern of Lagrange
2 and 3 points. Write the equation for
f ( x3) Lagrange 4 points !
f ( x 2)
f ( x) ( x− x 2 )( x− x 3 )( x− x 4 )
L1 ( x)=
( x 1 − x 2 )( x 1 − x 3 )( x 1− x 4 )
f ( x1)
( x− x 1 )( x− x 3 )( x− x 4 )
L 2 ( x)=
( x 2 − x 1 )( x 2 − x 3 )( x 2 − x 4 )
x1 x x2 x3 x4
( x− x 1 )( x− x 2 )( x− x 4 )
L3 ( x)=
( x 3 − x 1 )( x 3 − x 2 )( x 3 − x 4 )

( x− x 1 )( x− x 2 )( x− x 3 )
L 4 ( x)=
( x 4 − x 1 )( x 4 − x 2 )( x 4 − x 3 )

f ( x)= L 1 ( x) f ( x 1 )+ L 2 ( x) f ( x 2 )+ L3 ( x) f ( x 3 )+ L 4 ( x) f ( x 4 )

BDA34003 Engineering Mathematics IV 26


Waluyo Adi Siswanto ([email protected])
Polynomial Interpolation
Lagrange – n points

Cardinal function (Lagrange quotient) function y = IntpLagrange(X, Y, x)


% Lagrange Interpolation
n = length(X);
for n points
if n ~= length(Y)
error('X and Y must be the same length.');
i=1,2, ⋯ n end
y = 0;
for i = 1:n
( x− x 1 ) ( x− x 2 ) ( x− x n ) L = 1;
Li ( x)= . ⋯ p = 1;
( xi − x1) ( xi − x2) ( xi − x n)
for j = [1:i-1 i+1:n]
L = L.*(x-X(j))/(X(i)-X(j));
n
( x− xi ) p = p*(X(i)-X(j));
=∏ end
j=1 ( x i − x j ) disp('Lagrange Coefficient: ')
j≠i
disp(i)
n disp('=')
disp(L)
f ( x)= ∑ Li ( x) f ( x i )
i=1
y = y + L*Y(i);
end

BDA34003 Engineering Mathematics IV 27


Waluyo Adi Siswanto ([email protected])
Example 4-4

Four velocity measurement data are shown below :


1.5

1
Predict the velocity when
the displacement 3 mm
0.5
by using Lagrange
velocity (m/s)

0
0 1 2 3 4 5 6 7 a) 2 points
b) 3 points
-0.5
c) 4 points
-1
Plot the functions and
-1.5
discuss your results
displacement (mm)
Use IntpLagrange.m to
x-value v-value
predict the velocity when
0 1
the displacement 3 mm
2 -1
4 -1.2
6 1

BDA34003 Engineering Mathematics IV 28


Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 29
Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 30
Waluyo Adi Siswanto ([email protected])
When using 4 points,
The interpolation function
fits all measured data.

Therefore, when this function


is used to predict the velocity
at displacement 3 mm, the
result is more convincing
then others.

BDA34003 Engineering Mathematics IV 31


Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 32
Waluyo Adi Siswanto ([email protected])
Polynomial Interpolation
Newton Divided Difference
2 points
f ( x 2) The idea in Newton, the predicted function
is expressed from the known previous data

f ( x)= A+ c= f ( x 1 )+ c

f ( x)
From the simple triangular ratio (see page 9)
f ( x1) c
c=
( f ( x 2 )− f ( x 1 ))
( x− x ) 1
( x 2− x 1 )

then Newton writes


A ( f ( x 2 )− f ( x 1 ) )
f ( x)= f ( x )+
1 ( x− x )
1
( x 2− x 1 )
f ( x)=a 0+ a1 ( x− x 1 )

x1 x x2 a 0 , a 1 : Newton difference coefficients

BDA34003 Engineering Mathematics IV 33


Waluyo Adi Siswanto ([email protected])
See the routine below
a0
divided difference
x1 f ( x 1)
a1
( f ( x 2 )− f ( x 1 ))
( x 2− x 1)
x2 f ( x2)

f ( x)=a 0+ a1 ( x− x 1 )

BDA34003 Engineering Mathematics IV 34


Waluyo Adi Siswanto ([email protected])
Polynomial Interpolation
Newton Divided Difference
3 points

For 3 points, Newton extends to quadratic polynomial

f ( x)=a 0+ a1 ( x− x 1 )+ a 2 ( x− x 1 )( x− x 2 )
a 2 can be found for the additional data x 3 → f ( x 3 )=a 0 + a1 ( x 3− x 1 )+ a 2 ( x 3 − x 1 )( x 3 − x 2 )

f ( x 2 )− f ( x 1 )
f ( x 3 )− f ( x 1 )− ( x 3− x 1 )
f ( x 3 )−a0 −a1 ( x 3 − x 1 ) x 2− x 1
a 2= =
( x 3− x 1 )( x 3− x 2 ) ( x 3− x 1 )( x 3 − x 2 )
f ( x 2 )− f ( x 1 )
f ( x 3 )− f ( x 1 )− ( x 3− x 1 )
x 2− x 1
=
( x 3− x 1 )( x 3 − x 2 )

f ( x 3 )− f ( x 2 ) f ( x 2 )− f ( x 1 )

x 3− x 2 x 2 − x1
a 2=
( x3− x1)

BDA34003 Engineering Mathematics IV 35


Waluyo Adi Siswanto ([email protected])
See the routine below
a0
divided difference
x1 f ( x1)
a1
( f ( x 2 )− f ( x 1 )) a2
( x 2− x 1)
f ( x 3 )− f ( x 2 ) f ( x 2 )− f ( x 1 )
x2 f ( x2) −
x3− x2 x 2 − x1
f ( x 3 )− f ( x 2 ) ( x 3− x 1)
x 3− x 2

x3 f ( x3)

f ( x)=a 0+ a1 ( x− x 1 )+ a 2 ( x− x 1 )( x− x 2 )

BDA34003 Engineering Mathematics IV 36


Waluyo Adi Siswanto ([email protected])
Polynomial Interpolation
Newton Divided Difference
4 points

See the routine below


a0
divided difference
x1 A
a1
B− A
=F
x2− x1
a2
x2 B G− F
=I
x 3− x 1 a3
C−B J −I
=G =K
x 3− x 2 x4− x1
x3 C H −G
=J
x4− x2
D−C
=H
x4− x3
x4 D
f ( x)=a 0 +a1 ( x− x 1 )+a 2 ( x− x 1 )( x− x 2 )+a 3 ( x− x 1 )( x− x 2 )( x− x 3 )

BDA34003 Engineering Mathematics IV 37


Waluyo Adi Siswanto ([email protected])
Polynomial Interpolation
Newton Divided Difference
n points
function [a,yi] = IntpNewton(x,y,xi)
% Newton interpolation algorithm
% x,y - row-vectors of (n+1) data values (x,y)
% xi - a row-vector of x-values, where interpolation is to be found (could be a single value)
% yi - a row-vector of interpolated y-values
n = length(x) - 1;
ni = length(xi);
D = ones(n,n);
for k = 1 : n
D(k,1) = (y(k+1)-y(k))/(x(k+1)-x(k));
end
for k = 2 : n
for kk = 1 : (n-k+1)
D(kk,k) = (D(kk+1,k-1) - D(kk,k-1))/(x(kk+k)-x(kk));
end
end
a(1) = y(1);
for k = 2 : (n+1)
a(k) = D(1,k-1);
end
yi = a(n+1)*ones(1,ni);
for k = 1 : n
yi = a(n+1-k)+yi.*(xi-x(n+1-k));
end
BDA34003 Engineering Mathematics IV 38
Waluyo Adi Siswanto ([email protected])
Example 4-5

Available data
a) Prepare the Newton divided difference table
x y
b) Write the predicted equation
2.0 0.510
2.1 0.521 c) Predict the value y when x=2.5
2.4 0.510 d) Plot the predicted equations with
2.6 0.381 the available data

e) Run IntpNewton.m to check the result

BDA34003 Engineering Mathematics IV 39


Waluyo Adi Siswanto ([email protected])
x y first second third
2 0.51
(0.521−0.51)
(2.1−2) (−0.0367−0.11)
2.1 0.521
(0.51−0.521) (2.4−2) (−1.267+0.3667)
(2.4−2.1) (−0.645+0.0367) (2.6−2)
2.4 0.51
(0.381−0.51) (2.6−2.1)
2.6 0.381 (2.6−2.4)

BDA34003 Engineering Mathematics IV 40


Waluyo Adi Siswanto ([email protected])
x y first second third
2 0.51
0.1100
2.1 0.521 -0.3667
-0.0367 -1.4167
2.4 0.51 -1.2167
-0.6450
2.6 0.381

xx = 2.5
y(2.5) = 0.4633

y ( x)=0.51+ 0.11( x−2)−0.3667 ( x−2)( x−2.1)−1.4167( x−2)( x−2.1)( x−2.4)

y (2.5)=0.4633

BDA34003 Engineering Mathematics IV 41


Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 42
Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 43
Waluyo Adi Siswanto ([email protected])
You have learned that n data can be represented by a single polynomial function,
Then you can use it for predicting an arbitrary value in the range.

To predict V(3), you can use


a single function in Lagrange, either

or

or

or using Newton Divided Difference

f ( x)=a 0+ a1 ( x− x 1 )+ a 2 ( x− x 1 )( x− x 2 )

BDA34003 Engineering Mathematics IV 44


Waluyo Adi Siswanto ([email protected])
Sometimes, you have a complicated data, see below
12

10

8
stress y

0
0 50 100 150 200 250 300 350

displacement x

BDA34003 Engineering Mathematics IV 45


Waluyo Adi Siswanto ([email protected])
12
Since you are a smart engineer, you can see the trend and
decide to use different approach

10

8
stress y

0
0 50 100 150 200 250 300 350

displacement x

BDA34003 Engineering Mathematics IV 46


Waluyo Adi Siswanto ([email protected])
12
Predict in this range
Using Lagrange 4 points Predict in this range
Using Linear 2 points
10

8
stress y

Predict in this range


Using Newton 4 points
0
0 50 100 150 200 250 300 350

displacement x

BDA34003 Engineering Mathematics IV 47


Waluyo Adi Siswanto ([email protected])
YES … your approach is very smart and effective,
you can implement it in engineering applications.

Mathematicians,
they have developed a method to generate several
equations depending the range:
Piecewise Polynomial
Let's see how Piecewise Polynomial can help us

BDA34003 Engineering Mathematics IV 48


Waluyo Adi Siswanto ([email protected])
Piecewise Polynomial
understanding piecewise approach

In piecewise approach, the range can be split into several intervals


and in each interval it has its own polynomial.
Therefore, in a range, there are many polynomials not a single polynomial.

f ( x)
s n ( x)
s1 ( x) si ( x) fn
f n−1
f2
f 1+ 1
fi
f1

interval interval interval


1 i n
x
x1 x2 xi x i+ 1 x n−1 xn

BDA34003 Engineering Mathematics IV 49


Waluyo Adi Siswanto ([email protected])
Piecewise Polynomial
Natural Cubic Spline

In cubic spline, each interval of the data is set with each unique polynomial.

This is called
f ( x) s 2 ( x) si+ 1 ( x)
Spline function
s n−1 ( x)
si ( x)
s1 ( x) fn
f n−1
f2
f 1+ 1
fi
f1

interval interval interval


1 i n
x
x1 x2 xi x i+ 1 x n−1 xn

BDA34003 Engineering Mathematics IV 50


Waluyo Adi Siswanto ([email protected])
Piecewise Polynomial
Natural Cubic Spline - Algorithm

Step-1 : Construct divided-difference table

hk =( x k + 1− x k )

f k + 1− f k k =1,2,3 ⋯ , n−1
d k=
hk

b k =6(d k + 1 −d k ) k =1,2,3 ⋯ , n−2

Step-2 : Derive simultaneous equations with unknown mk


k =1,2,3 ⋯ , n−2 m1 =0 mn =0
hk mk + 2(hk + hk + 1 ) mk + 1 + hk + 1 m k + 2 = b k

Step-3 : Solve unknowns m1, m 2, ⋯ , m n−1

BDA34003 Engineering Mathematics IV 51


Waluyo Adi Siswanto ([email protected])
Step-4 : Generate formulas for spline cubic polynomials

i=1,2,3 ⋯ , n−1

S i ( x)=ai ( x− x i )3 + bi ( x− x i )2 + c i (x− x i)+ d i

mi + 1−mi
ai =
6 hi
mi
bi =
2

ci=
( hi )(
f i+ 1 − f i

2 hi mi + hi mi+ 1
6 )
d i= f i

BDA34003 Engineering Mathematics IV 52


Waluyo Adi Siswanto ([email protected])
Piecewise Polynomial
Natural Cubic Spline - Algorithm
function y_eval = IntpNatCspline(x_nodes,y_nodes,x_eval)
% Interpolation Natural Cubic Spline
m=length(x_nodes);
n=m-2;
a=zeros(1,n); b=zeros(1,n); c=zeros(1,n); f=zeros(1,n);
for i=1:n
b(i)=(x_nodes(i+2)-x_nodes(i))/3;
end
for i=2:n
a(i)=(x_nodes(i+1)-x_nodes(i))/6;
end
for i=1:n-1
c(i)=(x_nodes(i+2)-x_nodes(i+1))/6;
end
for i=1:n
f(i)=(y_nodes(i+2)-y_nodes(i+1))/(x_nodes(i+2)-x_nodes(i+1)) ...
-(y_nodes(i+1)-y_nodes(i))/(x_nodes(i+1)-x_nodes(i));
end
[soln, alpha, beta, ier] = tridiag(a,b,c,f,n,0);
M=zeros(m);
M(2:m-1)=soln;
len_x=length(x_eval);
for i=1:len_x
x=x_eval(i);
for j=2:m
if x_nodes(j-1) <= x & x <= x_nodes(j)
y_eval(i)=(M(j-1)*(x_nodes(j)-x)^3+M(j)*(x-x_nodes(j-1))^3)/(6*(x_nodes(j)-x_nodes(j-1))) ...
+(y_nodes(j-1)*(x_nodes(j)-x)+y_nodes(j)*(x-x_nodes(j-1)))/(x_nodes(j)-x_nodes(j-1)) ...
-(x_nodes(j)-x_nodes(j-1))*(M(j-1)*(x_nodes(j)-x)+M(j)*(x-x_nodes(j-1)))/6;
end
end
end

BDA34003 Engineering Mathematics IV 53


Waluyo Adi Siswanto ([email protected])
function [x, alpha, beta, ier] = tridiag(a,b,c,f,n,iflag)
a(1) = 0;
if iflag == 0
% Compute LU factorization of matrix M.
for j=2:n
if b(j-1) == 0
ier = 1;
return
end
a(j) = a(j)/b(j-1);
b(j) = b(j) - a(j)*c(j-1);
end
if b(n) == 0
ier = 1;
return
end
end
% Do forward substitution to solve lower triangular system.
for j=2:n
f(j) = f(j) - a(j)*f(j-1);
end
% Do backward substitution to solve upper triangular system.
f(n) = f(n)/b(n);
for j=n-1:-1:1
f(j) = (f(j) - c(j)*f(j+1))/b(j);
end
% Set output variables.
ier = 0;
x = f;
alpha = a; beta = b;

BDA34003 Engineering Mathematics IV 54


Waluyo Adi Siswanto ([email protected])
Example 4-6

x y
. .
. .
-1 -1
0 0
1 1
2 0
. .
. .

From a set of data, you want to predict


y at x=1.5.

The data that you want to use in the range -1 to 2


and the method is natural cubic spline.
spline

BDA34003 Engineering Mathematics IV 55


Waluyo Adi Siswanto ([email protected])
Step 1: x y h d b

f k + 1− f k
hk =( x k + 1− x k ) d k = b k =6(d k + 1 −d k )
hk

-1 -1
1 1
0 0 0
1 1
1 1 -12
1 -1
2 0

BDA34003 Engineering Mathematics IV 56


Waluyo Adi Siswanto ([email protected])
Step-2 : Derive simultaneous equations with unknown mk

hk mk + 2(hk + hk + 1 ) mk + 1 + hk + 1 m k + 2 = b k

h1 m1+ 2(h1+ h2 ) m2+ h2 m3 = b1


1(0)+ 2 (1+ 1) m2+ 1 m3 = 0 4 m2 + 1 m3 = 0

h2 m2+ 2(h2 + h3 ) m3+ h3 m 4 = b2


1 m2 + 2(1+ 1) m3 + 1(0) = −12 1 m2 + 4 m3 = −12

Step-3 : Solve unknowns m1, m 2, ⋯ , m n−1 of simultaneous equations

m2 =0.8

m3=−3.2

BDA34003 Engineering Mathematics IV 57


Waluyo Adi Siswanto ([email protected])
Step-4 : Generate formulas for spline cubic polynomials
i m h f a b c d
mi+ 1−mi mi
6 hi 2 ( hi )(
f i+ 1 − f i

2 hi mi+ hi mi+ 1
6 ) fi

0 -1
1 1 0.1333 0 0.8667 -1
0.8 0
2 1 -0.6667 0.4 1.2667 0
-3.2 1
3 1 0.5333 -1.6 0.0667 1
0 0
Since 1.5 is in the third interval, therefore use the third polynomial data

S i ( x)=ai ( x− x i )3 + bi ( x− x i )2 + c i (x− x i)+ d i


S 3 (1.5)=0.7

BDA34003 Engineering Mathematics IV 58


Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 59
Waluyo Adi Siswanto ([email protected])
Piecewise Polynomial
Clamped Cubic Spline - Algorithm

Step-1 : Construct divided-difference table

hk =( x k + 1− x k )

f k + 1− f k k =1,2,3 ⋯ , n−1
d k=
hk

b k =6(d k + 1 −d k ) k =1,2,3 ⋯ , n−2

Step-2 : For clamped cubic spline S ' ( x 1 )= f ' ( x 1 ) S ' ( x n )= f ' ( x n )

Derive simultaneous equations with unknown mi


1st data 2 h1 m1+ h1 m2 = 6 ( d 1 − f ' ( x1 ))

hk mk + 2(hk + hk + 1 ) mk + 1 + hk + 1 m k + 2 = b k for k =1,2,3 ⋯ , n−2


end data hn−1 mn−1+ 2 hn−1 m n = 6 ( f ' ( x n )−d n−1 )

BDA34003 Engineering Mathematics IV 60


Waluyo Adi Siswanto ([email protected])
Step-3 : Solve unknowns m1, m 2, ⋯ , m n−1
Step-4 : Generate formulas for spline cubic polynomials

i=1,2,3 ⋯ , n−1

S i ( x)=ai ( x− x i )3 + bi ( x− x i )2 + c i (x− x i)+ d i

mi + 1−mi
ai =
6 hi
mi
bi =
2

ci=
(
f i+ 1 − f i
hi

)(
2 hi mi + hi mi+ 1
6 )
d i= f i

BDA34003 Engineering Mathematics IV 61


Waluyo Adi Siswanto ([email protected])
Example 4-7

x y y'
. .
. .
-1 -1 -1
0 0
1 1
2 0 1
. .
. .

From a set of data, you want to predict


y at x=1.5.

The data that you want to use in the range -1 to 2


and the method is clamped cubic spline.
spline

BDA34003 Engineering Mathematics IV 62


Waluyo Adi Siswanto ([email protected])
Step 1: x y h d b

f k + 1− f k
hk =( x k + 1− x k ) d k = b k =6(d k + 1 −d k )
hk

-1 -1
1 1
0 0 0
1 1
1 1 -12
1 -1
2 0

BDA34003 Engineering Mathematics IV 63


Waluyo Adi Siswanto ([email protected])
Step-2 : Derive simultaneous equations with unknown mk

1st data 2 h1 m1+ h1 m2 = 6 ( d 1 − f ' ( x1 ))

2(1) m1+ (1) m2 = 6 ( 1−(−1)) 2 m1 + m 2 = 12

Intv data hk mk + 2(hk + hk + 1 ) mk + 1 + hk + 1 m k + 2 = b k for k =1,2,3 ⋯ , n−2


h1 m1+ 2(h1+ h2 ) m2+ h2 m3 = b1
1 m1+ 2(1+ 1) m2 + 1 m3 = 0 m1 + 4 m 2 + m3 = 0

h2 m2+ 2(h2 + h3 ) m3+ h3 m 4 = b2


1 m2 + 2(1+ 1) m3 + 1 m4 = −12 m2 + 4 m3+ m4 = −12

end data hn−1 mn−1+ 2 hn−1 m n = 6 ( f ' ( x n )−d n−1 )


1 m3+ 2(1)m 4 = 6 ( 1−(−1)) m3+ 2 m4 = 12

BDA34003 Engineering Mathematics IV 64


Waluyo Adi Siswanto ([email protected])
Step-3 : Solve unknown mk

2 m1 + m2 = 12
m1 + 4 m2 + m3 = 0
Use any method for solving
m2 + 4 m3+ m4 = −12 simultaneous equations
m3+ 2 m4 = 12

m1 =6.1333
m2 =−0.2667
m3=−5.0667
m4 =8.5333

BDA34003 Engineering Mathematics IV 65


Waluyo Adi Siswanto ([email protected])
Step-4 : Generate formulas for spline cubic polynomials
i m h f a b c d
mi+ 1−mi mi
6 hi 2 ( hi )(
f i+ 1 − f i

2 hi mi+ hi mi+ 1
6 ) fi

6.1333 -1
1 1
-0.2667 0
2 1
-5.0667 1
3 1 2.2667 -0.8444 -0.7333 1
8.5333 0
Since 1.5 is in the third interval, therefore use the third polynomial data

S i ( x)=ai ( x− x i )3 + bi ( x− x i )2 + c i (x− x i)+ d i


S 3 (1.5)=0.7056

BDA34003 Engineering Mathematics IV 66


Waluyo Adi Siswanto ([email protected])
Congratulations …

You have completed the Interpolation module.


You know the advantages and the disadvantages of each
method. You should be able to choose an appropriate
method for a particular engineering problem.

BDA34003 Engineering Mathematics IV 67


Waluyo Adi Siswanto ([email protected])
Student Activity

BDA34003 Engineering Mathematics IV 68


Waluyo Adi Siswanto ([email protected])
Activity 4a

The pressure data of compressed air is fluctuated as shown below:

Time (s) Pressure (Pa)


4 2
9 7
16 4
20 1

Predict the pressure when at t=7s by using


a. Newton divided different (4 points)
b. Lagrange (4 points)
c. Natural cubic spline
d. Use Lagrange function in freemat to verify the result

BDA34003 Engineering Mathematics IV 69


Waluyo Adi Siswanto ([email protected])
Activity 4b

An experimental stress data along beam length is shown below (without unit).
The complete data at location x between 100 and 200 are documented in the table
12

10

8
stress y

0
0 50 100 150 200 250 300 350

displacement x

BDA34003 Engineering Mathematics IV 70


Waluyo Adi Siswanto ([email protected])
Use Smath to do the following tasks

a) By using Lagrange method, predict the stress value at x =175,


- Lagrange 3 points
- Lagrange 5 points
- Lagrange 7 points

b) Plot the functions generated by the Lagrange method (3, 5 and 7 points) and the
Experimental data in the same plot

c) Draw your conclusion based on your results

BDA34003 Engineering Mathematics IV 71


Waluyo Adi Siswanto ([email protected])
BDA34003 Engineering Mathematics IV 72
Waluyo Adi Siswanto ([email protected])

You might also like