Professional Documents
Culture Documents
تحليلات عددية
تحليلات عددية
Numerical Analysis
• Contact details:
❖Email: 150082@uotechnology.edu.iq
❖
Lecture Content
• Introduction
• Use of numerical methods in a variety of petroleum engineering
problems
• The Purpose of Numerical methods in Petroleum Engineering
• Noncomputer Methods
Introduction
Numerical Analysis
Lecture 2: Analytical and Numerical methods Semester 1 (2020-2021)
• If the downward force is assigned a positive sign, the second law can
be used to formulate the force due to gravity, as
FD=mg (6)
• Where g = the gravitational constant, or the acceleration due to
gravity, which is approximately equal to 9.8 m/s2.
Mathematical Models
• Air resistance can be formulated in a variety of ways. A simple
approach is to assume that it is linearly proportional to velocity and
acts in an upward direction, as in
• FU=-CV (7)
• where c = a proportionality constant called the drag coefficient (kg/s).
Thus, the greater the fall velocity, the greater the upward force due to
air resistance. The parameter c accounts for properties of the falling
object, such as shape or surface roughness, that affect air resistance.
For the present case, c might be a function of the type of jumpsuit or
the orientation used by the parachutist during free-fall.
• The net force is the difference between the downward and upward
force.
Mathematical Models
• Therefore,
• Eqs. (4) through (7) can be combined to yield:
(8)
• (9)
Mathematical Models
• Equation (9) is a model that relates the acceleration of a falling object
to the forces acting on it. It is a differential equation because it is
written in terms of the differential rate of change (dv/dt) of the
variable that we are interested in predicting.
• However, in contrast to the solution of Newton’s second law in Eq. (3),
the exact solution of Eq. (9) for the velocity of the falling parachutist
cannot be obtained using simple algebraic manipulation.
• Rather, more advanced techniques such as those of calculus, must be
applied to obtain an exact or analytical solution.
Mathematical Models
• For example, if the parachutist is initially at rest (v = 0 at t = 0), calculus
can be used to solve Eq. (9) for:
(10)
• Note that Eq. (10) is cast in the general form of Eq. (1), where v(t) = the
dependent variable, t = the independent variable, c and m =
parameters, and g = the forcing function.
Analytical Solution to the Falling
Parachutist Problem
Example 1: A parachutist of mass 68.1 kg jumps out of a stationary hot
air balloon. Plot the velocity-time relationship and compute the treminal
velocity. The drag coefficient is equal to 12.5 kg/s.
Solution: Inserting the parameters into Eq. (10) yields:
For t = (0):
V(0) = 53.39 (1-e-0.18355 × 0) = 0
For t = (2):
V(2) = 53.39 (1-e-0.18355 × 2) = 16.405
For t = (4):
V(4) = 53.39 (1-e-0.18355 × 4) = 27.769
Follwing the same calculation for t = 6,8, 10, 12, 14, 16, 20, … ∞) as
follows:
Analytical Solution to the Falling
Parachutist Problem
Example 1
t, s V, m/s t, s V, m/s
0 0 18 51.429
2 16.405 20 52.032
4 27.769 22 52.449
6 35.642 24 52.738
8 41.095 26 52.939
10 44.873 28 53.077
12 47.49 30 53.174
14 49.303 32 53.24
16 50.559 ∞ 53.39
Analytical Solution to the Falling
Parachutist Problem
Example 1
Analytical Solution to the Falling
Parachutist Problem
Example 1
• According to the model, the parachutist accelerates rapidly (above Figure).
A velocity of 44.87 m/s (100.4 mi/h) is attained after 10 s.
• The terminal velocity is the constant velocity that is reached after a
sufficiently long time (in this example):
Terminal velocity = 53.39 m/s.
• This velocity is constant because, eventually, the force of gravity will be in
balance with the air resistance. Thus, the net force is zero and acceleration
has ceased. Equation (10) is called an analytical, or exact, solution because it
exactly satisfies the original differential equation. Unfortunately, there are
many mathematical models that cannot be solved exactly. In many of these
cases, the only alternative is to develop a numerical solution that
approximates the exact solution.
Numerical Solutions
• As mentioned previously, numerical methods are those in which the
mathematical Problem is reformulated so it can be solved by
arithmetic operations.
• This can be illustrated for Newton’s second law by realizing that the
time rate of change of velocity can be approximated by:
(11)
(12)
Numerical Solutions
• Notice that the term in brackets is the right-hand side of the differential
equation itself [Eq. (9)]. That is, it provides a means to compute the rate of
change or slope of v.
• Thus, the differential equation has been transformed into an equation that
can be used to determine the velocity algebraically at ti+1 using the slope and
previous values of v and t.
• If you are given an initial value for velocity at some time ti , you can easily
compute velocity at a later time ti+1. This new value of velocity at ti+1 can in
turn be employed to extend the computation to velocity at ti+2 and so on.
Thus, at any time along the way,
New value = old value + slope × step size
• Note that this approach is formally called Euler’s method.
Numerical Solution to the Falling
Parachutist Problem
Example 2: A parachutist of mass 68.1 kg jumps out of a stationary hot air
balloon. Use the numerical method to plot the velocity-time relationship and
compute the treminal velocity . The drag coefficient is equal to 12.5 kg/s.
Employ a step size of 2 s for the calculation.
• At the start of the computation (ti = 0), the velocity of the parachutist
is zero (v0 = 0).
• Using this information and the given parameter values, Eq. (12) can be
used to compute velocity at ti+1 = 2 s:
Numerical Solution to the Falling
Parachutist Problem
Example 2:
• Thus, velocity for the first interval (t = 0 to 2 s):
or
• Increases = decreases
Flow in pipe 1+ Flow in pipe 2- Flow out pipe 3- Flow out pipe 4= 0
The volume flow rate through a pipe is given by Q = vA, where v is the
average velocity and A is the cross-sectional area.
For the above junction, the balance can be used to compute that the flow out of the fourth pipe as follows:
Q1 in = Q2 out + Q3 out
Q1 in - Q2 out - Q3 out = 0
40-20- Q3 out = 0
Now, the following equation is used to calculate the area in the pipe 3
For the falling parachutist, steady-state conditions would correspond to the case where the net force was zero, or
[Eq. (8) with dv/dt = 0]
mg = cv (15)
Thus, at steady state, the downward and upward forces are in balance, and Eq. (1.15) can be solved for the
terminal velocity
v = mg/c
Although Eqs. (13) and (14) might appear simple; they represent the two fundamental ways that conservation laws
are employed in engineering.
University of Technology
Petroleum Technology Department
Numerical Analysis
Lecture 3: Numerical solution of differential equations
Semester 1 (2020-2021)
CISE301_Topic8L1 1
Learning Objectives
2
Learning Objectives
3
Derivatives
Derivatives
Partial Derivatives
Ordinary Derivatives
dv u
dt y
u is a function of
v is a function of one
more than one
independent variable
independent variable
4
Differential Equations
Differential
Equations
+ 6tv = 1 − 2 =0
dt 2 y 2
x
involve one or more involve one or more
Ordinary derivatives of partial derivatives of
unknown functions unknown functions
5
Differential Equations
6
Ordinary Differential Equations
7
Example of ODE:
Model of Falling Parachutist
The velocity of a falling parachutist is given by:
dv c
= 9.8 − v
dt M
M : mass
c : drag coefficien t
v : velocity
8
Definitions
Ordinary
differential
equation
dv c
= 9.8 − v
dt M
9
Definitions (Cont.)
dv c
= 9.8 − v (Dependent
dt M variable)
unknown
function to be
determined
10
Definitions (Cont.)
dv c
= 9.8 − v
dt M
(independent variable)
the variable with respect to which
other variables are differentiated
11
Order of a Differential Equation
The order of an ordinary differential equation is the order
of the highest order derivative.
Examples :
dx(t )
− x(t ) = et First order ODE
dt
d 2 x(t ) dx(t )
2
−5 + 2 x(t ) = cos(t ) Second order ODE
dt dt
3
d x(t )
2
dx(t )
2
− + 2 x 4 (t ) = 1 Second order ODE
dt dt
12
Order of a Differential Equation
13
Solution of a Differential Equation
A solution to a differential equation is a function that
satisfies the equation.
dx(t )
+ x(t ) = 0 Solution
Proof :
Proof :
x(t ) = e −t
dt dx(t )
dx(t )
= −e −t
= −e −t
dt
dx(t )
+ x(t ) = −e −t + e −t = 0
dt
dt
dx(t )
+ x(t ) = −e −t + e −t = 0
dt
14
Solution of a Differential Equation
y = xe x
is a solution of d2y
2
dy
−2 + y = 0
dx dx
• solution
dy
y = xe x
= xe x + e x
dx
d2y d2y
= xe x
+ e x
+ e x
= xe x
+ 2e x
dx 2 dx 2
xe + 2e − 2 xe − 2e + xe = 0
x x x x x
15
Solutions of Ordinary Differential
Equations
Homework
x(t ) = cos(2t )
is a solution to the ODE
2
d x(t )
2
+ 4 x (t ) = 0
dt
16
Linear ODE
17
Linear ODE
18
Linear ODE
19
Linear ODE
20
Linear ODE
Examples :
dx(t )
− x(t ) = et
dt Linear ODE
d 2 x(t ) dx(t )
2
− 5 + 2t 2
x(t ) = cos(t ) Linear ODE
dt dt
3
d x(t ) dx(t )
2
Non-linear ODE
2
− + x(t ) = 1
dt dt
21
Nonlinear ODE
An ODE is linear if
The unknown function and its derivatives appear to power one
No product of the unknown function and/or its derivatives
Examples of nonlinear ODE :
dx(t )
− cos( x(t )) = 1
dt
d 2 x(t ) dx(t )
2
− 5 x(t ) = 2
dt dt
22
Auxiliary Conditions
Auxiliary Conditions
23
Boundary-Value and
Initial value Problems
Initial-Value Problems Boundary-Value Problems
24
Classification of ODEs
25
Analytical Solutions
• Analytical Solutions to ODEs are available for linear ODEs and special
classes of nonlinear differential equations.
26
Numerical Solutions
27
Classification of the Methods
Numerical Methods
for Solving ODE
28
Taylor Series Method
The problem to be solved is a first order ODE:
dy( x)
= f ( x, y ), y( x0 ) = y0
dx
Estimates of the solution at different base points:
29
Taylor Series Expansion
Truncated Taylor Series Expansion
h k d k y
n
y ( x0 + h )
k ! dx k
k =0
x = x 00 , y =
= y
y 00
dy h 22 d 22 y h nn dd nn yy
y ( x0 ) + h + 22
+ ... +
dx xy==xy00 , 2! dx xx==xx00,, dxnn xx==xx00,,
nn!! dx
00 yy==yy00 yy==yy00
30
Euler Method
• Only the constant term and linear term are used in the Euler method.
• The error due to the use of the truncated Taylor series is of order
O(h2).
31
First Order Taylor Series Method
(Euler Method)
dydy
yy( x( 0x0++h)h=
) =y (yx(0x)0+) h+ h + O+(O
2
h 2()h )
x = xx0 ,= x0 ,
dxdx
y = yy = y
0 0
Notation
Notation: :
xxn n==x0x0+ +nhnh
, , yny=n = y ( yxn( ),
xn ),
dydy = f ( x , y )
dx x = x ,
= f (i x ,i y )
i i
x =
dxy = y i
ix ,
y =i yi
Euler
EulerMethod
Method
yyi +1 ==yiy+ +h hf (fx(i ,xy,i )y )
i +1 i i i
32
Euler Method
Problem :
Given the first order ODE : y ( x) = f ( x, y )
with the initial condition : y0 = y ( x0 )
Determine : yi = y ( x0 + ih ) for i = 1,2,...
Euler M ethod :
y0 = y ( x0 )
yi +1 = yi + h f ( xi , yi ) for i = 1,2,...
33
Interpretation of Euler Method
y2
y1
y0
x0 x1 x2 x
34
Interpretation of Euler Method
Slope=f(x0,y0)
y1
y1=y0+hf(x0,y0)
hf(x0,y0)
y0
x0 x1 x2 x
h
35
Interpretation of Euler Method
y2 y2=y1+hf(x1,y1)
Slope=f(x1,y1)
hf(x1,y1)
Slope=f(x0,y0)
y1 y1=y0+hf(x0,y0)
hf(x0,y0)
y0
x0 x1 x2 x
h h
36
Example
Use Euler method to solve the ODE:
dy
= 1+ x ,
2
y(1) = −4
dx
to determine y(1.01), y(1.02) and y(1.03).
37
Example
f ( x, y) = 1 + x , 2
x0 = 1, y0 = −4 , h = 0.01
Euler M ethod
yi +1 = yi + h f ( xi , yi )
Step 3 : (
y3 = y2 + h f ( x2 , y2 ) = −3.9598 + 0.01 1 + (1.02) = −3.9394
2
)
38
Example
f ( x, y) = 1 + x ,
2
x0 = 1, y0 = −4 , h = 0.01
Summary of the result:
i xi yi
0 1.00 -4.00
1 1.01 -3.98
2 1.02 -3.9595
3 1.03 -3.9394
39
Example
f ( x, y) = 1 + x ,
2
x0 = 1, y0 = −4 , h = 0.01
Comparison with true value:
i xi yi True value of yi
0 1.00 -4.00 -4.00
1 1.01 -3.98 -3.97990
2 1.02 -3.9595 -3.95959
3 1.03 -3.9394 -3.93909
40
Example
f ( x, y) = 1 + x ,
2
x0 = 1, y0 = −4 , h = 0.01
A graph of the
solution of the
ODE for
1<x<2
41
Types of Errors
• Local truncation error:
Error due to the use of truncated Taylor series to compute x(t+h) in one
step.
• Global Truncation error:
Accumulated truncation over many steps.
• Round off error:
Error due to finite number of bits used in representation of numbers. This
error could be accumulated and magnified in succeeding steps.
42
Second Order Taylor Series Methods
dy ( x)
Given = f ( y, x), y ( x0 ) = y0
dx
Second order Taylor Series method
2 2
dy h d y
yi +1 = yi + h + 2
+ O(h )
3
dx 2! dx
2
d y
2
needs to be derived analytically.
dx
43
Third Order Taylor Series Methods
dy ( x)
Given = f ( y, x), y ( x0 ) = y0
dx
Third order Taylor Series method
2 2 3 3
dy h d y h d y
yi +1 = yi + h + 2
+ 3
+ O(h )
4
dx 2! dx 3! dx
2 3
d y d y
2
and 3 need to be derived analytically.
dx dx
44
High Order Taylor Series Methods
dy ( x)
Given = f ( y, x), y ( x0 ) = y0
dx
th
n order Taylor Series method
dy h 2 d 2 y hn d n y n +1
yi +1 = yi + h + 2
+ .... + n
+ O(h )
dx 2! dx n! dx
2 3 n
d y d y d y
2
, 3 ,....., n need to be derived analytically.
dx dx dx
45
Higher Order Taylor Series Methods
• High order Taylor series methods are more accurate than Euler
method.
• But, the 2nd, 3rd, and higher order derivatives need to be derived
analytically which may not be easy.
46
Example
Second order Taylor Series Method
Use Second order Taylor Series method to solve :
dx
+ 2 x 2 + t = 1, x(0) = 1, use h = 0.01
dt
d 2 x(t )
What is : 2
?
dt
47
Example
Use Second order Taylor Series method to solve :
dx
+ 2 x + t = 1, x(0) = 1,
2
use h = 0.01
dt
dx
= 1− 2x2 − t
dt
d 2 x(t ) dx
2
= 0 − 4 x − 1 = − 4 x (1 − 2 x 2
− t ) −1
dt dt
2
h
xi +1 = xi + h(1 − 2 xi − ti ) + ( − 1 − 4 xi (1 − 2 xi − ti ))
2 2
48
Example
f (t , x) = 1 − 2 x 2 − t , t0 = 0, x0 = 1, h = 0.01
2
h
xi +1 = xi + h(1 − 2 xi − ti ) + ( − 1 − 4 xi (1 − 2 xi − ti ))
2 2
2
Step 1 :
x1 = 1 + 0.01(1 − 2(1) 2
− 0) +
(0.01)
2
(−1 − 4(1)(1 − 2 − 0)) = 0.9901
2
Step 2 :
49
Example
f (t , x) = 1 − 2 x 2 − t , t0 = 0, x0 = 1, h = 0.01
Summary of the results:
i ti xi Homework:
Calculate the True
0 0.00 1 value of xi.
1 0.01 0.9901
2 0.02 0.9807
3 0.03 0.9716
50
University of Technology
Petroleum Technology Department
Numerical Analysis
Lecture 4: Numerical solution of differential equations (Part two)
Semester 1 (2020-2021)
1
Learning Objectives
To be able to solve first order differential
equations using the Midpoint Method.
To be able to solve first order differential
equations using the Heun’s Predictor
Corrector Method.
Convert a single (or a system of) high order
ODE to a system of first order ODEs.
Use the methods discussed earlier in this
topic to solve systems of first order ODEs.
2
Topics Lecture 4
•Review Euler Method
• Heun’s Method
• Midpoint Method
•Solution of a system of first order ODEs.
•Conversion of a high order ODE to a system of
first order ODEs.
•Conversion of a system of high order ODEs to
a system of first order ODEs.
•Use different methods to solve systems of first
order ODEs.
3
Euler Method
Problem Euler M ethod
y ( x) f ( x, y ) y0 y ( x0 )
y ( x0 ) y0 yi 1 yi h f ( xi , yi )
for i 1,2,...
2
Local Truncation Error O(h )
Global Truncation Error O(h)
4
Types of Errors
• Local truncation error:
Error due to the use of truncated Taylor series to compute x(t+h) in one
step.
• Global Truncation error:
Accumulated truncation over many steps.
5
Introduction
Problem to be solved is a first order ODE :
y ( x) f ( x, y ), y( x0 ) y0
The methods proposed in this lesson
have the general form:
yi 1 yi h
For the case of Euler: f ( xi , yi )
Different forms of will be used for
the Midpoint and Heun’s Methods.
6
Midpoint Method
Problem M idpoint M ethod
y ( x) f ( x, y ) y0 y ( x0 )
h
y ( x0 ) y0 y 1 yi f ( xi , yi )
i 2
2
yi 1 yi h f ( x 1 ,y 1 )
i i
2 2
8
Midpoint Method
( xi , yi )
x0 x 1 xi 1
i
2
h
y 1 yi f ( xi , yi ) , yi 1 yi h f ( x 1 ,y 1 )
i 2 i i
2 2 2
9
Midpoint Method
slope f ( xi , yi )
( xi , yi )
x0 x 1 xi 1
i
2
h
y 1 yi f ( xi , yi ) , yi 1 yi h f ( x 1 ,y 1 )
i 2 i i
2 2 2
10
Midpoint Method
(x ,y )
slope f ( xi , yi )
1 1
i i
2 2
( xi , yi )
x0 x 1 xi 1
i
2
h
y 1 yi f ( xi , yi ) , yi 1 yi h f ( x 1 ,y 1 )
i 2 i i
2 2 2
11
Midpoint Method
slope f ( x 1 ,y 1 )
i i
(x 1 ,y 1 ) 2 2
i i
2 2
( xi , yi )
x0 x 1 xi 1
i
2
h
y 1 yi f ( xi , yi ) , yi 1 yi h f ( x 1 ,y 1 )
i 2 i i
2 2 2
12
Midpoint Method
slope f ( x 1 ,y 1 )
i i
(x 1 ,y 1 ) 2 2
i i
2 2
( xi , yi )
x0 x 1 xi 1
i
2
h
y 1 yi f ( xi , yi ) , yi 1 yi h f ( x 1 ,y 1 )
i 2 i i
2 2 2
13
Example 1
14
Example 1
Problem : f ( x, y ) 1 x 2 y , y0 y (0) 1, h 0.1
Step 1 :
h
y 1 y0 f ( x0 , y0 ) 1 0.05(1 0 1) 1.1
0 2
2
16
Heun’s Predictor Corrector Method
Problem Heun ' s M ethod
y ( x ) f ( x, y ) y0 y ( x0 )
y ( x0 ) y0 Predictor : yi01 yi h f ( xi , yi )
Corrector : yi11 yi
h
2
f ( xi , yi ) f ( xi 1 , yi01 )
17
Heun’s Predictor Corrector
(Prediction)
( xi 1 , yi01 )
( xi , yi )
xi xi 1
Prediction 0
yi 1 yi h f ( xi , yi )
18
Heun’s Predictor Corrector
(Prediction)
slope f ( xi , yi ) ( xi 1 , yi01 )
( xi , yi )
xi xi 1
Prediction y 0
i 1 yi h f ( xi , yi )
19
Heun’s Predictor Corrector
(Correction)
f ( xi , yi ) f ( xi 1 , yi01 )
slope
2
( xi 1 , yi01 )
( xi , yi ) ( xi 1 , yi11 )
xi xi 1
y1
i 1 yi
h
2
f ( xi , yi ) f ( xi 1 , yi01 )
20
Example 2
Use the Heun' s M ethod to solve the ODE
y ( x) 1 x y
2
y (0) 1
Use h 0.1. One correction only
Determine y(0.1) and y(0.2)
21
Example 2
Problem : f ( x, y ) 1 y x 2 , y0 y ( x0 ) 1, h 0.1
Step 1 :
Predictor : y10 y0 h f ( x0 , y0 ) 1 0.1( 2) 1.2
Corrector : y11 y0
h
2
f ( x0 , y0 ) f ( x1 , y10 )
1 (0.1 / 2)( 2 (1 1.2 (0.12 )))
1 (0.1 / 2)( 2 2.21) 1.2105
Step 2 :
Predictor : y 20 y1 h f ( x1 , y1 ) 1.4326
Corrector : y12 y1
h
2
f ( x1 , y1 ) f ( x2 , y 20 )
1.2105 (0.1 / 2)( 2.2205 (1 1.4326 (0.2 2 )))
1.2105 (0.1 / 2)(2.2205 2.4726) 1.4452
22
Summary
Euler, Midpoint and Heun’s methods are
similar in the following sense:
yi 1 yi h slope
Different methods use different estimates of
the slope.
Both Midpoint and Heun’s methods are
comparable in accuracy to the second
order Taylor series method.
23
Comparison
Method Local
truncation
Global
truncation
error error
yi 1 yi h f ( x 1 ,y 1 )
i i
2 2
24
Solving a System of First Order ODEs
Methods discussed earlier such as Euler,
Midpoint and Heun’s method are used to
solve first order ordinary differential
equations.
The same formulas will be used to solve
a system of first order ODEs.
In this case, the differential equation is a
vector equation and the dependent variable is
a vector variable.
25
Euler Method for Solving a System of
First Order ODEs
Recall Euler method for solving a first order ODE:
dy ( x)
Given f ( y, x), y (a) ya
dx
Euler Method :
y (a h) y (a ) h f ( y (a), a)
y (a 2h) y (a h) h f ( y (a h), a h)
y (a 3h) y (a 2h) h f ( y (a 2h), a 2h)
26
Example - Euler Method
Euler method to solve a system of n first order ODEs.
f1 (Y , x) y1 ( x0 )
f (Y , x) y ( x )
d Y ( x)
Given F (Y , x) 2 , Y ( x ) 2 0
dx ... 0
...
f n (Y , x) yn ( x0 )
Euler Method :
Y ( x0 h) Y ( x0 ) h F (Y ( x0 ), x0 )
Y ( x0 2h) Y ( x0 h) h F (Y ( x0 h), x0 h)
Y ( x0 3h) Y ( x0 2h) h F (Y ( x0 2h), x0 2h)
27
Solving a System of n First Order ODEs
Exactly the same y1 ( x)
y ( x)
formula is used but Y ( x) 2 Y is n 1 vector
the scalar variables ...
and functions are n
y ( x )
replaced by vector d y1
variables and vector dx f1 (Y , x)
values functions. d y
d Y ( x) 2 f 2 (Y , x)
dx F (Y , x)
Y is a vector of dx ...
...
length n. n n
d y f (Y , x )
F(Y,x) is a vector dx
valued function.
28
Example :
Euler method for solving a system of first order ODEs.
y1 ( x) y2 y1 (0) 1
y ( x) 1 y F (Y , x ), Y ( 0) y ( 0) 1
2 1 2
Two steps of Euler Method with h 0.1
STEP 1 :
Y ( x0 h) Y ( x0 ) h F (Y ( x0 ), x0 )
Y (0 h) Y (0) h F (Y (0),0)
y1 ( x0 h) y1 (0.1) y1 (0) y2 (0) 1 0.1 0.9
y ( x h) y (0.1) y (0) 0.11 y (0) 1 0.1(1 1) 1.2
2 0 2 2 1
STEP 2 :
Y ( x0 2h) Y ( x0 h) h F (Y ( x0 h), x0 h)
y1 ( x0 2h) y1 (0 2(0.1) y1 ( x0 h) y2 ( x0 h) 0.9 0.1(1.2) 0.78
y ( x 2h) y (0 2(0.2) y ( x h) 0 .11 y ( x h) 1.2 0.1(1 (0.9) 1.39
2 0 2 2 0 1 0
STEP 3 :
Y ( x0 3h) Y ( x0 2h) h F (Y ( x0 2h), x0 2h)
y1 ( x0 3h) y1 (0 3(0.1) y1 ( x0 2h) y2 ( x0 2h) 0.78 0.1(1.39) 0.64
y ( x 3h) y (0 3(0.2) y ( x 2h) 0 .11 y ( x 2h) 1.39 0.1(1 (0.78) 1.57
2 0 2 2 0 1 0
29
Methods for Solving a System of First Order ODEs
30
High Order ODEs
How do solve a second order ODE?
x 3x 6 x 1
How do solve high order ODEs?
31
The General Approach to Solve ODEs
High order ODE Convert System of first order ODEs Solve
z1 z2
z 1 3z 6 z ,
x 3x 6 x 1 Convert
2 2 1
Solve
x (0) 1; x(0) 4 4
Z (0)
1
Second order ODE Two first order ODEs
32
Conversion Procedure
High order ODE Convert System of first order ODEs Solve
34
Example of Converting a High Order
ODE to First Order ODEs
Convert x 3 x 6 x 1, x (0) 1; x(0) 4
to a systemof first order ODEs
35
Example of Converting a High Order
ODE to First Order ODEs
old new Initial Equation
name name cond.
x z1 4 z1 z 2
x z2 1 z2 1 3 z 2 6 z1
z1 z2 4
z 1 3z 6 z , Z (0) 1
2 2 1
36
Example of Converting a High Order
ODE to First Order ODEs
Convert
x 2 x 7 x 8 x 0
x(0) 9, x (0) 1; x(0) 4
1
z z 1
Z F (Z ) 2
, Z (0)
z2 2 2 z 2 8 z1 2
42
Solution of a Second Order ODE
z2 1
F (Z ) , Z (0) , h 0.1
2 2 z 2 8 z1 2
Z (0 0.1) Z (0) hF ( Z (0))
1 2 0.8
0.1
2 2 2 ( 2) 8(1) 2.2
Z (0 0.2) Z (0.1) hF ( Z (0.1))
0.8 2.2 0.58
0.1
2.2 2 2( 2 . 2) 8( 0.8) 2.2
43
Summary
Formulas used in solving a first order ODE
are used to solve systems of first order
ODEs.
Instead of scalar variables and functions, we
have vector variables and vector functions.
44
University of Technology
Petroleum Technology Department
Numerical Analysis
Lecture 5:Runge-Kutta Methods Semester 1 (2020-2021)
1
Learning Objectives
• Runge-Kutta Methods
• To understand the motivation for using Runge Kutta method and the
basic idea used in deriving them.
• Use Runge Kutta of order 2 to solve ODEs.
• Boundary Value Problem
• Examples
2
Simplified method for the computation of numerical solution
of system
8
Second Order Runge-Kutta Method
K1 h f ( xi , yi )
K 2 h f ( xi h, yi K1 )
yi 1 yi w1K1 w2 K 2
Problem :
Find , , w1, w2
such that yi 1 is as accurate as possible.
9
2nd Order Runge-Kutta Methods
K1 h f ( xi , yi )
K 2 h f ( xi h, yi K1 )
yi 1 yi w1K1 w2 K 2
2
h
2
yi 1 yi K1 K 2 yi f ( xi , yi ) f ( xi 1, yi 1 )
1 0
This is Heun ' s Method with a Single Corrector
11
Choosing , , w1 and w2
1 1
Choosing then , w1 0, w2 1
2 2
Second Order Runge - Kutta method becomes :
K1 h f ( xi , yi )
h K1
K 2 h f ( xi , yi )
2 2
h K1
yi 1 yi K 2 yi h f ( xi , yi )
2 2
This is the M idpoint M ethod
12
2nd Order Runge-Kutta Methods
Alternative Formulas
1 1
w2 , w2 , w1 w2 1
2 2
1 1
Pick any nonzero number : , w2 , w1 1
2 2
STEP 1 :
K1 h f (t0 1, x0 4) 0.01(1 x02 t03 ) 0.18
K 2 h f (t0 h, x0 K1 )
0.01(1 ( x0 0.18)2 (t0 .01)3 ) 0.1662
x (1 0.01) x (1) K1 K 2 / 2
4 (0.18 0.1662) / 2 3.8269
14
Second order Runge-Kutta Method
Example
STEP 2
15
x (t ) 1 x (t ) t , x(1) 4,
2 3
CISE301_Topic8L4&5 16
2nd Order Runge-Kutta RK2
yi 1 yi k1 k2
h
2
Local error is O ( h 3 ) and global error is O (h 2 )
17
Higher-Order Runge-Kutta
18
3rd Order Runge-Kutta RK3
Know as RK3
k1 f ( xi , yi )
h 1
k2 f ( xi , yi k1h )
2 2
k3 f ( xi h, yi k1h 2k2h )
yi 1 yi k1 4k2 k3
h
6
4 3
Local error is O (h ) and Global error is O ( h )
19
4th Order Runge-Kutta RK4
k1 f ( xi , yi )
h 1
k2 f ( xi , yi k1h )
2 2
h 1
k3 f ( xi , yi k2h )
2 2
k4 f ( xi h, yi k3h )
dy
1 y x 2
dx
y (0) 0.5
h 0.2
Use RK 4 to compute y (0.2) and y (0.4)
22
Example: RK4
Problem :
dy
1 y x2 , y (0) 0.5
dx
Use RK 4 to find y (0.2), y (0.4)
23
4th Order Runge-Kutta RK4
k1 f ( xi , yi )
h 1
k2 f ( xi , yi k1h )
2 2
h 1
k3 f ( xi , yi k2h )
2 2
k4 f ( xi h, yi k3h )
y1 y0
h
k1 2k2 2k3 k4 0.8293
6
25
Example: RK4
Problem :
h 0.2
dy
1 y x2 , y (0) 0.5 f ( x, y ) 1 y x 2
dx
x1 0.2, y1 0.8293
Use RK 4 to find y (0.2), y (0.4)
k1 f ( x1, y1 ) 1.7893
1 1
k2 f ( x1 h, y1 k1h ) 1.9182
2 2
Step 2
1 1
k3 f ( x1 h, y1 k2h ) 1.9311
2 2
k4 f ( x1 h, y1 k3h ) 2.0555
y2 y1
0.2
k1 2k2 2k3 k4 1.2141
6
26
Example: RK4
Problem :
dy
1 y x2 , y (0) 0.5
dx
Use RK 4 to find y (0.2), y (0.4)
29
Example :
RK2 method for solving a system of first order ODEs
30
Example :
RK2 method for solving a system of first order ODEs
31
32
33
Example :
RK2 method for solving a system of first order ODEs
y1 ( x) y2 y1 (0) 1
y ( x) 1 y F (Y , x), Y (0) y (0) 1
2 1 2
Two steps of second order Runge Kutta M ethod with h 0.1
STEP 1 :
y2 (0) 0.1
K1 h F (Y (0),0) 0.1
1 y1 (0) 0.2
y2 (0) 0.2 0.12
K 2 h F (Y (0) K1,0 h) 0.1
1 ( y1 ( 0) 0.1) 0. 19
Y (0 h) Y (0) 0.5( K1 K 2)
y1 (0.1) 1 1 0.1 0.12 0.89
y (0.1) 1 2 0.2 0.19 1.195
2
34
y1 ( x) y2 y1 (0) 1
y ( x) 1 y F (Y , x), Y (0) y (0) 1
2 1 2
STEP 2 :
y2 (0.1) 0.1195
K1 h F (Y (0.1),0.1) 0.1
1 y1 ( 0. 1) 0. 1890
y2 (0.1) 0.189 0.1384
K 2 h F (Y (0.1) K1,0.1 h) 0.1
1 ( y1 ( 0.1) 0.1195) 0.1771
Y (0.1 h) Y (0.1) 0.5( K1 K 2)
y1 (0.2) 0.89 1 0.1195 0.1384 0.7611
y (0.2) 1.195 2 0.1890 0.1771 1.3780
2
35
4th Order Runge-Kutta RK4
k1 f ( xi , yi )
h 1
k2 f ( xi , yi k1h )
2 2
h 1
k3 f ( xi , yi k2h )
2 2
k4 f ( xi h, yi k3h )
2 t
2 t
x 2 x x e x 2 x x e
x(0) 1, x (0) 2.5 x(0) 1, x(2) 1.5
same different
39
Shooting Method
40
University of Technology
Petroleum Technology Department
Numerical Analysis
Lecture 6: Solution of Systems of Linear Equation using Numerical
Methods Semester 1 (2020-2021)
۱
Learning Objectives
• Vectors
• Matrices
• Systems of Linear Equations
• Cramer’s Rule
• Naive Gaussian Elimination
• Problems with Naive Gaussian Elimination
• Gauss-Jordan Method
• Examples
۲
VECTORS
Vector : a one dimensional array of numbers
Examples :
2
row vector [1 4 2] column vector
1
1 0 0 0
0 1 0 0
Identity vectors e1 = , e2 = , e3 = , e4 =
0 0 1 0
0 0 0 1
۳
MATRICES
Matrix : a two dimensional array of numbers
Examples :
0 0 0 1 0
zero matrix identity matrix
0 0 0 0 1
1 0 0 0 1 2 0 0
0 4 0 0 3 4 1 0
diagonal , Tridiagonal
0 0 0 0 0 1 4 1
0 0 0 6 0 0 2 1
٤
MATRICES
Examples :
1 2 1 3
2 1 − 1 0 4 1 0
symmetric 1 0 5 , upper triangular
0 0 4 1
− 1 5 4
0 0 0 1
٥
Determinant of a MATRICES
٦
Determinant of a MATRICES
۷
Determinant of a MATRICES
Defined for square matrices only
Examples :
2 3 − 1
det 1 0 5 = 2
0 5 3 -1 3 -1
-1 -1
5 4 5 4 0 5
− 1 5 4
= 2(−25) − 1(12 + 5) − 1(15 − 0) = −82
۸
Systems of Linear Equations
A system of linear equations can be presented
in different forms
2 x1 + 4 x2 − 3x3 = 3 2 4 − 3 x1 3
2.5 x1 − x2 + 3x3 = 5 ⇔ 2.5 − 1 3 x2 = 5
x1 − 6 x3 = 7 1 0 − 6 x3 7
Standard form Matrix form
۹
Cramer’s Rule
۱۰
Cramer’s Rule
۱۱
Cramer’s Rule
۱۲
Cramer’s Rule
۱۳
Cramer’s Rule is Not Practical
۱٤
Naive Gaussian Elimination
The method consists of two steps:
Forward Elimination: the system is
reduced to upper triangular form. A sequence
of elementary operations is used.
Backward Substitution: Solve the system
starting from the last variable.
۱٥
Elementary Row Operations
۱٦
Forward Elimination
ai1
aij ← aij − a1 j (1 ≤ j ≤ n )
a11
To eliminate x1 2 ≤ i ≤ n
a
bi ← bi − i1 b1
a11
ai 2
aij ← aij − a 2 j (2 ≤ j ≤ n )
a 22
To eliminate x2 3 ≤ i ≤ n
a
bi ← bi − i 2 b2
a 22
۱۷
Forward Elimination
aik
aij ← aij − akj (k ≤ j ≤ n)
akk
To eliminate xk k + 1 ≤ i ≤ n
aik
bi ← bi − bk
a kk
۱۸
Backward Substitution
bn
xn =
a n ,n
bn −1 − a n −1,n xn
xn −1 =
a n −1,n −1
bn − 2 − a n − 2,n xn − a n − 2,n −1 xn −1
xn − 2 =
a n − 2, n − 2
n
bi − ∑ ai , j x j
j = i +1
xi =
a i ,i
۱۹
Example 1
Solve using Naive Gaussian Elimination :
Part 1 : Forward Elimination ___ Step1 : Eliminate x1 from equations 2, 3
x1 + 2 x2 + 3x3 = 8 eq1 unchanged ( pivot equation)
2
2 x1 + 3x2 + 2 x3 = 10 eq 2 ← eq 2 − eq1
1
3
3x1 + x2 + 2 x3 = 7 eq3 ← eq3 − eq1
1
x1 + 2 x2 + 3 x3 = 8
− x2 − 4 x3 = −6
− 5 x2 − 7 x3 = − 17
۲۰
Example 1
Part 1 : Forward Elimination Step2 : Eliminate x2 from equation 3
x1 + 2 x2 + 3x3 = 8 eq1 unchanged
− x2 − 4 x3 = −6 eq 2 unchanged ( pivot equation)
−5
− 5 x2 − 7 x3 = − 17 eq3 ← eq3 − eq 2
−1
x1 + 2 x2 + 3 x3 = 8
⇒ − x2 − 4 x3 = −6
13 x3 =13
۲۱
Example 1
Backward Substitution
b3 13
x3 = = =1
a3,3 13
b2 − a2,3 x3 − 6 + 4 x3
x2 = = =2
a 2, 2 −1
b1 − a1, 2 x2 − a1,3 x3 8 − 2 x2 − 3 x3
x1 = = =1
a1,1 a1,1
x1 1
The solution is x2 = 2
x3 1
۲۲
Example
Forward Elimination
6 −2 2 4 x1 16
12 − 8 6 10 x 26
2 =
3 − 13 9 3 x3 − 19
− 6 4 1 − 18 x4 − 34
Part 1 : Forward Elimination
Step1 : Eliminate x1 from equations 2, 3, 4
6 − 2 2 4 x1 16
0 − 4 2 2 x − 6
2 =
0 − 12 8 1 x3 − 27
0 2 3 − 14 x4 − 18
۲۳
Example
Forward Elimination
Step2 : Eliminate x2 from equations 3, 4
6 − 2 2 4 x1 16
0 − 4 2 2 x − 6
2 =
0 0 2 − 5 x3 − 9
0 0 4 − 13 4 − 21
x
Step3 : Eliminate x3 from equation 4
6 − 2 2 4 x1 16
0 − 4 2 2 x − 6
2 =
0 0 2 − 5 x3 − 9
0 0 0 − 3 x4 − 3
۲٤
Example
Forward Elimination
۲٥
Example
Backward Substitution
6 − 2 2 4 x1 16
0 − 4 2 2 x − 6
2 =
0 0 2 − 5 x3 − 9
0 0 0 − 3 x4 − 3
Solve for x4 , then solve for x3 ,... solve for x1
−3 −9+5
x4 = = 1, x3 = = −2
−3 2
− 6 − 2(−2) − 2(1) 16 + 2(1) − 2(−2) − 4(1)
x2 = = 1, x1 = =3
−4 6
۲٦
Naive Gaussian Elimination
o The method consists of two steps
o Forward Elimination: the system is reduced to
upper triangular form. A sequence of elementary
operations is used.
a11 a12 a13 x1 b1 a11 a12 a13 x1 b1
a a22 a23 x = b ⇒ 0 a22 ' a23 ' x = b '
21 2 2 2 2
a31 a32 a33 x3 b3 0 0 a33 ' x3 b3 '
۲۷
Problems with Naive Gaussian Elimination
o The Naive Gaussian Elimination may fail for
very simple cases. (The pivoting element is zero).
0 1 x1 1
1 1 x = 2
2
o Very small pivoting element may result in
serious computation errors
10 −10 1 x1 1
=
1 1 x2 2
۲۸
Example 2
Solve the following system using Gaussian Elimination
with Scaled Partial Pivoting :
1 − 1 2 1 x1 1
3 2 1 4 x 1
2 =
5 8 6 3 x3 1
4 2 5 3 x − 1
4
۲۹
Example 2
Initialization step
Scale vector:
1 − 1 1 x1 1
2 disregard sign
3 2
4 x2 1
1 find largest in
= magnitude in
each row
5 8 3 x3 1
6
4 2 3 x4 − 1
5
Scale vector S = [2 4 8 5]
Index Vector L = [1 2 3 4]
۳۰
Why Index Vector?
Index vectors are used because it is much
easier to exchange a single index element
compared to exchanging the values of a
complete row.
In practical problems with very large N,
exchanging the contents of rows may not
be practical.
۳۱
Example 2
Forward Elimination-- Step 1: eliminate x1
۳٦
Example 3
Solve the following sytstem using Gaussian Elimination
with Scaled Partial Pivoting
1 − 1 2 1 x1 1
3 2 1 4 x 1
2 =
5 − 8 6 3 x3 1
4 2 5 3 x4 − 1
۳۷
Example 3
Initialization step
1 − 1 2 1 x1 1
3 2 1
4 x2 1
=
5 − 8 6 3 x3 1
4 2 5 3 x4 − 1
Scale vector S = [2 4 8 5]
Index Vector L = [1 2 3 4]
۳۸
Example 3
Forward Elimination-- Step 1: eliminate x1
al , 2
i 0.5 10.5 1.5
Ratios : i = 2,3,4 = ⇒ L = [ 4 3 2 1]
Sli 4 8 2
٤۱
Example 3
Forward Elimination-- Step 2: eliminate x2
Updating A and B
0 − 1.5 0.75 0.25 x1 1.25
0 0.5 − 2.75 1.75 x 1.75
2 =
0 − 10.5 − 0.25 − 0.75 x3 2.25
4 2 5 3 x4 − 1
L = [ 4 1 3 2]
0 0 0.7857 0.3571 x1 0.9286
0 0 - 2.7619 1.7143 x 1.8571
2 =
0 − 10.5 − 0.25 − 0.75 x3 2.25
4 2 5 3 4
x − 1
٤۲
Example 3
Forward Elimination-- Step 3: eliminate x3
Selection of the third pivot equation
0 0 0.7857 0.3571 x1 0.9286
0 0 − 2.7619 1.7143 x 1.8571
2 =
0 − 10.5 − 0.25 − 0.75 x3 2.25
4 2 5 3 x4 − 1
S = [2 4 8 5 ] L=[ 4 3 2 1 ]
al ,3 2.7619 0.7857
Ratios : i
i = 3,4 = ⇒ L = [ 4 3 2 1]
Sli 4 2
٤۳
Example 3
Forward Elimination-- Step 3: eliminate x3
0 0 0.7857 0.3571 x1 0.9286
0 0 − 2.7619 1.7143 x 1.8571
2 =
0 − 10.5 − 0.25 − 0.75 x3 2.25
4 2 5 3 4
x − 1
L = [ 4 3 2 1]
0 0 0 0.8448 x1 1.4569
0 0 − 2.7619 1.7143 x 1.8571
2 =
0 − 10.5 − 0.25 − 0.75 x3 2.25
4 2 5 3 x4 − 1
٤٤
Example 3
Backward Substitution
0 0 0 0.8448 x1 1.4569 L = [ 4 3 2 1]
0 0 − 2.7619 1.7143 x2 1.8571
=
0 − 10.5 − 0.25 − 0.75 x3 2.25
4
2 5 3 x4 − 1
bl4 1.4569 bl3 − al3 ,4 x4 1.8571 − 1.7143x4
x4 = = = 1.7245, x3 = = = 0.3980
al4 ,4 0.8448 a l3 ,3 − 2.7619
bl2 − al2 ,4 x4 − al2 ,3 x3
x2 = = −0.3469
al2 ,2
bl1 − al1 ,4 x4 − al1 ,3 x3 − al1 ,2 x2 − 1 − 3 x 4 − 5 x3 − 2 x 2
x1 = = = −1.8673
al1 ,1 4
٤٥
Remarks:
We use index vector to avoid the need to move
the rows which may not be practical for large
problems.
If we order the equation as in the last value of
the index vector, we have a triangular form.
Scale vector is formed by taking maximum in
magnitude in each row.
Scale vector does not change.
٤٦
Gauss-Jordan Method
The method reduces the general system of
equations AX=B to IX=B where I is an identity
matrix.
2 − 2 2 x1 0
4 2 − 1 x = 7
2
2 − 2 4 x3 2
Step 1 Eleminate x1 from equations 2and 3
eq1 ← eq1 / 2
1 − 1 1 x1 0
4
eq 2 ← eq 2 − eq1 ⇒ 0 6 − 5 x2 = 7
1
0 0 2 x3 2
2
eq3 ← eq3 − eq1
1
٤۸
Gauss-Jordan Method
Example
1 − 1 1 x1 0
0 6 − 5 x = 7
2
0 0 2 x3 2
Step 2 Eleminate x 2 from equations 1 and 3
eq 2 ← eq 2 / 6 1 0 0.1667 x1 1.1667
−1 x = 1.1667
eq1 ← eq1 − eq 2 ⇒ 0 1 − 0.8333 2
1 0 0
2 x3 2
٤۹
Gauss-Jordan Method
Example
1 0 0.1667 x1 1.1667
0 1 − 0.8333 x = 1.1667
2
0 0 2 x3 2
Step 3 Eleminate x 3 from equations 1 and 2
eq3 ← eq3 / 2
1 0 0 x1 1
0.1667 x = 2
eq1 ← eq1 − eq 3 ⇒
0 1 0 2
1 0 0 1 x 1
− 0.8333 3
eq 2 ← eq 2 − eq3
1
٥۰
Gauss-Jordan Method
Example
2 − 2 2 x1 0
4 2 − 1 x = 7
2
2 − 2 4 x3 2
is transformed to
1 0 0 x1 1 x1 1
0 1 0 x = 2 ⇒ solution is x = 2
2 2
0 0 1 x3 1 x3 1
٥۱
University of Technology
Petroleum Technology Department
Numerical Analysis
2
Learning Objectives
Grasp the difference between initial value
problems and boundary value problems.
Appreciate the difficulties involved in solving the
boundary value problems.
Use the shooting method to solve boundary
value problems.
Use the finite difference method to solve BVP.
3
Gauss-Jordan Method
The method reduces the general system of
equations AX=B to IX=B where I is an identity
matrix.
2 2 2 x1 0
4 2 1 x 7
2
2 2 4 x3 2
Step 1 Eleminate x1 from equations 2and 3
eq1 eq1 / 2
1 1 1 x1 0
4
eq 2 eq 2 eq1 0 6 5 x2 7
1
0 0 2 x3 2
2
eq 3 eq 3 eq1
1
5
Gauss-Jordan Method
Example
1 1 1 x1 0
0 6 5 x 7
2
0 0 2 x3 2
Step 2 Eleminate x 2 from equations 1 and 3
eq 2 eq 2 / 6 1 0 0.1667 x1 1.1667
1
eq1 eq1 eq 2 0 1 0.8333 x 1.1667
2
1 0 0
2 x3 2
6
Gauss-Jordan Method
Example
1 0 0.1667 x1 1.1667
0 1 0.8333 x 1.1667
2
0 0 2 x3 2
Step 3 Eleminate x 3 from equations 1 and 2
eq 3 eq 3 / 2
1 0 0 x1 1
0.1667 x 2
eq1 eq1 eq 3
0 1 0 2
1 0 0 1 x 1
0.8333 3
eq 2 eq 2 eq 3
1
7
Boundary-Value and
Initial Value Problems
Initial-Value Problems Boundary-Value Problems
2 t
x 2 x x e x 2 x x e 2t
x(0) 1, x (0) 2.5 x(0) 1, x(2) 1.5
same different
8
Shooting Method
9
The Shooting Method
Target
10
The Shooting Method
Target
11
Solution of Boundary-Value Problems
Shooting Method for Boundary-Value Problems
13
Example 1
Original BVP
y 4 y 4 x 0
y (0) 0, y (1) 2
0 1 x
24
Example 1
Original BVP
y 4 y 4 x 0
y (0) 0, y (1) 2
2. 0
0 1 x
25
Example 1
Original BVP
y 4 y 4 x 0
y (0) 0, y (1) 2
2. 0
0 1 x
26
Example 1
Original BVP
y 4 y 4 x 0
y (0) 0, y (1) 2
2. 0
to be
determined
0 1 x
27
Example 1
Step1: Convert to a System of First Order ODEs
y 4 y 4 x 0
y (0) 0, y (1) 2
Convert to a systemof first order Equations
y 1 y 2 y1 (0) 0
y 4(y x), y (0) ?
2 1 2
Guess#1
y (0) 0
Thus _ from _ RK 2
0 1 x
y (1) 0.7688
-0.7688
29
Example 1
Guess # 2
y 4 y 4 x 0
y (0) 0, y (1) 2
0.99
Guess#2
y (0) 1
From _ RK 2 0 1 x
y (1) 0.99
30
Example 1
Interpolation for Guess # 3
y 4 y 4 x 0 y(1)
y (0) 0, y (1) 2
0.99
Guess y (0) y(1)
1 0 -0.7688
0 1 2 y’(0)
2 1 0.9900
-0.7688
31
Example 1
Interpolation for Guess # 3
y 4 y 4 x 0 y(1)
y (0) 0, y (1) 2 2
Guess 3
0.99
Guess y (0) y(1)
1.5743
1 0 -0.7688
0 1 2 y’(0)
2 1 0.9900
-0.7688
32
Example 1
Guess # 3
y 4 y 4 x 0 2.000
y (0) 0, y (1) 2
Guess#3
y (0) 1.5743
0 1 x
33
Summary of the Shooting Method
1. Guess the unavailable values for the
auxiliary conditions at one point of the
independent variable.
2. Solve the initial value problem.
3. Check if the boundary conditions are
satisfied, otherwise modify the guess and
resolve the problem.
4. Repeat (3) until the boundary conditions
are satisfied.
34
Properties of the Shooting Method
35
Solution of Boundary-Value Problems
Finite Difference Method
Boundary-Value convert
Algebraic
Problems Equations
Find the unknowns y1, y2, y3
y4=0.8
Find y ( x) to solve BVP y3=?
y y1=?
y 2 y y x
2
y2=?
y0=0.2
y (0) 0.2, y (1) 0.8
0 0.25 0.5 0.75 1.0 x
x0 x1 x2 x3 x4
36
Solution of Boundary-Value Problems
Finite Difference Method
Divide the interval into n sub-intervals.
The solution of the BVP is converted to
the problem of determining the value of
function at the base points.
Use finite approximations to replace the
derivatives.
This approximation results in a set of
algebraic equations.
Solve the equations to obtain the solution
of the BVP.
37
Finite Difference Method
Example
y 2 y y x 2
y (0) 0.2, y (1) 0.8 To be
determined
Divide the interval
[0,1 ] into n = 4 y y3=? y4=0.8
intervals y1=?
Base points are y2=?
x0=0
x1=0.25 y0=0.2
x2=.5
x3=0.75 0 0.25 0.5 0.75 1.0 x
x4=1.0 x0 x1 x2 x3 x4
38
Finite Difference Method
Example
y 2 y y x 2
y (0) 0.2, y (1) 0.8
Divide the interval Replace
[0,1 ] into n = 4 yi 1 2 yi yi 1
intervals y 2
central difference formula
h
Base points are y y
y i 1 i 1 central difference formula
x0=0 2h
x1=0.25 y 2 y y x 2
x2=.5 Becomes
x3=0.75 yi 1 2 yi yi 1 yi 1 yi 1
2 yi xi 2
h2 2h
x4=1.0
39
Second Order BVP
d2y dy
2
2 y x 2
with y (0) 0.2, y (1) 0.8
dx dx
Let h 0.25
Base Points
x0 0, x1 0.25, x2 0.5, x3 0.75, x4 1
dy y ( x h) y ( x) yi 1 yi
dx h h
d2y y ( x h) 2 y ( x ) y ( x h) yi 1 2 yi yi 1
2
2
dx h h2
40
Second Order BVP
d2y dy
2 y x 2
dx 2 dx
yi 1 2 yi yi 1 yi 1 yi
2
2 yi xi i 1,2,3
2
h h
x0 0, x1 0.25, x2 0.5, x3 0.75, x4 1
y0 0.2, y1 ?, y2 ?, y3 ?, y4 0.8
16 yi 1 2 yi yi 1 8 yi 1 yi yi xi 2
24 yi 1 39 yi 16 yi 1 xi 2
41
Second Order BVP
24 yi 1 39 yi 16 yi 1 xi
2
i 1 24 y2 39 y1 16 y0 x1
2
i2 24 y3 39 y2 16 y1 x2
2
i3 24 y4 39 y3 16 y2 x3
2
39 24 0 y1 0.252 16(0.2)
16 39 24 y 0.5 2
2
0 16 39 y3 0.752 24(0.8)
Solution y1 0.4791, y2 0.6477, y3 0.7436
42
Second Order BVP
d2y dy
2 y x 2
dx 2 dx
yi 1 2 yi yi 1 yi 1 yi
2
2 yi xi i 1,2,...,100
2
h h
x0 0, x1 0.01, x2 0.02 ... x99 0.99, x100 1
y0 0.2, y1 ?, y2 ?, ... y99 ?, y100 0.8
10000 yi 1 2 yi yi 1 200 yi 1 yi yi xi 2
44
Remarks
Finite Difference Method :
Different formulas can be used for
approximating the derivatives.
Different formulas lead to different
solutions. All of them are approximate
solutions.
45
Example
46
Example
47
University of Technology
Petroleum Technology Department
Numerical Analysis
۲
Motivation
Given a set of experimental data:
x 1 2 3
y 5.1 5.9 6.3
۳
Motivation
In engineering, two types of applications are
encountered:
Trend analysis: Predicting values of dependent
variable, may include extrapolation beyond data points
or interpolation between data points.
Hypothesis testing: Comparing existing mathematical
model with measured data.
1. What is the best mathematical function f that
represents the dataset?
2. What is the best criterion to assess the fitting of
the function f to the data?
٤
Curve Fitting
Given a set of tabulated data, find a curve
or a function that best represents the
data.
Given:
1. The tabulated data
2. The form of the function
g k ( x) are known.
۷
Decide on the Criterion
1. Least Squares Regression :
n
minimize ∑ i
( y − f ( x i )) 2
i =1
۸
Least Squares Regression
Given: xi x1 x2 …. xn
yi y1 y2 …. yn
ei = ( yi − f ( xi )) 2 = ( f ( xi ) − yi ) 2
2
۹
Determine the Unknowns
۱۰
Determine the Unknowns
Necessary condition for the minimum :
∂ Φ ( a, b)
=0
∂a
∂ Φ ( a, b)
=0
∂b
۱۱
Determining the Unknowns
∂ Φ ( a , b) n
= ∑ 2(a + bxi − yi ) = 0
∂a i =1
∂ Φ ( a , b) n
= ∑ 2(a + bxi − yi )xi = 0
∂b i =1
۱۲
Normal Equations
n n
n a + ∑ xi b = ∑ yi
i =1 i =1
n n 2 n
∑ xi a + ∑ xi b = ∑ xi y i
i =1 i =1 i =1
۱۳
Solving the Normal Equations
n n n
n ∑ xi yi − ∑ xi ∑ yi
b= i =1 i =1 i =1
2
n n
n ∑ xi − ∑ xi
2
i =1 i =1
1 n n
a = ∑ yi − b ∑ xi
n i =1 i =1
۱٤
Example 1: Linear Regression
Assume :
f(x) = a + bx x 1 2 3
y 5.1 5.9 6.3
Equations :
n n
n a + ∑ xi b = ∑ yi
i =1 i =1
n n 2 n
∑ xi a + ∑ xi b = ∑ xi y i
i =1 i =1 i =1
۱٥
Example 1: Linear Regression
i 1 2 3 sum
xi 1 2 3 6
yi 5.1 5.9 6.3 17.3
xi2 1 4 9 14
xi yi 5.1 11.8 18.9 35.8
Equations :
3 a + 6 b = 17.3
6a + 14 b = 35.8
Solving : a = 4.5667 b = 0.60
۱٦
Multiple Linear Regression
Example: t 0 1 2 3
f(x,t) = a + b x + c t
That best fits the data with the least sum of the
square of errors.
۱۷
Solution of Multiple Linear Regression
Construct Φ , the sum t 0 1 2 3
of the square of the
x 0.1 0.4 0.2 0.2
error and derive the
necessary conditions by y 3 2 1 2
equating the partial
derivatives with respect
to the unknown
parameters to zero, then
solve the equations.
۱۸
Solution of Multiple Linear Regression
n
f ( x, t ) = a + bx + ct , Φ ( a , b, c ) = ∑ (a + bxi + cti − yi )2
i =1
Necessary conditions :
∂Φ ( a , b, c ) n
= 2∑ (a + bxi + cti − yi ) = 0
∂a i =1
∂Φ ( a , b, c ) n
= 2∑ (a + bxi + cti − yi ) xi = 0
∂b i =1
∂Φ ( a , b, c ) n
= 2∑ (a + bxi + cti − yi ) ti = 0
∂c i =1
۱۹
System of Equations
n n n
a n + b ∑ xi + c ∑ ti = ∑ y i
i =1 i =1 i =1
n n n n
a ∑ xi + b ∑ ( x i ) 2 + c ∑ ( x i t i ) = ∑ ( x i y i )
i =1 i =1 i =1 i =1
n n n n
a ∑ ti + b∑ ( xi ti ) + c ∑ (ti ) = ∑ (ti yi )
2
i =1 i =1 i =1 i =1
۲۰
Example 2: Multiple Linear Regression
i 1 2 3 4 Sum
ti 0 1 2 3 6
xi 0.1 0.4 0.2 0.2 0.9
yi 3 2 1 2 8
xi2 0.01 0.16 0.04 0.04 0.25
xi ti 0 0.4 0.4 0.6 1.4
xi yi 0.3 0.8 0.2 0.4 1.7
ti2 0 1 4 9 14
ti yi 0 2 2 6 10
۲۱
Example 2: System of Equations
4a + 0.9b + 6c = 8
0.9a + 0.25b + 1.4c = 1.7
6a + 1.4b + 14c = 10
Solving :
a = 2.9574, b = −1.7021, c = −0.38298
f ( x, t ) = a + bx + ct = 2.9574 − 1.7021 x − 0.38298 t
۲۲
Polynomial Regression
The least squares method can be extended to fit
the data to a higher-order polynomial
f ( x ) = a + bx + cx ,
2 2
ei = ( f ( x ) − yi ) 2
( )
n
Minimize Φ ( a , b, c ) = ∑
2
a + bxi + cxi2 − yi
i =1
Necessary conditions :
∂Φ ( a , b, c ) ∂Φ ( a , b, c ) ∂Φ ( a , b, c )
= 0, = 0, =0
∂a ∂b ∂c
۲۳
Equations for Quadratic Regression
( )
n
Minimize Φ ( a, b, c ) = ∑
2
a + bxi + cxi2 − yi
i =1
∂Φ ( a, b, c )
( )
n
= 2∑ a + bxi + cxi2 − yi = 0
∂a i =1
∂Φ ( a, b, c )
( )
n
= 2∑ a + bxi + cxi2 − yi xi = 0
∂b i =1
∂Φ ( a, b, c )
( )
n
= 2∑ a + bxi + cxi2 − yi xi2 = 0
∂c i =1
۲٤
Normal Equations
n n n
a n + b ∑ xi + c ∑ xi2 = ∑ yi
i =1 i =1 i =1
n n n n
a ∑ xi + b ∑ 2
xi +c∑ 3
xi = ∑ xi y i
i =1 i =1 i =1 i =1
n n n n
a ∑ xi2 + b ∑ xi3 + c ∑ xi4 = ∑ xi2 yi
i =1 i =1 i =1 i =1
۲٥
Example 3: Polynomial Regression
Fit a second-order polynomial to the following data
xi 0 1 2 3 4 5 ∑=15
yi 2.1 7.7 13.6 27.2 40.9 61.1 ∑=152.6
xi2 0 1 4 9 16 25 ∑=55
xi3 0 1 8 27 64 125 225
xi4 0 1 16 81 256 625 ∑=979
xi y i 0 7.7 27.2 81.6 163.6 305.5 ∑=585.6
۲٦
Example 3: Equations and Solution
6 a + 15 b + 55 c = 152.6
15 a + 55 b + 225 c = 585.6
55 a + 225 b + 979 c = 2488.8
Solving . . .
a = 2.4786, b = 2.3593, c = 1.8607
f ( x ) = 2.4786 + 2.3593 x + 1.8607 x 2
۲۷
How Do You Judge Functions?
Given two or more functions to fit the data,
How do you select the best?
Answer :
Determine the parameters for each function,
then compute Φ for each one. The function
resulting in smaller Φ (least sum of the squares
of the errors) is the best.
۲۸
Example showing that Quadratic is
preferable than Linear Regression
y y
x x
۲۹
Fitting with Nonlinear Functions
xi 0.24 0.65 0.95 1.24 1.73 2.01 2.23 2.52
۳۰
Fitting with Nonlinear Functions
n
Φ ( a , b, c ) = ∑ ( a ln( xi ) + b cos( xi ) + c e xi − yi ) 2
i =1
Necessary condition for the minimum :
∂ Φ ( a , b, c )
= 0
∂a
∂ Φ ( a , b, c )
= 0 ⇒ Normal Equations
∂b
∂ Φ ( a, b, c )
= 0
∂c ۳۱
Normal Equations
n n n n
a ∑ (ln xi ) + b∑ (ln xi )(cos xi ) + c ∑ (ln xi )( e ) = ∑ yi (ln xi )
2 xi
i =1 i =1 i =1 i =1
n n n n
a ∑ (ln xi )(cos xi ) + b∑ (cos xi ) + c ∑ (cos xi )( e ) = ∑ yi (cos xi )
2 xi
i =1 i =1 i =1 i =1
n n n n
a ∑ (ln xi )( e ) + b∑ (cos xi )( e ) + c ∑ ( e ) = ∑ yi ( e xi )
xi xi xi 2
i =1 i =1 i =1 i =1
۳۲
Example 4: Evaluating Sums
xi 0.24 0.65 0.95 1.24 1.73 2.01 2.23 2.52 ∑=11.57
yi 0.23 -0.23 -1.1 -0.45 0.27 0.1 -0.29 0.24 ∑=-1.23
(ln xi)2 2.036 0.1856 0.0026 0.0463 0.3004 0.4874 0.6432 0.8543 ∑=4.556
ln(xi) cos(xi) -1.386 -0.3429 -0.0298 0.0699 -0.0869 -0.2969 -0.4912 -0.7514 ∑=-3.316
ln(xi) * exi -1.814 -0.8252 -0.1326 0.7433 3.0918 5.2104 7.4585 11.487 ∑=25.219
yi * ln(xi) -0.328 0.0991 0.0564 -0.0968 0.1480 0.0698 -0.2326 0.2218 ∑=-0.0625
cos(xi)2 0.943 0.6337 0.3384 0.1055 0.0251 0.1808 0.3751 0.6609 ∑=3.26307
cos(xi) * exi 1.235 1.5249 1.5041 1.1224 -0.8942 -3.1735 -5.696 -10.104 ∑=-14.481
yi*cos(xi) 0.223 -0.1831 -0.6399 -0.1462 -0.0428 -0.0425 0.1776 -0.1951 ∑=-0.8485
(exi)2 1.616 3.6693 6.6859 11.941 31.817 55.701 86.488 154.47 ∑=352.39
yi * exi 0.2924 -0.4406 -2.844 -1.555 1.523 0.7463 -2.697 2.9829 ∑=-1.9923
۳۳
Example 4: Equations & Solution
4.55643 a − 3.31547 b + 25.2192 c = −0.062486
− 3.31547 a + 3.26307 b − 14.4815 c = −0.848514
25.2192 a − 14.4815 b + 352.388 c = −1.992283
۳٤
Example 5
xi 1 2 3
Given:
yi 2.4 5 9
( )
n
Instead of minimizing : Φ ( a, b) = ∑ ae
2
bxi
− yi
i =1
n
Minimize : Φ (α , b) = ∑ (α + bxi − zi )2 ( Easier to solve)
i =1
۳٦
Example 5: Equations
n
Φ (α , b) = ∑ (α + b xi − zi )2
i =1
Normal Equations are obtained using :
∂Φ n
= 2∑ (α + b xi − zi ) = 0
∂α i =1
∂Φ n
= 2∑ (α + b xi − zi ) xi = 0
∂b i =1
n n n n n
α n + b ∑ xi = ∑ zi and α ∑ xi + b∑ xi2 = ∑ ( xi zi )
i =1 i =1 i =1 i =1 i =1
۳۷
Evaluating Sums and Solving
xi 1 2 3 ∑=6
yi 2.4 5 9
zi=ln(yi) 0.875469 1.609438 2.197225 ∑=4.68213
xi2 1 4 9 ∑=14
xi z i 0.875469 3.218876 6.591674 ∑=10.6860
Equations :
3 α + 6 b = 4.68213 α = ln(a ), a = eα
6 α + 14 b = 10.686 a = e 0.23897 = 1.26994
Solving Equations :
f ( x ) = ae bx = 1.26994 e 0.66087 x
α = 0.23897, b = 0.66087
۳۸
Power Equation
۳۹
Power Equation
٤۰
Power Equation
٤۱
Power Equation
٤۲
٤۳
Activities
٤٤
University of Technology
Petroleum Technology Department
Numerical Analysis
Lecture 9 : Interpolation
Semester 1 (2020-2021)
۲
Introduction
Interpolation was used for x sin(x)
long time to provide an
0 0.0000
estimate of a tabulated
function at values that are 0.1 0.0998
not available in the table.
0.2 0.1987
٤
Example
Temperature Viscosity
An experiment is used to determine (degree)
the viscosity of water as a function
of temperature. The following table
0 1.792
is generated:
5 1.519
10 1.308
Problem: Estimate the viscosity
when the temperature is 8 degrees.
15 1.140
٥
Interpolation Problem
Find a polynomial that fits the data
points exactly.
V : Viscosity
n
V(T) = ∑ ak T k T : Temperature
k =0 ak : Polynomial
Vi = V(Ti ) coefficients
Linear Interpolation: V(T)= 1.73 − 0.0422 T
V(8)= 1.3924
٦
Existence and Uniqueness
Given a set of n+1 points:
(x0 , f ( x0 ) ), (x1, f ( x1 ) ), ...., (xn , f ( xn ) )
Assumption: x0 , x1 ,..., xn are distinct
Theorem:
There is a unique polynomial fn(x) of order ≤ n
such that:
f n ( xi ) = f ( xi ) for i = 0 ,1,...,n
۷
Examples of Polynomial Interpolation
Linear Interpolation Quadratic Interpolation
۸
Linear Interpolation
Given any two points, ( x0 , f ( x0 ) ), ( x1 , f ( x1 ) )
4−2
f1 ( x) = 2 + (x − 1) = 2 x
2 −1
۹
Quadratic Interpolation
Given any three points: (x0 , f ( x0 ) ), (x1 , f ( x1 ) ), and (x2 , f ( x2 ) )
The polynomial that interpolates the three points is:
f 2 ( x ) = b0 + b1 ( x − x0 ) + b2 ( x − x0 )( x − x1 )
where :
b0 = f ( x0 )
f ( x1 ) − f ( x0 )
b1 = f [ x0 , x1 ] =
x1 − x0
f ( x2 ) − f ( x1 ) f ( x1 ) − f ( x0 )
−
x2 − x1 x1 − x0
b2 = f [ x0 , x1 , x2 ] =
x2 − x0
۱۰
General nth Order Interpolation
Given any n+1 points: (x0 , f ( x0 ) ), (x1 , f ( x1 ) ), ..., (xn , f ( xn ) )
The polynomial that interpolates all points is:
۱۱
Divided Differences
f [ xk ] = f ( xk ) Zeroth order DD
f [ x1 ] − f [ x0 ]
f [ x0 , x1 ] = First order DD
x1 − x0
f [ x1 , x2 ] − f [ x0 , x1 ]
f [ x0 , x1 , x2 ] = Second order DD
x2 − x0
............
f [ x1 , x2 ,..., xk ] − f [ x0 , x1 ,..., xk −1 ]
f [ x0 , x1 ,..., xk ] =
xk − x0
۱۲
Divided Difference Table
x F[ ] F[ , ] F[ , , ] F[ , , ,]
x0 F[x0] F[x0,x1] F[x0,x1,x2] F[x0,x1,x2,x3]
x1 F[x1] F[x1,x2] F[x1,x2,x3]
x2 F[x2] F[x2,x3]
x3 F[x3]
i −1
(x − x j )
n
f n ( x) = ∑ F [ x0 , x1 ,..., xi ] ∏
i =0 j =0
۱۳
Divided Difference Table
x F[ ] F[ , ] F[ , , ]
xi f(xi)
0 -5 2 -4
1 -3 6
0 -5
-1 -15 1 -3
-1 -15
Entries of the divided difference
table are obtained from the data
table using simple operations.
۱٤
Divided Difference Table
x F[ ] F[ , ] F[ , , ] xi f(xi)
0 -5 2 -4 0 -5
1 -3 6 1 -3
-1 -15 -1 -15
۱٦
Divided Difference Table
x F[ ] F[ , ] F[ , , ] xi yi
0 -5 2 -4 0 -5
1 -3 6
-1 -15 1 -3
-1 -15
− 15 − (−3)
=6
−1−1
f [ x2 ] − f [ x1 ]
f [ x1 , x2 ] =
x2 − x1
۱۷
Divided Difference Table
x F[ ] F[ , ] F[ , , ] xi yi
0 -5 2 -4 0 -5
1 -3 6
-1 -15 1 -3
-1 -15
6 − (2)
= −4
− 1 − ( 0)
f [ x1 , x2 ] − f [ x0 , x1 ]
f [ x0 , x1 , x2 ] =
x2 − x0
۱۸
Divided Difference Table
x F[ ] F[ , ] F[ , , ] xi yi
0 -5 2 -4 0 -5
1 -3 6
-1 -15 1 -3
-1 -15
f 2 ( x) = −5 + 2( x − 0) − 4( x − 0)( x − 1)
x y x y
1 0 2 3
2 3 1 0
3 8 3 8
۲۰
Two Examples
x Y
x Y
1 0 3 1 2 3 3 1
2 3 5 1 0 4
3 8 3 8
P2 ( x) = 0 + 3( x − 1) + 1( x − 1)( x − 2) P2 ( x) = 3 + 3( x − 2) + 1( x − 2)( x − 1)
= x −1
2 = x2 −1
۲۱
Properties of Divided Difference
Ordering the points should not affect the divided difference:
f [ x0 , x1 , x2 ] = f [ x1 , x2 , x0 ] = f [ x2 , x1 , x0 ]
۲۲
Example
Find a polynomial to x f(x)
interpolate the data.
2 3
4 5
5 1
6 6
7 9
۲۳
Example
x f(x) f[ , ] f[ , , ] f[ , , , ] f[ , , , , ]
2 3 1 -1.6667 1.5417 -0.6750
4 5 -4 4.5 -1.8333
5 1 5 -1
6 6 3
7 9
f 4 = 3 + 1( x − 2) − 1.6667( x − 2)( x − 4) + 1.5417( x − 2)( x − 4)( x − 5)
− 0.6750( x − 2)( x − 4)( x − 5)( x − 6)
۲٤
Summary
Interpolating Condition : f ( xi ) = f n ( xi ) for i = 0, 1, 2, ..., n
* The interpolating Polynomial is unique.
* Different methods can be used to obtain it
- Newton Divided Difference
- Lagrange Interpolation
- Other methods
۲٥
Lagrange Interpolation
۲٦
The Interpolation Problem
Given a set of n+1 points:
۲۷
Lagrange Interpolation
Problem: xi x0 x1 …. xn
Given
yi y0 y1 …. yn
f n ( xi ) = f ( xi ) for i = 0,1,..., n
n
Lagrange Interpolation Formula: f n ( x) = ∑ f ( xi ) i ( x)
i =0
(x − x )
n
i ( x) = ∏ (x − x )
j =0, j ≠i
j
i j
۲۸
Lagrange Interpolation
۲۹
Lagrange Interpolation Example
P2 ( x ) = f ( x0 ) 0 ( x ) + f ( x1 ) 1 ( x ) + f ( x2 ) 2 ( x) x 1/3 1/4 1
0 ( x) =
( x − x1 ) (x − x2 )
=
( x − 1 / 4 ) (x − 1) y 2 -1 7
(x0 − x1 ) (x0 − x2 ) (1 / 3 − 1 / 4) (1 / 3 − 1)
1 ( x) =
(x − x0 ) (x − x2 ) = (x − 1 / 3) (x − 1)
(x1 − x0 ) (x1 − x2 ) (1 / 4 − 1 / 3) (1 / 4 − 1)
2 ( x) =
(x − x0 ) (x − x1 ) = (x − 1 / 3) (x − 1 / 4)
(x2 − x0 ) (x2 − x1 ) (1 − 1 / 3) (1 − 1 / 4)
P2 ( x) = 2{− 18( x − 1 / 4)( x − 1)}− 1{16( x − 1 / 3)( x − 1)}
+ 7{2( x − 1 / 3)( x − 1 / 4)}
۳۰
Example
Find a polynomial to interpolate: x y
0 1
Both Newton’s interpolation
method and Lagrange 1 3
interpolation method must
give the same answer. 2 2
3 5
4 4
۳۱
Newton’s Interpolation Method
0 1 2 -3/2 7/6 -5/8
1 3 -1 2 -4/3
2 2 3 -2
3 5 -1
4 4
۳۲
Interpolating Polynomial
3 7
f 4 ( x ) = 1 + 2( x ) − x ( x − 1) + x ( x − 1)( x − 2)
2 6
5
− x ( x − 1)( x − 2)( x − 3)
8
115 95 2 59 3 5 4
f4 ( x) = 1 + x− x + x − x
12 8 12 8
۳۳
Interpolating Polynomial Using
Lagrange Interpolation Method
∑
4
f 4 ( x) = f ( xi ) i = 0 + 31 + 2 2 + 5 3 + 4 4
i =0
( x − 1) ( x − 2) ( x − 3) ( x − 4) ( x − 1)( x − 2)( x − 3)( x − 4)
0 = =
(0 − 1) (0 − 2) (0 − 3) ( 0 − 4) 24
( x − 0) ( x − 2 ) ( x − 3) ( x − 4) x( x − 2)( x − 3)( x − 4)
1 = =
(1 − 0) (1 − 2) (1 − 3) (1 − 4) −6
( x − 0) ( x − 1) ( x − 3) ( x − 4) x( x − 1)( x − 3)( x − 4)
2 = =
( 2 − 0) ( 2 − 1) ( 2 − 3) ( 2 − 4) 4
( x − 0) ( x − 1) ( x − 2) ( x − 4) x( x − 1)( x − 2)( x − 4)
3 = =
(3 − 0) (3 − 1) ( 3 − 2) ( 3 − 4) −6
( x − 0) ( x − 1) ( x − 2) ( x − 3) x ( x − 1)( x − 2)( x − 3)
4 = =
( 4 − 0) ( 4 − 1) ( 4 − 2) ( 4 − 3) 24
۳٤
Inverse Interpolation
Error in Polynomial Interpolation
۳٥
Inverse Interpolation
Problem : Given a table of values
Find x such that : f ( x ) = yk , where yk is given
xi x0 x1 …. xn
yi y0 y1 …. yn
One approach:
Use polynomial interpolation to obtain f n (x) to interpolate the
data then use Newton’s method to find a solution to x
f n ( x ) = yk
۳٦
Inverse Interpolation
Inverse interpolation: xi x0 x1 …. xn
1. Exchange the roles
yi y0 y1 …. yn
of x and y.
2. Perform polynomial
Interpolation on the yi y0 y1 …. yn
new table.
xi x0 x1 …. xn
3. Evaluate
x = f n ( yk )
۳۷
Inverse Interpolation
x
y
x y
۳۸
Inverse Interpolation
Question:
۳۹
Inverse Interpolation
Example
x 1 2 3
Problem : y 3.2 2.0 1.6
BE CAREFUL
٤۱
10th Order Polynomial Interpolation
2
0.5
true function
-0.5
-5 -4 -3 -2 -1 0 1 2 3 4 5
٤۲
Summary
The interpolating polynomial is unique.
Different methods can be used to obtain it.
Newton’s divided difference
Lagrange interpolation
Others
Polynomial interpolation can be sensitive to
data.
BE CAREFUL when high order polynomials
are used.
٤۳
University of Technology
Petroleum Technology Department
Numerical Analysis
۲
Motivation
How do you evaluate the Time Displacement
function. 0 30.1
measurements.
15 40.2
۳
Recall
df f ( x + h) − f ( x )
= lim
dx h→0 h
Taylor Theorem :
f ( 2 ) ( x ) h 2 f ( 3) ( x ) h 3
f ( x + h) = f ( x ) + f ' ( x ) h + + + O(h 4 )
2! 3!
E = O(h ) ⇒ ∃ real , finite C , such that : E ≤ C h
n n
٤
Three Formula
df ( x) f ( x + h) − f ( x)
Forward Difference : =
dx h
df ( x) f ( x) − f ( x − h)
Backward Difference : =
dx h
df ( x) f ( x + h) − f ( x − h)
Central Difference : =
dx 2h
٦
Forward/Backward Difference Formula
Forward Difference : f ( x + h) = f ( x ) + f ' ( x ) h + O ( h 2 )
⇒ f ' ( x ) h = f ( x + h) − f ( x ) + O ( h 2 )
f ( x + h) − f ( x )
⇒ f ' ( x) = + O ( h)
h
_______________________________________________________
Backward Difference : f ( x − h) = f ( x ) − f ' ( x ) h + O ( h 2 )
⇒ f ' ( x ) h = f ( x ) − f ( x − h) + O ( h 2 )
f ( x ) − f ( x − h)
⇒ f ' ( x) = + O ( h)
h
۷
Central Difference Formula
Central Difference :
f ( 2 ) ( x) h 2 f ( 3) ( x) h 3 f ( 4 ) ( x ) h 4
f ( x + h) = f ( x ) + f ' ( x ) h + + + + ...
2! 3! 4!
f ( 2 ) ( x ) h 2 f ( 3) ( x ) h 3 f ( 4 ) ( x ) h 4
f ( x − h) = f ( x ) − f ' ( x ) h + − + + ...
2! 3! 4!
f ( 3) ( x ) h 3
f ( x + h) − f ( x − h) = 2 f ' ( x ) h + 2 + ...
3!
f ( x + h) − f ( x − h)
⇒ f ' ( x) = + O(h 2 )
2h
۸
The Three Formula (Revisited)
df ( x) f ( x + h) − f ( x)
Forward Difference : = + O ( h)
dx h
df ( x) f ( x) − f ( x − h)
Backward Difference: = + O ( h)
dx h
df ( x) f ( x + h) − f ( x − h)
Central Difference : = + O(h 2 )
dx 2h
۹
Higher Order Formulas
f ( 2) ( x)h 2 f (3) ( x)h 3 f ( 4) ( x)h 4
f ( x + h) = f ( x ) + f ' ( x ) h + + + + ...
2! 3! 4!
f ( 2) ( x)h 2 f (3) ( x)h 3 f ( 4) ( x)h 4
f ( x − h) = f ( x ) − f ' ( x ) h + − + + ...
2! 3! 4!
f ( 2) ( x ) h 2 f ( 4) ( x ) h 4
f ( x + h) + f ( x − h) = 2 f ( x ) + 2 +2 + ...
2! 4!
f ( x + h) − 2 f ( x ) + f ( x − h)
⇒ f ( 2) ( x ) = 2
+ O ( h 2
)
h
f ( 4) (ξ )h 2
Error = −
12
۱۰
Other Higher Order Formulas
f ( x + h) − 2 f ( x) + f ( x − h)
f ( 2) ( x ) =
h2
f ( x + 2h ) − 2 f ( x + h ) + 2 f ( x − h ) − f ( x − 2h )
f (3) ( x ) =
2h 3
f ( x + 2h ) − 4 f ( x + h ) + 6 f ( x ) − 4 f ( x − h ) + f ( x − 2h )
f ( 4) ( x ) =
h4
۱۱
Example
Use forward, backward and centered difference
approximations to estimate the first derivate of:
f(x) = –0.1x4 – 0.15x3 – 0.5x2 – 0.25x + 1.2
at x = 0.5 using step size h = 0.5 and h = 0.25
Note that the derivate can be obtained directly:
f’(x) = –0.4x3 – 0.45x2 – 1.0x – 0.25
The true value of f’(0.5) = -0.9125
In this example, the function and its derivate
are known. However, in general, only tabulated
data might be given.
۱۲
Solution with Step Size = 0.5
f(0.5) = 0.925, f(0) = 1.2, f(1.0) = 0.2
Forward Divided Difference:
f’(0.5) ≈ (0.2 – 0.925)/0.5 = -1.45
|εt| = |(-0.9125+1.45)/-0.9125| = 58.9%
Backward Divided Difference:
f’(0.5) ≈ (0.925 – 1.2)/0.5 = -0.55
|εt| = |(-0.9125+0.55)/-0.9125| = 39.7%
Centered Divided Difference:
f’(0.5) ≈ (0.2 – 1.2)/1.0 = -1.0
|εt| = |(-0.9125+1.0)/-0.9125| = 9.6%
۱۳
Solution with Step Size = 0.25
f(0.5)=0.925, f(0.25)=1.1035, f(0.75)=0.6363
Forward Divided Difference:
f’(0.5) ≈ (0.6363 – 0.925)/0.25 = -1.155
|εt| = |(-0.9125+1.155)/-0.9125| = 26.5%
Backward Divided Difference:
f’(0.5) ≈ (0.925 – 1.1035)/0.25 = -0.714
|εt| = |(-0.9125+0.714)/-0.9125| = 21.7%
Centered Divided Difference:
f’(0.5) ≈ (0.6363 – 1.1035)/0.5 = -0.934
|εt| = |(-0.9125+0.934)/-0.9125| = 2.4%
۱٤
Discussion
For both the Forward and Backward difference,
the error is O(h)
Halving the step size h approximately halves the
error of the Forward and Backward differences
The Centered difference approximation is more
accurate than the Forward and Backward
differences because the error is O(h2)
Halving the step size h approximately quarters
the error of the Centered difference.
۱٥
Richardson Extrapolation
f ( x + h) − f ( x − h)
Central Difference : f ' ( x) = + O(h 2 )
2h
۱٦
Richardson Extrapolation
Hold f ( x ) and x fixed :
f ( x + h) − f ( x − h)
φ (h) =
2h
φ ( h ) = f ' ( x ) − a2h 2 − a4h 4 − a6h 6 − ...
2 4 6
h h h h
φ ( ) = f ' ( x ) − a2 − a4 − a6 − ...
2 2 2 2
Use two derivate
h 3 4 15
φ ( h ) − 4φ ( ) = −3 f ' ( x ) − a4h − a6h 6 − ... estimates to
2 4 16 compute a third,
4 1
⇒ f ' ( x ) = φ ( h / 2) − φ ( h ) + O ( h 4 ) more accurate
3 3 approximation
۱۷
Richardson Extrapolation Example
Use the function:
۱۸
Solution
The first-derivative estimates can be computed
with centered differences as:
f (0.5 + h ) − f (0.5 − h )
φ (h) = at x = 0.5
2h
f (1) − f (0) 0.2 − 1.2
φ (0.5) = = = −1.0, | ε t | = 9.6%
1 1
f (0.75) − f (0.25)
φ (0.25) = = −0.934375, | ε t | = 2.4%
0.5
The improved estimate can be obtained by applying :
4 1 4 1
f ' (0.5) ≅ φ ( h / 2) − φ ( h ) = ( −0.934375) − ( −1) = −0.9125
3 3 3 3
Which produces the exact result for this example
۱۹
Richardson Extrapolation Table
D(0,0)=Φ(h)
D(1,0)=Φ(h/2) D(1,1)
۲۰
Richardson Extrapolation Table
h
First Column : D ( n,0) = φ n
2
Others :
4m 1
D ( n, m ) = m D ( n, m − 1) − m D( n − 1, m − 1)
4 −1 4 −1
۲۱
Example
Evaluate numerically the derivative of :
f(x) = x cos(x) at x = 0.6
۲۲
Example
First Column
f(x + h) - f(x - h)
Φ (h) =
2h
f(0.7) - f(0.5)
Φ (0.1) = = 1.08483
0.2
f(0.65) - f(0.55)
Φ (0.05) = = 1.08988
0.1
f(0.625) - f(0.575)
Φ (0.025) = = 1.09115
0.05
۲۳
Example
Richardson Table
D (0,0) = 1.08483 , D(1,0) = 1.08988, D(2,0) = 1.09115
4m 1
D ( n, m ) = m D( n, m − 1) − m D( n − 1, m − 1)
4 −1 4 −1
4 1
D(1,1) = D(1,0) − D(0 ,0) = 1.09156
3 3
4 1
D( 2 ,1) = D( 2 ,0) − D (1,0) = 1.09157
3 3
16 1
D ( 2 ,2) = D ( 2 ,1) − D (1,1) = 1.09157
15 15
۲٤
Example
Richardson Table
۲٥