Professional Documents
Culture Documents
Numerical Analysis
Lecture 3: Numerical solution of differential equations
Semester 1 (2022-2023)
CISE301_Topic8L1 ۱
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.
۲
Topics Lecture
2
Local Truncation Error O(h )
Global Truncation Error O(h)
٤
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.
٥
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.
٦
Midpoint Method
Problem Midpoint Method
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
۸
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
۹
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
۱۰
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
۱۱
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
۱۲
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
۱۳
Example 1
۱٤
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
۱٦
Heun’s Predictor Corrector Method
Problem Heun ' s Method
y ( x ) = f ( x, y ) y0 = y ( x0 )
y ( x0 ) = y0 Predictor : yi0+1 = yi + h f ( xi , yi )
Corrector : yi1+1 = yi +
h
2
(
f ( xi , yi ) + f ( xi +1, yi0+1 ) )
۱۷
Heun’s Predictor Corrector
(Prediction)
( xi +1 , yi0+1 )
( xi , yi )
xi xi +1
Prediction 0
yi +1 = yi + h f ( xi , yi )
۱۸
Heun’s Predictor Corrector
(Prediction)
slope = f ( xi , yi ) ( xi +1 , yi0+1 )
( xi , yi )
xi xi +1
Prediction y 0
i +1 = yi + h f ( xi , yi )
۱۹
Heun’s Predictor Corrector
(Correction)
f ( xi , yi ) + f ( xi +1 , yi0+1 )
slope =
2
( xi +1 , yi0+1 )
( xi , yi ) ( xi +1 , yi1+1 )
xi xi +1
y 1
i +1 = yi +
h
2
(
f ( xi , yi ) + f ( xi +1 , yi0+1 ) )
۲۰
Example 2
Use the Heun' s Method 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)
۲۱
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
۲۲
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.
۲۳
Comparison
Method Local
truncation
Global
truncation
error error
yi +1 = yi + h f ( x 1 ,y 1 )
i+ i+
2 2
۲٤
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.
۲٥
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)
۲٦
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)
۲۷
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.
۲۸
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 + h) + h F (Y ( x0 + h), x0 + h)
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
۲۹
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 Method 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
۳۰
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
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
۳۱
Methods for Solving a System of First Order ODEs
۳۲
High Order ODEs
How do solve a second order ODE?
x + 3 x + 6 x = 1
How do solve high order ODEs?
۳۳
The General Approach to Solve ODEs
High order ODE Convert System of first order ODEs Solve
x z
x + 3 x + 6 x = 1 z = 1 − 3 z − 6 x ,
Convert
Solve
x (0) = 1; x(0) = 4
z (0) = 0, x(0) = 0
۳٤
Conversion Procedure
High order ODE Convert System of first order ODEs Solve
۳٦
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 system of first order ODEs
۳۷
Example of Converting a High Order
ODE to First Order ODEs
x z
=
z 1 − 3 z − 6 x ,
z (0) = 0, x(0) = 0
۳۸
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
٤٤
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.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
٤٥
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.
٤٦