Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Computational Physics: Lecture – II

In the previous lecture we discussed the Euler’s method to solve the simple first order
differential equations. Now, let us discuss how we can improve the Euler’s method.

Consider a simple first order ordinary differential equation

𝑑 𝑓(𝑥)
= 𝐺(𝑥)
𝑑𝑥

With the initial condition 𝑓(𝑥 = 𝑎) = 𝑓! . Here 𝑥 is a generic independent variable such as
position, time, etc. We assume that 𝑓(𝑥) is a well-behaved function so that it can be expanded
around the known point 𝑥.

" (𝑥)
ℎ# ""
𝑓(𝑥 + ℎ) = 𝑓(𝑥) + ℎ 𝑓 + 𝑓 (𝑥) + 𝑜(ℎ$ )
2!

The general expression is


ℎ# "
𝑓%&' = 𝑓% + ℎ 𝐺(𝑥% ) + 𝐺 (𝑥% ) + 𝑜(ℎ$ )
2!
ℎ "
𝑓%&' = 𝑓% + ℎ /𝐺(𝑥% ) + 𝐺 (𝑥% )0 + 𝑜(ℎ$ )
2

Where 𝐺 " (𝑥% ) = 𝑓 "" (𝑥% )

Consider the Taylor’s series of the function at 𝑥%

ℎ ℎ " ℎ #
𝐺 1𝑥% , 3 = 𝐺(𝑥% ) + 𝐺 (𝑥% ) + 𝑜 41 3 5
2 2 2

It implies,


𝑓%&' = 𝑓% + ℎ 𝐺 1𝑥% , 3 + 𝑜(ℎ$ )
2

It is the improved version of Euler’s method. Note that the main difference between Euler’s
and improved Euler’s method is where the function 𝐺(𝑥) is evaluated. For example, in the
Euler’s method it is evaluated at a given value of 𝑥, i.e., 𝐺(𝑥) however, in the improved version
(
it is evaluated as 𝐺 6𝑥 , # 7
Runge-Kutta Method

Consider a general first order ordinary differential equation

𝑑 𝑓(𝑥)
= 𝐺8𝑥, 𝑓(𝑥)9
𝑑𝑥

With the initial condition 𝑓(𝑥 = 𝑎) = 𝑓) . Compare to ODE that we considered earlier, in this
general ODE 𝑓(𝑥) depends on both 𝑥 and 𝑓(𝑥).

Start with the Euler’s equation with the second order term

(!
𝑓%&' = 𝑓% + ℎ 𝐺8𝑥% , 𝑓(𝑥% )9 + #!
𝐺 " 8𝑥% , 𝑓(𝑥% )9 + 𝑜(ℎ$ )

with the initial condition 𝑓! = 𝑓(𝑥 = 0)

Since the function 𝐺8𝑥, 𝑓(𝑥)9 has two variables, 𝑥 and 𝑓(𝑥), the derivative of 𝐺8𝑥, 𝑓(𝑥)9 is

𝑑 𝐺8𝑥, 𝑓 (𝑥)9 𝜕𝐺 𝑑𝑥 𝜕𝐺 𝑑𝑓 𝜕𝐺 𝜕𝐺
𝐺 " 8𝑥, 𝑓(𝑥)9 = = + = + 𝐺
𝑑𝑥 𝜕𝑥 𝑑𝑥 𝜕𝑓 𝑑𝑥 𝜕𝑥 𝜕𝑓

The above Euler’s equation modifies into

𝑓%&' = 𝑓% + ℎ 𝐺8𝑥% , 𝑓(𝑥% )9


ℎ# 𝜕𝐺8𝑥% , 𝑓(𝑥% )9 𝜕𝐺8𝑥% , 𝑓(𝑥% )9
+ < + 𝐺8𝑥% , 𝑓(𝑥% )9=
2 𝜕𝑥% 𝜕𝑓
+ 𝑜(ℎ$ )

Consider the Taylor expansion of the function of

ℎ ℎ
𝐺 >𝑥% + , 𝑓(𝑥% ) + 𝐺8𝑥% , 𝑓(𝑥% )9?
2 2
ℎ 𝜕𝐺8𝑥% , 𝑓(𝑥% )9 𝜕𝐺8𝑥% , 𝑓(𝑥% )9
= 𝐺8𝑥% , 𝑓(𝑥% )9 + < + 𝐺8𝑥% , 𝑓(𝑥% )9=
2 𝜕𝑥% 𝜕𝑓
+ 𝑜(ℎ# )

With this, the above Euler’s equation modifies as

ℎ ℎ
𝑓%&' = 𝑓% + ℎ 𝐺 >𝑥% + , 𝑓(𝑥% ) + 𝐺8𝑥% , 𝑓(𝑥% )9? + 𝑜(ℎ$ )
2 2
It is called the “Mid-point Runge-Kutta” method or simply, RK-2 method

We can define the RK-2 coefficients 𝑘' and 𝑘# as

𝑘' = ℎ 𝐺8𝑥% , 𝑓(𝑥% )9


ℎ 𝑘'
𝑘# = ℎ 𝐺 1𝑥% + , 𝑓(𝑥% ) + 3
2 2

Thus,
𝑓%&' = 𝑓% + 𝑘#

Note that while solving RK-2 method we have essentially used Taylor’s series up to the order
𝑜(ℎ# ) but without explicitly calculating the second derivative of the function 𝑓(𝑥).

The local error in RK-2 method is ~ℎ$ and the global error is ~ℎ#

One can follow the same procedure and further extend the accuracy of the method. While
doing so one can get the improved RK-2 method. It is called RK-4 method, where the 𝑓%&'
term reads,

1 𝑘' 𝑘+
𝑓%&' = 𝑓% + / + 𝑘# + 𝑘$ + 0
3 2 2
where

𝑘' = ℎ 𝐺8𝑥% , 𝑓(𝑥% )9

ℎ 𝑘'
𝑘# = ℎ 𝐺 1𝑥% + , 𝑓(𝑥% ) + 3
2 2

ℎ 𝑘#
𝑘$ = ℎ 𝐺 1𝑥% + , 𝑓(𝑥% ) + 3
2 2

𝑘+ = ℎ 𝐺(𝑥% + ℎ, 𝑓(𝑥% ) + 𝑘$ )

The local error in RK-4 method is ~ℎ, and the global error is ~ℎ+
Example

Consider the following differential equation,

-.
-/
=𝑥𝑦 with the initial condition 𝑦 (𝑥 = 1) = 5

By taking the step size ℎ = 0.1, find the numerical solution in the interval [1, 5] using both
the RK-2 and RK-4 method. Compare the results by plotting the solutions together with the
analytical solution.

Adaptive step size

So far, we have fixed the step size ℎ while updating the function (𝑓%&' ). In this case, the error
depends on the step size. Imagine, in a given problem, we want to fix the accuracy or tolerance
(error) of the results. Thus, we can choose the step size with respect to the error. It is called the
dynamical or adaptive step size.

For example, in Euler’s method we use the following

0
𝑓%&' = 𝑓%0 + ℎ 𝐺8𝑥% , 𝑓%0 (𝑥% )9 + 𝑜(ℎ# )

and in RK-2 method we use the following

1
ℎ ℎ
𝑓%&' = 𝑓%1 + ℎ 𝐺 >𝑥% + , 𝑓%1 (𝑥% ) + 𝐺8𝑥% , 𝑓%1 (𝑥% )9? + 𝑜(ℎ$ )
2 2

The difference in these two schemes is


0 1
𝑟%&' = 𝑓%&' − 𝑓%&' = 𝑜(ℎ# ) = 𝛼 ℎ#

for some constant 𝛼 and ℎ. Now, assume that ℎ is not known (keep it as a variable) but fix the
tolerance 𝜀. Such that
#
ℎ%&' 𝜀
#
=
ℎ% |𝑟%&' |

The step size at 𝑛 + 1 can be chosen as


𝜀
ℎ%&' = ℎ% O
|𝑟%&' |
with a condition that 𝜀 < |𝑟%&' |.

Multi step size

The RK method is an example of a “one-step” method. It takes the information from the current
step, and uses it to generate the value of the function in the next step.
Consider a general first order ordinary differential equation

𝑑 𝑓(𝑥)
= 𝐺8𝑥, 𝑓(𝑥)9
𝑑𝑥

With the initial condition 𝑓(𝑥 = 𝑎) = 𝑓) . Imagine a method, in which while updating the
function all the information from the previous steps was used. Like the following
%

𝑓%&' = 𝑓) + Q 𝛼2 𝐺8𝑥2 , 𝑓(𝑥2 )9


23'

Here the coefficients {𝛼2 } can be chosen to maintain the accuracy and stability of the numerical
method.

Higher order differential equations

Higher order differential equations may be expressed as a system of first order differential
equations.

𝑑# 𝑦 𝑑𝑦
= 𝑓 1𝑥, 𝑦, 3
𝑑𝑥 # 𝑑𝑥
-.
With the initial conditions 𝑦(𝑥 = 0) = 𝑦! and | = 𝑦!"
-/ /3!

Let
𝑑𝑦
=𝑧
𝑑𝑥

In terms of 𝑧 the differential equation modifies as

𝑑𝑧 𝑑# 𝑦
= = 𝑓(𝑥, 𝑦, 𝑧)
𝑑𝑥 𝑑𝑥 #

The corresponding initial condition reads as 𝑧(𝑥 = 0) = 𝑦!" = 𝑧!


-4
Using the Euler’s method, we can solve the above differential equations, -/
= 𝑓(𝑥, 𝑦, 𝑧) and
-.
-/
= 𝑧 as

𝑧%&' = 𝑧% + ℎ 𝑓(𝑥% , 𝑦% , 𝑧% )

𝑦%&' = 𝑦% + ℎ 𝑧%

Here, the second order differential equation is expressed as two first order differential equations
using the Euler’s method. However, if the considered differential equation is complex then
instead of Euler’s method one can choose RK methods to get better accuracy in the numerical
solutions. We will practice some problems on these methods in the lab !

You might also like