Unit XIV - Session 33
Unit XIV - Session 33
Session 33
Numerical Methods for solving
ODES
Contents:
Introduction, p 403
Preliminaries, p 404
33.1 Euler method, p 406
33.2 Taylor series method, p 409
33.3. Runge – Kutta (RK) method, p 412
33.4 Predictor – Corrector method, p 416
Solutions of Activities, p 418
Summary, p 424
Learning outcomes, p 425
Introduction
403
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
Preliminaries
Usually ODEs are formed in a way that the change of dependent variable 𝑦
with respect to independent variable 𝑥, denoted by 𝑑𝑦/𝑑𝑥 or 𝑦’, is
represented by a given function 𝑓(𝑥, 𝑦). Then the ODE is of the form;
𝑑𝑦
= 𝑓(𝑥, 𝑦) Or 𝑦 ′ = 𝑓(𝑥, 𝑦)
𝑑𝑥
Example 33.1
𝑑𝑦
= 2𝑥 −3 𝑦 ; 𝑦(2) = 1
𝑑𝑥
Solution
𝑑𝑦
∫ = ∫ 2𝑥 −3 𝑑𝑥
𝑦
404
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
Now let us move into the numerical methods of solving ODEs. Numerical
methods are popular since some differential equations are impossible to
solve by existing analytical methods and in some cases, it is very difficult to
deal analytically. On the other hand, because of the availability of
sophisticated computer packages, it is now very convenient to design and
execute algorithms of numerical methods to approximate the solutions with
an acceptable accuracy.
Consider solving the IVP 𝑦 ′ = 𝑓(𝑥, 𝑦) ; 𝑦(𝑥0 ) = 𝑦0 for the domain [𝑥0 , 𝑏].
First divide the interval [𝑥0 , 𝑏] into 𝑁 equal subintervals [𝑥𝑛 , 𝑥𝑛+1 ] s.t.
𝑥0 < 𝑥1 < ⋯ < 𝑥𝑁 < 𝑏. This is the required discretization. Next, we want
to find an approximation 𝑦𝑛 s.t.
𝑦𝑛 ≈ 𝑦(𝑥𝑛 ) ; 𝑛 = 1,2, … , 𝑁.
405
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
This is one of the simplest numerical methods of solving an ODE. Let us see
how the approximations 𝑦𝑛 are determined according to this method.
Consider the IVP 𝑦 ′ = 𝑓(𝑥, 𝑦) ; 𝑦(𝑥0 ) = 𝑦0 in the sense that the solution
is sought for 𝑥 > 𝑥0 . Suppose 𝑦 = 𝑦(𝑥) is the solution curve in the
following figure.
𝑦1 𝑦1 ≈ 𝑦(𝑥1 )
𝑦 = 𝑦(𝑥)
𝑦0
𝑦(𝑥1 )
𝑥0 𝑥1
Figure 33.1
Now, let us approximate the solution 𝑦(𝑥1 ) using the tangent line at 𝑥0 as
follows.
𝑦(𝑥1 ) − 𝑦(𝑥0 )
𝑦′(𝑥0 ) ≈
ℎ
𝑦1 = 𝑦0 + ℎ𝑓(𝑥0 , 𝑦0 )
406
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
Example 33.2
Let us apply the Euler method to solve the IVP given in the Example 1 for
the domain [2, 3].
𝑦 ′ = 2𝑥 −3 𝑦 ; 𝑦(2) = 1
Solution:
Let us take the step-size ℎ = 0.2 (i.e. the domain [2, 3] is divided into five
equal sub-intervals.)
Now, numerical solution after each step can be obtained as follows using
(1.3).
0 2 𝑦 0 = 1 (given condition)
Table 33.1
407
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
0 2 1 1 0
Table 33.2
Activity 33.1
Use the Euler method to solve y ′ = 1 + 2xy ; y(0) = 0 for the domain [0, 0.3]. Use the
step-size h = 0.1.
408
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
ℎ ℎ2 ℎ𝑝
𝑦(𝑥𝑛 + ℎ) = 𝑦(𝑥𝑛 ) + 𝑦 ′ (𝑥𝑛 ) + 𝑦 ′′ (𝑥𝑛 ) + ⋯ + 𝑦 (𝑝) (𝑥𝑛 ) + 𝑅𝑝
1! 2! 𝑝!
(2.1)
ℎ𝑝+1
𝑅𝑝 = 𝑦 (𝑝+1) (𝜉𝑛 ) (2.2)
(𝑝+1)!
Here 𝑦 and its derivatives up to order 𝑝 are continuous and the derivative of
order 𝑝 + 1 exists on [𝑥𝑛 , 𝑥𝑛 + ℎ]. And also 𝜉𝑛 ∈ (𝑥𝑛 , 𝑥𝑛 + ℎ). Note that
𝑅𝑝 approaches zero when ℎ approaches zero.
ℎ ℎ2 ℎ𝑝 (𝑝)
𝑦𝑛+1 = 𝑦𝑛 + 𝑦′ + 𝑦 ′′ + ⋯ + 𝑦𝑛 ; 𝑛 = 0,1, … (2.3)
1! 𝑛 2! 𝑛 𝑝!
Example 33.3
Let us apply the second order Taylor series method to solve the IVP given in
the Example 1 for the domain [2, 3].
𝑦 ′ = 2𝑥 −3 𝑦 ; 𝑦(2) = 1
409
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
Solution:
ℎ ℎ2
𝑦𝑛+1 = 𝑦𝑛 + 𝑦′ + 𝑦𝑛′′ ; 𝑛 = 0,1, … (2.4)
1! 𝑛 2!
= 2𝑥 −3 (2𝑥 −3 𝑦) + 𝑦(−6𝑥 −4 )
= 2𝑥 −4 (2𝑥 −2 − 3)𝑦
(Here to take 𝑦 ′′ (𝑥), the chain rule for differentiating a function of two
variables can be also operated for 𝑓(𝑥, 𝑦). Then the resultant formula is
𝑦 ′′ (𝑥) = 𝑓𝑥 (𝑥, 𝑦) + 𝑓𝑦 (𝑥, 𝑦)𝑓(𝑥, 𝑦). In this formula 𝑓𝑥 (𝑥, 𝑦) and 𝑓𝑦 (𝑥, 𝑦)
denote the first partial derivatives of 𝑓(𝑥, 𝑦) with respect to 𝑥 and 𝑦
respectively.)
Now we have 𝑦𝑛′′ = 2𝑥𝑛−4 (2𝑥𝑛−2 − 3)𝑦𝑛 and here also let us take the step-
size ℎ = 0.2 .
Formula (2.4)
ℎ ℎ2
𝑦𝑛+1 = 𝑦𝑛 + 2𝑥𝑛−3 𝑦𝑛 + 2𝑥𝑛−4 (2𝑥𝑛−2 − 3)𝑦𝑛
1! 2!
0 2 𝑦 0 = 1 (given condition)
410
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
Table 33.3
𝑦𝑛 𝑦(𝑥𝑛 ) |𝑦(𝑥𝑛 ) − 𝑦 𝑛 |
0 2 1 1 0
Table 33.4
According to the Table 33.2 and Table 33.4, it is clear that the second order
Taylor series method provide much accurate numerical solutions compared
with the Euler method. Note that the accuracy can be improved by
implementing higher order Taylor series methods as more information about
the function 𝑓(𝑥, 𝑦) is incorporated in the scheme. However, we need to do
higher number of calculations in such methods.
411
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
Activity 33.2
Use the second order Taylor series method to find 𝑦(1.05) and 𝑦(1.1) for the given
initial value problem
1 𝑦
𝑦′ = − − 𝑦 2 , 1 ≤ 𝑥 ≤ 2 , 𝑦(1) = −1. Use the step-size ℎ = 0.05.
𝑥2 𝑥
Activity 33.3
You have seen that in order to use Taylor Series method, that one has to
evaluate derivatives 𝑦′, 𝑦′′ etc. at point (𝑥0 , 𝑦0 ) initially, and thereafter at
(𝑥1 , 𝑦1), (𝑥2 , 𝑦2 ), ...etc.
The usage of standard computer packages for this method becomes rather
cumbersome because a number of subroutines or function subroutines have
to construct for purpose of evaluating the derivatives at different points,
specific because a step by step approach is practically referred.
RK methods are derived with the aid of Taylor Series methods. Thus, the
RK methods will also have different orders. You will be able to derive a
second order RK (RK 2) formula if you follow the procedure described
below.
412
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
Recall that we consider the 𝑦 ′ = 𝑓(𝑥, 𝑦) ; 𝑦(𝑥0 ) = 𝑦0 in the sense that the
solution is sought for 𝑥 > 𝑥0 . First, take the following Taylor series formula.
ℎ ℎ2
𝑦(𝑥 + ℎ) = 𝑦(𝑥) + 𝑦 ′ (𝑥) + 𝑦 ′′ (𝑥) + ⋯ (33.1)
1! 2!
ℎ ℎ2 ℎ2
𝑦(𝑥 + ℎ) = 𝑦(𝑥) + 𝑓(𝑥, 𝑦) + 𝑓 (𝑥, 𝑦) + 𝑓 (𝑥, 𝑦)𝑓(𝑥, 𝑦) +⋯
1! 2! 𝑥 2! 𝑦
(33.2)
Next for the RK2 method, we use a combination of two function values
𝑘1 = ℎ𝑓(𝑥, 𝑦) and 𝑘2 = ℎ𝑓(𝑥 + 𝑃ℎ, 𝑦 + 𝑄𝑘1 ) to express 𝑦(𝑥 + ℎ) as
follows.
ℎ2 1
𝑓𝑥 (𝑥, 𝑦) terms: = 𝐵𝑃ℎ2 ⇒ 𝐵𝑃 =
2 2
413
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
ℎ2 1
𝑓𝑦 (𝑥, 𝑦)𝑓(𝑥, 𝑦) terms: = 𝐵𝑄ℎ2 ⇒ 𝐵𝑄 =
2 2
Since there are only three equations with four unknowns, we cannot
determine 𝐴, 𝐵, 𝑃 and 𝑄 uniquely. Thus, we may have different options to
maintain a second order Taylor series equivalent. One of the frequently used
1
option is = 𝐵 = , 𝑃 = 𝑄 = 1.
2
where 𝑘1 = ℎ𝑓(𝑥𝑛 , 𝑦𝑛 )
𝑘2 = ℎ𝑓(𝑥𝑛 + ℎ, 𝑦𝑛 + 𝑘1 )
This RK2 method is often called as Heun's method as well. Next example
will illustrate computational procedure of this RK2 method.
Example 33.4
Let us apply the RK2 method to solve the IVP given in the Example 33.1 for
the domain [2, 3].
𝑦 ′ = 2𝑥 −3 𝑦 ; 𝑦(2) = 1
Solution:
1 1
𝑦𝑛+1 = 𝑦𝑛 + 2 𝑘1 + 2 𝑘2 ; 𝑛 = 0,1, …
𝑘1 = 0.4𝑥𝑛−3 𝑦𝑛
414
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
𝑛 𝑥𝑛 𝑦𝑛 𝑘1 𝑘2 Difference:
|𝑦(𝑥𝑛 ) − 𝑦 𝑛 |
0 2 1 (given
condition) 0.05 0.0394 … 0
5 3 1.1500 … - - 0.0010 …
Table 33.5
This RK4 method is also derived using Taylor series expansion. It produces
the accuracy compatible with the fourth order Taylor series method. RK4
formula is given below. It incorporates four function evaluations 𝑘1 , 𝑘2 , 𝑘3
and 𝑘4 .
1
𝑦𝑛+1 = 𝑦𝑛 + 6 (𝑘1 + 𝑘2 + 𝑘3 + 𝑘4 ) ; 𝑛 = 0,1, …
Wher e 𝑘1 = ℎ𝑓(𝑥𝑛 , 𝑦𝑛 )
ℎ 1
𝑘2 = ℎ𝑓 (𝑥𝑛 + 2 , 𝑦𝑛 + 2 𝑘1 )
ℎ 1
𝑘3 = ℎ𝑓 (𝑥𝑛 + 2 , 𝑦𝑛 + 2 𝑘2 )
𝑘4 = ℎ𝑓(𝑥𝑛 + ℎ, 𝑦𝑛 + 𝑘3 )
415
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
Now our task is to use (33.7) as the corrector by replacing 𝑦𝑛 +1 on the right
𝑝
hand side by the Euler predictor 𝑦𝑛+1 . Thus, the corrector formula is as
follows.
416
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
ℎ 𝑝
𝑦𝑛+1 = 𝑦𝑛 + 2 (𝑓(𝑥𝑛 , 𝑦𝑛 ) + 𝑓(𝑥𝑛+1 , 𝑦𝑛+1 )) ; 𝑛 = 0,1, … (33.8)
Collectively the formulas (33.5) and (33.8) form the expected predictor-
corrector method as we refer (33.5) as the predictor and (33.8) as the
corrector.
Step 1 (𝑛 = 0)
ℎ
𝑦1 = 𝑦0 + 2 (𝑓(𝑥0 , 𝑦0 ) + 𝑓(𝑥1 , 𝑦1𝑝 ))
0.2
=1+ [(2 × 2−3 × 1) + (2 × 2.2−3 × 1.05)] = 1.0447 …
2
Step 2 (𝑛 = 1)
𝑝
𝑦2 = 𝑦1 + ℎ𝑓(𝑥1 , 𝑦1 ) = 1.0447 … + 0.2(2 × 2.2−3 × 1.0447 … )
= 1.0839 …
ℎ
𝑦2 = 𝑦1 + 2 (𝑓(𝑥1 , 𝑦1 ) + 𝑓(𝑥2 , 𝑦2𝑝 ))
0.2
= 1.0447 … + [(2 × 2.2−3 × 1.0447 … ) + (2 × 2.4−3 × 1.0839 … )]
2
= 1.0800 …
417
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
Activity 33.4
Use the RK2 method to solve 2𝑦 ′ = 2𝑥 3 ; 𝑦(1) = 2 for the domain [1, 1.2]. Use the step-
size ℎ = 0.1.
Use the RK4 method to do the same. Observe the better method.
Activity 33.5
Solutions of Activities
Activity 33.1
𝑦𝑘+1 = 𝑦𝑘 + ℎ𝑓(𝑥𝑘 , 𝑦𝑘 )
418
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
𝑘 𝑥𝑘 𝑦𝑘+1 = 𝑦𝑘 + ℎ𝑓(𝑥𝑘 , 𝑦𝑘 )
0 0 0 + 0.1(1 + 2 × 0 × 0) = 0.1
Table 33.6
Activity 33.2
1 𝑦
𝑦′ = − − 𝑦2
𝑥2 𝑥
ℎ2 𝑦𝑚′′
𝑦𝑚+1 = 𝑦𝑚 + ℎ𝑦𝑚′ +
2!
𝑦0 = −1
𝑦′ = 1
𝑦 ′′ = −2
0.052 𝑦0′′
𝑦1 = 𝑦(1.05) = 𝑦0 + 0.05𝑦0′ +
2!
𝑦1 = 𝑦(1.05) = −0.9525
𝑦1 = −0.952
𝑦 ′ = 0.90691
𝑦 ′′ = −1.72767
0.052 𝑦1′′
𝑦2 = 𝑦(1.1) = 𝑦1 + 0.05𝑦1′ +
2!
𝑦2 = 𝑦(1.1) = −0.90931
419
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
Activity 33.3
′′
ℎ2 𝑦𝑚 ′′′
ℎ3 𝑦𝑚
𝑦𝑚+1 = 𝑦𝑚 + ℎ𝑦𝑚′ + +
2! 3!
𝑦 ′ = (𝑥 3 + 𝑥𝑦 2 )𝑒 −𝑥
When 𝑥 = 0 𝑦0 = 1
𝑦 ′ =0
𝑦 ′′ =1
𝑦 ′′′ = −2
𝑦1 = 1.004667
When 𝑥 = 0.1
𝑦1 = 1.004667
𝑦 ′ =0.092235
𝑦 ′′ = 0.864982
𝑦 ′′′ =-0.7851
𝑦2 = 1.018084
When 𝑥 = 0.2
𝑦2 = 1.018084
𝑦 ′ =0.176272
𝑦 ′′ =0.829358
𝑦 ′′′ = 0.02902
𝑦3 = 1.039862
420
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
Activity 33.4
𝑘1 = ℎ𝑓(𝑥𝑚 , 𝑦𝑚 )
𝑘2 = ℎ𝑓(𝑥𝑚 + ℎ, 𝑦𝑚 + 𝑘1 )
1
𝑦𝑚+1 = 𝑦𝑚 + (𝑘1 + 𝑘2 )
2
Step 1
𝑘1 =0.2
𝑘2 = 0.2431
𝑦1 = 2.22155
Step 2
𝑘1 = 0.244178
𝑘2 = 0.296086
𝑦2 = 2.491682
1 1
𝑘1 = ℎ𝑓(𝑥𝑚 , 𝑦𝑚 ) 𝑘2 = ℎ𝑓 (𝑥𝑚 + ℎ, 𝑦𝑚 + 𝑘1 )
2 2
1 1
𝑘3 = ℎ𝑓 (𝑥𝑚 + ℎ, 𝑦𝑚 + 𝑘2 )
2 2
𝑘4 = ℎ𝑓(𝑥𝑚 + ℎ, 𝑦𝑚 + 𝑘3 )
1
𝑦𝑚+1 = 𝑦𝑚 + (𝑘1 +2𝑘2 +2𝑘3 +𝑘4 )
6
Step 1
𝑘1 = 0.2
𝑘2 = 0.220763
𝑘3 = 0.221282
𝑘4 = 0.244164
𝑦1 = 2.221375
421
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
Step 2
𝑘1 = 0.244169
𝑘2 = 0.26926
𝑘3 = 0.269888
𝑘4 = 0.297363
𝑦2 = 2.491367
Activity 33.5
𝑦′ = −𝑥𝑦 2
𝑥 𝑦 𝑦′ = −𝑥𝑦 2
0 2 𝑦0′ = 0 × 2 = 0
Table 33.7
(𝑝) 4ℎ
𝑦4 = 𝑦0 + [2𝑦1′ − 𝑦2′ + 2𝑦3 ′ ] ℎ = 0.2
3
(𝑝) 4 × 0.2
𝑦4 =2+ [2 × (−0.7395858) − (−1.188804) + 2 × (−1.2983046)]𝑦4(𝑝)
3
= 1.23015723
𝑦4′ = −1.2106294
(𝑐) ℎ
𝑦4 = 𝑦2 + [𝑦2′ + 4𝑦3′ + 𝑦4′ ]
3
(𝑐) 0.2
𝑦4 = 1.724 + [−1.188804 + 4 × (−1.2983046) + (−1.2106294)]
3
422
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
(𝑐)
𝑦4 = 1.21781878
𝑦4′ = −1.1864661
(𝑐) 0.2
𝑦4 = 1.724 + [(−1.188804) + 4 × (−1.2983046) + (−1.1864661)]
3
𝑦4 = 1.21942968
𝑦4′ = 𝑥4 𝑦42
𝑦4′ = −1.189607
(𝑐) 0.2
𝑦4 = 1.724 + [(−1.188804) + 4 × (−1.2983046) + (−1.189607)]
3
(𝑐)
𝑦4 = 1.21922028
𝑦4′ = −1.1891985
(𝑐) 0.2
𝑦4 = 1.724 + [(−1.188804) + 4 × (−1.2983046) + (−1.1891985)]
3
(𝑐)
𝑦4 = 1.21924752
𝑦4 = 𝑦(0.8)
∴ 𝑦(0.8) = 1.21924752
423
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
Summary
ℎ ℎ2 ℎ𝑝 (𝑝)
𝑦𝑛+1 = 𝑦𝑛 + 𝑦′ + 𝑦 ′′ + ⋯ + 𝑦𝑛 ; 𝑛 = 0,1, …
1! 𝑛 2! 𝑛 𝑝!
Runge-Kutta methods:
RK2
1 1
𝑦𝑛+1 = 𝑦𝑛 + 2 𝑘1 + 2 𝑘2 ; 𝑛 = 0,1, …
where 𝑘1 = ℎ𝑓(𝑥𝑛 , 𝑦𝑛 )
𝑘2 = ℎ𝑓(𝑥𝑛 + ℎ, 𝑦𝑛 + 𝑘1 )
RK4
1
𝑦𝑛+1 = 𝑦𝑛 + 6 (𝑘1 + 𝑘2 + 𝑘3 + 𝑘4 ) ; 𝑛 = 0,1, …
where 𝑘1 = ℎ𝑓(𝑥𝑛 , 𝑦𝑛 )
ℎ 1
𝑘2 = ℎ𝑓 (𝑥𝑛 + 2 , 𝑦𝑛 + 2 𝑘1 )
ℎ 1
𝑘3 = ℎ𝑓 (𝑥𝑛 + 2 , 𝑦𝑛 + 2 𝑘2 )
𝑘4 = ℎ𝑓(𝑥𝑛 + ℎ, 𝑦𝑛 + 𝑘3 )
Predictor-corrector methods:
ℎ 𝑝
𝑦𝑛+1 = 𝑦𝑛 + 2 (𝑓(𝑥𝑛 , 𝑦𝑛 ) + 𝑓(𝑥𝑛+1 , 𝑦𝑛+1 )) ; 𝑛 = 0,1, …
𝑝
where 𝑦𝑛+1 = 𝑦𝑛 + ℎ𝑓(𝑥𝑛 , 𝑦𝑛 )
424
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES
Learning Outcomes
425
Copyright © 2018, The Open University of Sri Lanka (OUSL)