Professional Documents
Culture Documents
Chapter 08.04
Runge-Kutta 2nd Order Method
for
Ordinary Differential Equations
Runge-Kutta 2nd order method is a numerical technique to solve ordinary differential
equation of the form
= f ( x, y ), y (0 ) = y 0
dy
dx
Only first order ordinary differential equations can be solved by using Runge-Kutta
2nd order method. In other sections, we have discussed how Euler and Runge-Kutta
methods are used to solve higher order ordinary differential equations or coupled
(simultaneous) differential equations.
How does one write a first order differential equation in the above form?
Example 1
+ 2 y = 1.3e − x , y (0 ) = 5
dy
dx
is rewritten as
= 1.3e − x − 2 y, y (0 ) = 5
dy
dx
In this case
f ( x, y ) = 1.3e − x − 2 y
Another example is given as follows
Example 2
+ x 2 y 2 = 2 Sin (3x ), y (0) = 5
dy
ey
dx
is rewritten as
dy 2 Sin(3 x) − x 2 y 2
= , y (0) = 5
dx ey
In this case
2Sin(3x) − x 2 y 2
f ( x, y ) =
ey
Euler’s method is given by
y i +1 = y i + f ( xi , y i )h (1)
where
W:\mws\gen\08ode\mws_gen_ode_txt_runge2nd.doc
RUNGE-KUTTA 2ND ORDER METHOD 08.04.2
x0 = 0
y0 = y0
h = xi +1 − xi
To understand Runge-Kutta 2nd order method, we need to derive Euler’s method from
Taylor series.
2 3
y i +1 = y i +
dy
(xi +1 − xi ) + 1 d 2y (xi +1 − xi )2 + 1 d y3 (xi +1 − xi )3 + ...
dx xi , yi 2! dx x , y 3! dx x , y
i i i i
2! 3!
As you can see the first two terms of the Taylor series
y i +1 = yi + f ( xi , y i )h
are the Euler’s method and hence can be considered to be Runge-Kutta 1st order
method.
The true error in the approximation is given by
f ′( xi , yi ) 2 f ′′( xi , yi ) 3
Et = h + h + ... (3)
2! 3!
So how would a 2nd order method formula look like. It would include one more term
of the Taylor series as follows.
yi +1 = yi + f ( xi , yi )h + f ′( xi , yi )h 2
1
(4)
2!
Let us take a generic example of a first ordinary differential equation
= e − 2 x − 3 y , y (0) = 5
dy
dx
f (x, y ) = e−2 x − 3 y
Now since y is a function of x,
∂f ( x, y ) ∂f ( x, y ) dy
f ′( x, y ) = + (5)
∂x ∂y dx
∂
= (e − 2 x − 3 y ) +
∂x
[
∂ −2 x
∂y
]
(e − 3 y ) (e −2 x − 3 y )
= −2e −2 x + ( −3)(e −2 x − 3 y )
= −5e −2 x + 9 y
The 2nd order formula for the above example would be
y i +1 = y i + f (xi , y i )h + f ′(xi , y i )h 2
1
2!
yi +1 = yi + (e − 2 xi − 3 yi )h + (− 5e − 2 xi + 9 yi )h 2
1
2!
W:\mws\gen\08ode\mws_gen_ode_txt_runge2nd.doc
RUNGE-KUTTA 2ND ORDER METHOD 08.04.3
Heun’s method
1
Here a2 = is chosen, giving
2
1
a1 =
2
p1 = 1
q11 = 1
resulting in
⎛1 1 ⎞
yi +1 = yi + ⎜ k1 + k2 ⎟h (8)
⎝2 2 ⎠
where
k1 = f ( x i , y i ) (9a)
k 2 = f ( xi + h, y i + k1 h ) (9b)
This method is graphically explained in Figure 1.
W:\mws\gen\08ode\mws_gen_ode_txt_runge2nd.doc
RUNGE-KUTTA 2ND ORDER METHOD 08.04.4
y Slope = f ( xi + h, yi + k1h )
yi+1, predicted
Slope = f ( xi , yi )
Average Slope =
1
[ f (xi + h, yi + k1h ) + f (xi , yi )]
2
yi
x
xi xi+1
Midpoint method
Here a2 = 1 is chosen, giving
a1 = 0
1
p1 =
2
1
q11 =
2
resulting in
yi +1 = yi + k2h (10)
where
k1 = f ( xi , yi ) (11a)
⎛ 1 1 ⎞
k 2 = f ⎜ xi + h, yi + k1h ⎟ (11b)
⎝ 2 2 ⎠
Ralston’s method
2
Here a 2 = is chosen, giving
3
1
a1 =
3
3
p1 =
4
3
q11 =
4
resulting in
W:\mws\gen\08ode\mws_gen_ode_txt_runge2nd.doc
RUNGE-KUTTA 2ND ORDER METHOD 08.04.5
1 2
yi +1 = yi + ( k1 + k 2 )h (12)
3 3
where
k1 = f ( xi , yi ) (13a)
⎛ 3 3 ⎞
k 2 = f ⎜ x i + h , y i + k1 h ⎟ (13b)
⎝ 4 4 ⎠
Example 3
A ball at 1200K is allowed to cool down in air at an ambient temperature of 300K.
Assuming heat is lost only due to radiation, the differential equation for the
temperature of the ball is given by
dθ
= −2.2067 × 10 -12 (θ 4 − 81 × 108 )
dt
Find the temperature at t = 8 minutes using Runge-Kutta 2nd order method. Assume
a step size of h = 4 minutes.
Solution
dθ
dt
(
= −2.2067 × 10 −12 θ 4 − 81 × 10 8)
( )
f (t , θ ) = −2.2067 × 10 −12 θ 4 − 81 × 10 8
Per Heun’s method given by Equations (8) and (9)
⎛1 1 ⎞
θ i +1 = θ i + ⎜ k1 + k 2 ⎟h
⎝2 2 ⎠
k1 = f (ti , θ i )
k 2 = f (ti + h, θ i + k1h )
i = 0, t0 = 0, θ 0 = θ (0) = 1200
k1 = f (t 0 ,θ o )
= f (0,1200)
= −2.2067 × 10 −12 (1200 4 − 81 × 10 8 )
= −4.5579
k 2 = f (t 0 + h,θ 0 + k1 h )
= f (0 + 240,1200 + (− 4.5579)240)
= f (240,106.09)
= −2.2067 × 10 −12 (106.09 4 − 81 × 10 8 )
= 0.017595
⎛1 1 ⎞
θ 1 = θ 0 + ⎜ k1 + k 2 ⎟ h
⎝2 2 ⎠
W:\mws\gen\08ode\mws_gen_ode_txt_runge2nd.doc
RUNGE-KUTTA 2ND ORDER METHOD 08.04.6
⎛1 ⎞
= 1200 + ⎜ (− 4.5579 ) + (0.017595 )⎟240
1
⎝2 2 ⎠
= 1200 + (− 2.2702)240
= 655.16K
i = 1, t1 = t 0 + h = 0 + 240 = 240, θ1 = 655.16 K
k1 = f (t1 ,θ1 )
= f (240,655.16)
= −2.2067 × 10 −12 (655.16 4 − 81 × 10 8 )
= −0.38869
k 2 = f (t1 + h, θ1 + k1 h )
= f (240 + 240,655.16 + (− 0.38869)240)
= f (480,561.87 )
= −2.2067 × 10 −12 (561.87 4 − 81 × 10 8 )
= −0.20206
⎛1 1 ⎞
θ 2 = θ 1 + ⎜ k1 + k 2 ⎟ h
⎝2 2 ⎠
⎛1 ⎞
= 655.16 + ⎜ (− 0.38869 ) + (− 0.20206 )⎟ 240
1
⎝2 2 ⎠
= 655.16 + (− 0.29538)240
= 584.27 K
θ 2 = θ (480) = 584.27 K
The results from Heun’s method are compared with exact results in Figure 2.
The exact solution of the ordinary differential equation is given by the solution of a
non-linear equation as
θ − 300
0.92593 ln − 1.8519 tan −1 (0.0033333θ ) = −0.22067 × 10 −3 t − 2.9282
θ + 300
The solution to this nonlinear equation at t=480 is
θ ( 480) = 647.57 K
W:\mws\gen\08ode\mws_gen_ode_txt_runge2nd.doc
RUNGE-KUTTA 2ND ORDER METHOD 08.04.7
1200
h=120
Temperature, θ(K)
Exact
800
h=240
400
h=480
0
0 100 200 300 400 500
-400
Time, t(sec)
Using smaller step size would increases the accuracy of the result as given in Table 1
and Figure 3 below.
W:\mws\gen\08ode\mws_gen_ode_txt_runge2nd.doc
RUNGE-KUTTA 2ND ORDER METHOD 08.04.8
800
Temperature, θ(480)
600
400
200
0
0 100 200 300 400 500
-200 Step size, h
-400
In Table 2, the Euler’s method and Runge-Kutta 2nd order method results are shown
as a function of step size,
W:\mws\gen\08ode\mws_gen_ode_txt_runge2nd.doc
RUNGE-KUTTA 2ND ORDER METHOD 08.04.9
1200
1100
θ(K)
1000 Midpoint
Temperature,
Ralston
900
Heun
800
700 Analytical
600
Euler
500
0 100 200 300 400 500 600
Time, t (sec)
Figure 4. Comparison of Euler and Runge Kutta methods with exact results
over time.
W:\mws\gen\08ode\mws_gen_ode_txt_runge2nd.doc
RUNGE-KUTTA 2ND ORDER METHOD 08.04.10
W:\mws\gen\08ode\mws_gen_ode_txt_runge2nd.doc