You are on page 1of 46

University of Technology

Petroleum Technology Department

Numerical Analysis
Lecture 3: Numerical solution of differential equations
Semester 1 (2022-2023)

Doaa Saleh Mahdi

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

•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.
۳
Euler Method
Problem Euler Method
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)
٤
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

Local Truncation Error O(h3 )


Global Truncation Error O(h 2 )
۷
Motivation
 The midpoint can be summarized as:
 Euler method is used to estimate the solution
at the midpoint.
 The value of the rate function f(x,y) at the mid
point is calculated.
 This value is used to estimate yi+1.
 Local Truncation error of order O(h3).
 Comparable to Second order Taylor series
method.

۸
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

Use the Midpoint Method to solve the ODE


y ( x) = 1 + x 2 + y
y ( 0) = 1
Use h = 0.1. Determine y(0.1) and y(0.2)

۱٤
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

y1 = y0 + h f ( x 1, y 1) = 1 + 0.1(1 + 0.0025 + 1.1) = 1.2103


0+ 0+
2 2
Step 2 :
h
y 1 = y1 + f ( x1 , y1 ) = 1.2103 + .05(1 + 0.01 + 1.2103) = 1.3213
1+ 2
2

y2 = y1 + h f ( x 1, y 1) = 1.2103 + 0.1( 2.3438) = 1.4446


1+ 1+
2 2
۱٥
Heun’s Predictor
Corrector

۱٦
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 ) )

Local Truncation Error O(h 3 )


Global Truncation Error O(h 2 )

۱۷
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

Euler Method yi +1 = yi + h f ( xi , yi ) O (h 2 ) O (h)


Heun' s Method
Predictor : yi0+1 = yi + h f ( xi , yi ) O(h 3 ) O(h 2 )
k +1
Corrector : y = yi +
i +1
h
2
(
f ( xi , yi ) + f ( xi +1 , yik+1 ) )
h
Midpoint y 1 = yi + f ( xi , yi ) O(h 3 ) O(h 2 )
i+ 2
2

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.
 y1 ( 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. 11 − 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 . 11 − 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 . 11 − 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
 y1 ( 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
 y1 ( 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

 We have extended Euler and RK2 methods to


solve systems of first order ODEs.

 Other methods used to solve first order ODE can


be easily extended to solve systems 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

Second order ODE Two first order ODEs

۳٤
Conversion Procedure
High order ODE Convert System of first order ODEs Solve

1. Select the dependent variables


One way is to take the original dependent
variable and its derivatives up to one degree less
than the highest order derivative.
2. Write the Differential Equations in terms of
the new variables. The equations come from the
way the new variables are defined or from the
original equation.
3. Express the equations in a matrix form.
۳٥
Remarks on the Conversion Procedure
High order ODE Convert System of first order ODE Solve

1. Any nth order ODE is converted to a system of n


first order ODEs.
2. There are an infinite number of ways to select
the new variables. As a result, for each high
order ODE there are an infinite number of set of
equivalent first order systems of ODEs.
3. Use a table to make the conversion easier.

۳٦
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

1. Select a new set of variables


(Second order ODE ⇒ We need two variables)
x = z One degree less than the
highest order derivative

۳۷
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. Select a new set of variables (3 of them)


z1 = x
One degree less than the
z 2 = x highest order derivative
z3 = x
۳۹
Example of Converting a High Order
ODE to First Order ODEs
old new Initial Equation
name name cond.
x z1 4 z1 = z 2
x z2 1 z2 = z3
x z3 9 z3 = −2 z3 − 7 z 2 − 8 z1
 z1   z2   4
 z  =  z , Z (0) = 1 
 2  3   
 z3  − 2 z3 − 7 z 2 − 8 z1  9 
٤۰
Conversion Procedure for Systems of
High Order ODEs
System of high order ODEs Convert System of first order ODE Solve

1. Select the dependent variables


Take the original dependent variables and their
derivatives up to one degree less than the
highest order derivative for each variable.
2. Write the Differential Equations in terms of
the new variables. The equations come from the
way the new variables are defined or from the
original equation.
3. Express the equations in a matrix form.
٤۱
Example of Converting a High Order
ODE to First Order ODEs
Convert
x + 5x + 2 x + 8 y = 0
y + 2 xy + x = 2
x(0) = 4; x (0) = 2; x(0) = 9; y (0) = 1; y (0) = −3
1. Select a new set of variables ((3 + 2) variables)
z1 = x
z 2 = x One degree less
z3 = x
than the highest
order derivative
z4 = y
z5 = y
٤۲
Example of Converting a High Order
ODE to First Order ODEs
old new Initial Equation
name name cond.
x z1 4 z1 = z 2
x z2 2 z2 = z3
x z3 9 z3 = −5 z3 − 2 z2 − 8 z4
y z4 1 z4 = z5
y z5 −3 z5 = 2 − z2 − 2 z1 z4
٤۳
Solution of a Second Order ODE
 Solve the equation using Euler method. Use h=0.1
x + 2 x + 8 x = 2
x(0) = 1; x (0) = −2
Select a new set of variables : z1 = x, z 2 = x
The second order equation is expressed as :

  1  
z z   1 
Z = F (Z ) =   =  2
 , Z ( 0) =  
 z2  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.

 High order ODEs are converted to a set of


first order ODEs.

٤٦

You might also like