0% found this document useful (0 votes)
47 views23 pages

Unit XIV - Session 33

Uploaded by

prasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views23 pages

Unit XIV - Session 33

Uploaded by

prasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Unit 14: Numerical Methods for solving ODES and PDES

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

Here our focus is on numerical methods of solving first order ordinary


differential equations (ODEs) since higher order differential equations can
be reduced to a system of first order ODEs. Recall that in any ODE, we
have only one independent variable. Discretizing the domain of the
independent variable (separating into a set of discrete points) and
formulating different ways of approximating the dependent variable are the
key aspects here.

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 𝑦 ′ = 𝑓(𝑥, 𝑦)
𝑑𝑥

The general solution of such an equation requires one arbitrary constant. If


one condition such as, when 𝑥 = 𝑥0 , 𝑦 = 𝑦0 is given, then 𝑦 can be found
as a particular solution. When a differential equation is given along with
such a condition, it is also called as an Initial Value Problem (IVP). What
we expect by a solution of a differential equation is a relationship between
the dependent and independent variables.

Example 33.1

To recall an analytical way of solving an ODE, consider the following


problem. Note that a condition is given here, when = 2 , 𝑦 = 1 denoted by
𝑦(2) = 1.

𝑑𝑦
= 2𝑥 −3 𝑦 ; 𝑦(2) = 1
𝑑𝑥

Solution

𝑑𝑦
∫ = ∫ 2𝑥 −3 𝑑𝑥
𝑦

𝑙𝑛 |𝑦| = −𝑥 −2 + 𝑐 ; 𝑐 - arbitrary constant

This relationship can be considered as the general solution.

For a particular solution, substitute = 2 , 𝑦 = 1 in the general solution to


obtain𝑐.

𝑙𝑛 |1| = −2−2 + 𝑐 → 𝑐 = 0.25

404
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES

Particular solution: 𝑙𝑛 |𝑦| = −𝑥 −2 + 0.25

If we want to explicitly provide 𝑦 as a function of 𝑥, the following


simplification is possible.
−2 +0.25 −2 +0.25
|𝑦| = 𝑒 −𝑥 → 𝑦 = ± 𝑒 −𝑥

Since the condition 𝑦(2) = 1 should be satisfied, we have to restrict to the


−2 +0.25
solution 𝑦 = 𝑒 −𝑥 .

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.

Preparing for numerical methods

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, … , 𝑁.

Length of each subinterval taken above is called the step-size (h).


i.e. 𝑥𝑛+1 = 𝑥𝑛 + ℎ . This type of approach is known commonly as a
numerical method or as a finite difference method of solving an ODE.

Subsequently, we can make use of these discrete values to find approximate


values of 𝑦(𝑡) at any point 𝑥 ∈ [𝑥0 , 𝑏], assuming that 𝑦(𝑡) is linear within
each subinterval.

405
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES

33.1 Euler method

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 )

∴ 𝑦(𝑥1 ) ≈ 𝑦(𝑥0 ) + ℎ𝑓(𝑥0 , 𝑦(𝑥0 ))

Taking the right-hand side as an approximation for 𝑦(𝑥1 ) and denoting it as


𝑦1 , we can write

𝑦1 = 𝑦0 + ℎ𝑓(𝑥0 , 𝑦0 )

By repeating this procedure, we approximate the solution 𝑦(𝑥𝑛+1 ) by 𝑦𝑛+1


as follows.

𝑦𝑛+1 = 𝑦𝑛 + ℎ𝑓(𝑥𝑛 , 𝑦𝑛 ) ; 𝑛 = 0,1, … (1.1)

406
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES

This method is called the Euler method.

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:

Euler formula: 𝑦𝑛+1 = 𝑦𝑛 + ℎ𝑓(𝑥𝑛 , 𝑦𝑛 ) ; 𝑛 = 0,1, … (1.2)

Here, 𝑓(𝑥𝑛 , 𝑦𝑛 ) = 2𝑥𝑛−3 𝑦𝑛 , 𝑥0 = 2 and 𝑦0 = 1.

Let us take the step-size ℎ = 0.2 (i.e. the domain [2, 3] is divided into five
equal sub-intervals.)

Formula (1.2)  𝑦𝑛 + 1 = 𝑦𝑛 + 0.2 (2𝑥𝑛−3 𝑦𝑛 ) ; 𝑛 = 0,1, …

or by further simplifying 𝑦𝑛 + 1 = 𝑦𝑛 (1 + 0.4 𝑥𝑛−3 ) ; 𝑛 = 0,1, … (1.3)

Now, numerical solution after each step can be obtained as follows using
(1.3).

𝑛 𝑥𝑛 Numerical solution: 𝑦𝑛 + 1 = 𝑦𝑛 (1 + 0.4 𝑥𝑛−3 )

0 2 𝑦 0 = 1 (given condition)

1 2.2 𝑦 1 = 𝑦0 (1 + 0.4 𝑥0−3 ) = 1 (1 + 0.4 × 2−3 ) = 1.05

2 2.4 𝑦 2 = 𝑦1 (1 + 0.4 𝑥1−3 ) = 1.05 (1 + 0.4 × 2.2−3 ) = 1.0894 …

3 2.6 𝑦 3 = 𝑦2 (1 + 0.4 𝑥2−3 ) = 1.0894 … (1 + 0.4 × 2.4−3 )


= 1.1209 …

4 2.8 𝑦 4 = 𝑦3 (1 + 0.4 𝑥3−3 ) = 1.1209 … (1 + 0.4 × 2.6−3 )


= 1.1464 …

5 3 𝑦 5 = 𝑦4 (1 + 0.4 𝑥4−3 ) = 1.1464 … (1 + 0.4 × 2.8−3 )


= 1.1673 …

Table 33.1

407
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES

To observe the accuracy of numerical solutions, we can compare it with the


−2 +0.25
exact solution 𝑦 = 𝑒 −𝑥 obtained in Example 33.1.

𝑛 𝑥𝑛 Numerical solution 𝑦 𝑛 Exact solution 𝑦(𝑥𝑛 ) Difference: |𝑦(𝑥𝑛 ) − 𝑦 𝑛 |

0 2 1 1 0

1 2.2 1.05 1.0443. .. 0.0056. ..

2 2.4 1.0894 … 1.0793. .. 0.0100. ..

3 2.6 1.1209 … 1.1074. .. 0.0135. ..

4 2.8 1.1464 … 1.1302. .. 0.0162. ..

5 3 1.1673 … 1.1489. .. 0.0183. ..

Table 33.2

According to the Table 33.2 it is evident that the numerical solution is


reasonably accurate. Note that the accuracy can be improved by reducing
the step-size. Meanwhile calculations should be carefully carried out as
lesser step-sizes direct more calculation steps which is vulnerable for error
propagation due to rounding-off. This may usually occur if you round-off
the answers at the end of each step.

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.

Let us now study a more general method.

408
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES

33.2 Taylor series Method

You should be quickly familiar with Taylor’s formula

ℎ ℎ2 ℎ𝑝
𝑦(𝑥𝑛 + ℎ) = 𝑦(𝑥𝑛 ) + 𝑦 ′ (𝑥𝑛 ) + 𝑦 ′′ (𝑥𝑛 ) + ⋯ + 𝑦 (𝑝) (𝑥𝑛 ) + 𝑅𝑝
1! 2! 𝑝!

(2.1)

where the remainder term 𝑅𝑝 can be written as

ℎ𝑝+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.

Now we drop the remainder term in (2.1) to produce a numerical scheme


called 𝒑th order Taylor series method which has the following form (2.3).
Then 𝑅𝑝 is referred as truncation error and note that 𝑅𝑝 approaches zero
when the step-size ℎ approaches zero.

ℎ ℎ2 ℎ𝑝 (𝑝)
𝑦𝑛+1 = 𝑦𝑛 + 𝑦′ + 𝑦 ′′ + ⋯ + 𝑦𝑛 ; 𝑛 = 0,1, … (2.3)
1! 𝑛 2! 𝑛 𝑝!

Here 𝑦𝑛′ = 𝑓(𝑥𝑛 , 𝑦𝑛 ) and the values of higher order derivatives


(𝑝)
(𝑦𝑛′′ , 𝑦𝑛′′′ , … , 𝑦𝑛 ) should be determined by differentiating 𝑓(𝑥, 𝑦)
sucessively. Note: The first order Taylor series method is same as the
Euler method. Taylor series scheme (2.3) can now be applied step by step to
obtain the numerical solution as illustrated in the following example.

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:

Second order Taylor series scheme:

ℎ ℎ2
𝑦𝑛+1 = 𝑦𝑛 + 𝑦′ + 𝑦𝑛′′ ; 𝑛 = 0,1, … (2.4)
1! 𝑛 2!

Here, 𝑦𝑛′ = 𝑓(𝑥𝑛 , 𝑦𝑛 ) = 2𝑥𝑛−3 𝑦𝑛 ,

For 𝑦𝑛′′ , we need 𝑦 ′′ (𝑥) which would be obtained as follows.


𝑑 𝑑 𝑑𝑦 𝑑
𝑦 ′′ (𝑥) = 𝑑𝑥 𝑓(𝑥, 𝑦) = 𝑑𝑥 2𝑥 −3 𝑦 = 2𝑥 −3 𝑑𝑥 + 𝑦 𝑑𝑥 2𝑥 −3

= 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!

𝑦𝑛+1 = [1 + 0.4𝑥𝑛−3 + 0.04𝑥𝑛−4 (2𝑥𝑛−2 − 3)]𝑦𝑛 ; 𝑛 = 0,1, … (2.5)

Now by initiating with 𝑥0 = 2 and 𝑦0 = 1, numerical solution after each


step can be obtained as follows using (2.5).

𝑛 𝑥𝑛 Numerical solution: 𝑦𝑛 + 1 = [1 + 0.4𝑥𝑛−3 + 0.04𝑥𝑛−4 (2𝑥𝑛−2 − 3)]𝑦𝑛

0 2 𝑦 0 = 1 (given condition)

1 2.2 𝑦 1 = [1 + 0.4𝑥0−3 + 0.04𝑥0−4 (2𝑥0−2 − 3)]𝑦0 = 1.0437 …

2 2.4 𝑦 2 = [1 + 0.4𝑥1−3 + 0.04𝑥1−4 (2𝑥1−2 − 3)]𝑦1 = 1.0783 …

3 2.6 𝑦 3 = [1 + 0.4𝑥2−3 + 0.04𝑥2−4 (2𝑥2−2 − 3)]𝑦2 = 1.1061 …

410
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES

4 2.8 𝑦 4 = [1 + 0.4𝑥3−3 + 0.04𝑥3−4 (2𝑥3−2 − 3)]𝑦3 = 1.1286 …

5 3 𝑦 5 = [1 + 0.4𝑥4−3 + 0.04𝑥4−4 (2𝑥4−2 − 3)]𝑦4 = 1.1472 …

Table 33.3

As we did in the Example 2, numerical solutions can be compared with the


−2 +0.25
exact solution 𝑦 = 𝑒 −𝑥 .

𝑛 𝑥𝑛 Numerical solution Exact solution Difference:

𝑦𝑛 𝑦(𝑥𝑛 ) |𝑦(𝑥𝑛 ) − 𝑦 𝑛 |

0 2 1 1 0

1 2.2 1.0437 … 1.0443. .. 0.0005. ..

2 2.4 1.0783 … 1.0793. .. 0.0010. ..

3 2.6 1.1061 … 1.1074. .. 0.0013. ..

4 2.8 1.1286 … 1.1302. .. 0.0016. ..

5 3 1.1472 … 1.1489. .. 0.0017. ..

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

Use the third order Taylor series method to solve


𝑦′ = (𝑥3 + 𝑥𝑦2 )𝑒−𝑥 , 𝑦(0) = 1 for 𝑥 = 0.1, 0.2, Use the step-size ℎ = 0.1.

33.3 Runge - Kutta (RK) Methods

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.

In the RK methods, in order to go to 𝑦𝑛+1 from 𝑦𝑛 instead of evaluating


derivatives at(𝑥𝑛 , 𝑦𝑛) , the function 𝑓(𝑥, 𝑦) may have to be evaluated at
(𝑥𝑛 , 𝑦𝑛 ) and several other points around (𝑥𝑛 , 𝑦𝑛 ).

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

33.3.1 Second order Runge – Kutta (RK2) method

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!

Now the derivatives in (3.1) has to be replaced by the formulas 𝑦 ′ = 𝑓(𝑥, 𝑦)


and 𝑦 ′′ (𝑥) = 𝑓𝑥 (𝑥, 𝑦) + 𝑓𝑦 (𝑥, 𝑦)𝑓(𝑥, 𝑦). Then the new version takes to
following form.

ℎ ℎ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.

𝑦(𝑥 + ℎ) = 𝑦(𝑥) + 𝐴𝑘1 + 𝐵𝑘2 (33.3)

For further manipulations, 𝑘2 should be expanded using Taylor series for


two variable functions to obtain the following formula.

𝑘2 = ℎ[𝑓(𝑥, 𝑦) + 𝑃ℎ𝑓𝑥 (𝑥, 𝑦) + 𝑄𝑘1 𝑓𝑦 (𝑥, 𝑦) + ⋯ ]

= ℎ𝑓(𝑥, 𝑦) + 𝑃ℎ2 𝑓𝑥 (𝑥, 𝑦) + 𝑄ℎ2 𝑓(𝑥, 𝑦)𝑓𝑦 (𝑥, 𝑦) + ⋯

Now by substituting into (33.3), we have

𝑦(𝑥 + ℎ) = 𝑦(𝑥) + (𝐴 + 𝐵)ℎ𝑓(𝑥, 𝑦) + 𝐵𝑃ℎ2 𝑓𝑥 (𝑥, 𝑦) +


𝐵𝑄ℎ2 𝑓(𝑥, 𝑦)𝑓𝑦 (𝑥, 𝑦) + ⋯ (33.4)

By comparing similar terms in (33.2) and (33.4), the following relationships


can be obtained.

𝑓(𝑥, 𝑦) terms: ℎ = (𝐴 + 𝐵)ℎ ⇒ 𝐴 + 𝐵 = 1

ℎ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

Then we arrive at the following RK2 formula from (33.3).


1 1
𝑦𝑛+1 = 𝑦𝑛 + 2 𝑘1 + 2 𝑘2 ; 𝑛 = 0,1, …

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, …

where 𝑘1 = ℎ𝑓(𝑥𝑛 , 𝑦𝑛 ) = ℎ(2𝑥𝑛−3 𝑦𝑛 )

𝑘2 = ℎ𝑓(𝑥𝑛 + ℎ, 𝑦𝑛 + 𝑘1 ) = ℎ[2(𝑥𝑛 + ℎ)−3 (𝑦𝑛 + 𝑘1 )]

Let us take the step-size ℎ = 0.2. Then we have

𝑘1 = 0.4𝑥𝑛−3 𝑦𝑛

𝑘2 = 0.4[(𝑥𝑛 + 0.2)−3 (𝑦𝑛 + 𝑘1 )]

414
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES

Now by initiating with 𝑥0 = 2 and 𝑦0 = 1, numerical solution 𝑦𝑛 after each


step can be obtained as in the following table. Comparison with the exact
solution 𝑦(𝑥𝑛 ) is also given in the last column to observe the accuracy of the
method.

𝑛 𝑥𝑛 𝑦𝑛 𝑘1 𝑘2 Difference:

|𝑦(𝑥𝑛 ) − 𝑦 𝑛 |

0 2 1 (given
condition) 0.05 0.0394 … 0

1 2.2 1.0447 … 0.0392 … 0.0313 … 0.00037 …

2 2.4 1.0800 … 0.0312 … 0.0252 … 0.00064 …

3 2.6 1.1082 … 0.0252 … 0.0206 … 0.00083 …

4 2.8 1.1312 … 0.0206 … 0.0170 … 0.00097 …

5 3 1.1500 … - - 0.0010 …

Table 33.5

33.3.2 Fourth order Runge - Kutta (RK4) method

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

33.4 Predictor – Corrector method

In this method it is expected to achieve higher accuracy by correcting the


value obtained for the dependent variable (𝑦) at each point 𝑥. Hence, we
can choose one method for predicting the value and another method for
correcting it in order to reduce the error propagation. Since there are number
of Predictor – Corrector rules, we shall confirm ourselves to one of the
simplest form here.

Let us consider the Euler formula 𝑦𝑛 +1 = 𝑦𝑛 + ℎ𝑓(𝑥𝑛 , 𝑦𝑛 ) ; 𝑛 = 0,1, … in


solving 𝑦 ′ = 𝑓(𝑥, 𝑦) ; 𝑦(𝑥0 ) = 𝑦0 . Here the term 𝑦𝑛 +1 can be taken as a
𝑝
predicted value and rename as 𝑦𝑛+1 .
𝑝
𝑦𝑛+1 = 𝑦𝑛 + ℎ𝑓(𝑥𝑛 , 𝑦𝑛 ) (33.5)

Thus, the Euler formula is the predictor, which has to be corrected by


another method to achieve higher accuracy than progressing with the Euler
method. For this correcting task, we may use a method called Trapezoidal
method which is derived below.

From the fundamental theorem of calculus, we have the following integral


formula.
𝑥
𝑦(𝑥𝑛+1 ) = 𝑦(𝑥𝑛 ) + ∫𝑥 𝑛+1 𝑓(𝑥, 𝑦(𝑥))𝑑𝑥 (33.6)
𝑛

Now, we arrive at the following numerical scheme by applying the


trapezoidal rule to the integral on the right hand side and hence name it as
Trapezoidal method. Recall that 𝑥𝑛+1 = 𝑥𝑛 + ℎ.

𝑦𝑛+1 = 𝑦𝑛 + 2 (𝑓(𝑥𝑛 , 𝑦𝑛 ) + 𝑓(𝑥𝑛+1 , 𝑦𝑛+1 )) ; 𝑛 = 0,1, … (33.7)

This owes a higher accuracy, but it cannot be implemented in explicit way


as 𝑦𝑛 +1 appears on both sides of the formula. Such formulas are called
implicit formulas as well.

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.

If we apply the above method to solve the IVP 𝑦 ′ = 2𝑥 −3 𝑦 ; 𝑦(2) = 1 in


Example 1, the first two steps can be computed as follows. Here, the step-
size is taken as ℎ = 0.2 and we initiate with 𝑥0 = 2, 𝑦0 = 1.

Step 1 (𝑛 = 0)

𝑦1𝑝 = 𝑦0 + ℎ𝑓(𝑥0 , 𝑦0 ) = 1 + 0.2(2 × 2−3 × 1) = 1.05


𝑦1 = 𝑦0 + 2 (𝑓(𝑥0 , 𝑦0 ) + 𝑓(𝑥1 , 𝑦1𝑝 ))

0.2
=1+ [(2 × 2−3 × 1) + (2 × 2.2−3 × 1.05)] = 1.0447 …
2

𝑦1 = 1.0447 … is the numerical solution obtained at 𝑥 = 2.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 …

𝑦2 = 1.0800 … is the numerical solution obtained at 𝑥 = 2.4.

Similarly, you would find 𝑦3 , 𝑦4 , 𝑦5 by repeating the above procedure.

417
Copyright © 2018, The Open University of Sri Lanka (OUSL)
Unit 14: Numerical Methods for solving ODES and PDES

Note that the predictor-corrector method derived above is equivalent the


RK2 method used earlier. Adams-Bashforth method and Milne-Simpson
method are another two predictor-corrector methods frequently discussed.
Predictors and correctors of these methods are based on Lagrange
polynomial approximations.

For various other numerical methods and for theoretical considerations of


solving ODEs, you are referred to a standard text book on Numerical
Analysis.

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

Apply the Predictor-corrector method to solve the differential equation 𝑦 ′ = −𝑥𝑦 2 at


𝑥 = 0.8 , given that 𝑦(0) = 2, 𝑦(0.2) = 1.923, 𝑦(0.4) = 1.724, 𝑦(0.6) = 1.471

Solutions of Activities

Activity 33.1

ℎ = 0.1, 𝑥0 = 𝑦0 , 𝑓(𝑥, 𝑦) = 1 + 2𝑥𝑦

Euler Equation is,

𝑦𝑘+1 = 𝑦𝑘 + ℎ𝑓(𝑥𝑘 , 𝑦𝑘 )

Therefore, 𝑦𝑘+1 = 𝑦𝑘 + 0.1(1 + 2𝑥𝑘 𝑦𝑘 )

Computations can be tabulated as follows.

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

1 0.1 0.1 + 0.1(1 + 2 × 0.1 × 0.1) = 0.202

2 0.2 0.202 + 0.1(1 + 2 × 0.2 × 0.202) = 0.31008

3 0.3 0.31008 + 0.1(1 + 2 × 0.3 × 0.31008) = 0.42868

Table 33.6

Activity 33.2

1 𝑦
𝑦′ = − − 𝑦2
𝑥2 𝑥

𝑦 ′′ = −2𝑥 −3 − (−𝑦𝑥 −2 + 𝑥 −1 𝑦 ′ ) − 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 )𝑒 −𝑥

𝑦 ′′ = −(𝑥 3 + 𝑥𝑦 2 )𝑒 −𝑥 + 𝑒 −𝑥 (3𝑥 2 + 𝑦 2 + 2𝑥𝑦𝑦 ′ )

𝑦 ′′′ = (𝑥 3 + 𝑥𝑦 2 )𝑒 −𝑥 − 2𝑒 −𝑥 (3𝑥 2 + 𝑦 2 + 2𝑥𝑦𝑦 ′ )

−𝑒 −𝑥 (6𝑥 + 2𝑦𝑦 ′ + 2𝑥𝑦𝑦 ′′ + 2𝑥𝑦′2 + 2𝑦𝑦 ′ )

When 𝑥 = 0 𝑦0 = 1

𝑦 ′ =0

𝑦 ′′ =1

𝑦 ′′′ = −2

0.12 𝑦0′′ 0.13 𝑦0′′′


𝑦1 = 𝑦0 + 0.1𝑦0′ + +
2! 3!

𝑦1 = 1.004667

When 𝑥 = 0.1

𝑦1 = 1.004667

𝑦 ′ =0.092235

𝑦 ′′ = 0.864982

𝑦 ′′′ =-0.7851

0.12 𝑦1′′ 0.13 𝑦1′′′


𝑦2 = 𝑦1 + 0.1𝑦1′ + +
2! 3!

𝑦2 = 1.018084

When 𝑥 = 0.2

𝑦2 = 1.018084

𝑦 ′ =0.176272

𝑦 ′′ =0.829358

𝑦 ′′′ = 0.02902

0.12 𝑦2′′ 0.13 𝑦2′′′


𝑦3 = 𝑦2 + 0.1𝑦2′ + +
2! 3!

𝑦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

Second order Runge Kutta Method

𝑘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

Fourth order Runge Kutta method

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

0.2 1.923 𝑦1′ = −0.2 × 1.9232 = −0.7395858

0.4 1.724 𝑦2′ = −0.4 × 1.7242 = −1.188804

0.6 1.471 𝑦3′ = −0.6 × 1.4712 = −1.2983046

Table 33.7

We have the predictor formula,

(𝑝) 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′ = −𝑥4 𝑦42

𝑦4′ = −0.8 × 1.230157232

𝑦4′ = −1.2106294

Now we have the corrector formula,

(𝑐) ℎ
𝑦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′ = −𝑥4 𝑦42

𝑦4′ = −0.8 × 1.217818782

𝑦4′ = −1.1864661

Substituting the value of 𝑦4′ again in corrector formula

(𝑐) 0.2
𝑦4 = 1.724 + [(−1.188804) + 4 × (−1.2983046) + (−1.1864661)]
3

𝑦4 = 1.21942968

𝑦4′ = 𝑥4 𝑦42

𝑦4′ = −0.8 × 1.219429682

𝑦4′ = −1.189607

Again, using corrector formula

(𝑐) 0.2
𝑦4 = 1.724 + [(−1.188804) + 4 × (−1.2983046) + (−1.189607)]
3
(𝑐)
𝑦4 = 1.21922028

𝑦4′ = −𝑥4 𝑦42

𝑦4′ = −0.8 × 1.219220282

𝑦4′ = −1.1891985

Again, using corrector formula

(𝑐) 0.2
𝑦4 = 1.724 + [(−1.188804) + 4 × (−1.2983046) + (−1.1891985)]
3
(𝑐)
𝑦4 = 1.21924752

Therefore, the correct answer of 𝑦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

In this session, we have covered the followings.

Preliminaries of solving an IVP 𝑦 ′ = 𝑓(𝑥, 𝑦) ; 𝑦(𝑥0 ) = 𝑦0 .

Euler method: 𝑦𝑛+1 = 𝑦𝑛 + ℎ𝑓(𝑥𝑛 , 𝑦𝑛 ) ; 𝑛 = 0,1, …

𝑝th order Taylor series method:

ℎ ℎ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

At the successful completion of this session, you will be able to derive

 numerical method of solving ordinary differential equations (ODEs):


Euler method, Taylor series method, Runge-Kutta (RK) methods and
Predictor-corrector methods;
 Apply the above methods to given problems and
 Observe the accuracy of numerical solutions.

425
Copyright © 2018, The Open University of Sri Lanka (OUSL)

You might also like