You are on page 1of 11

Chapter 08.

03
Runge-Kutta 2nd Order Method for
Ordinary Differential Equations

After reading this chapter, you should be able to:


1. understand the Runge-Kutta 2nd order method for ordinary differential equations and
how to use it to solve problems.
What is the Runge-Kutta 2nd order method?
The Runge-Kutta 2nd order method is a numerical technique used to solve an ordinary
differential equation of the form
dy
= f ( x, y ), y (0 ) = y 0
dx
Only first order ordinary differential equations can be solved by using the Runge-Kutta 2nd
order method. In other sections, we will discuss how the 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
Rewrite

in

dy
+ 2 y = 1.3e x , y (0 ) = 5
dx
dy
= f ( x, y ), y (0) = y 0 form.
dx

Solution
dy
+ 2 y = 1.3e x , y (0 ) = 5
dx
dy
= 1.3e x 2 y, y (0 ) = 5
dx
In this case

08.03.1

08.03.2

Chapter 08.03

f ( x, y ) = 1.3e x 2 y
Example 2
Rewrite
ey

in

dy
+ x 2 y 2 = 2 sin(3 x), y (0 ) = 5
dx

dy
= f ( x, y ), y (0) = y 0 form.
dx

Solution
dy
+ x 2 y 2 = 2 sin(3 x), y (0 ) = 5
dx
dy 2 sin(3 x) x 2 y 2
=
, y (0 ) = 5
dx
ey
In this case
2 sin(3 x) x 2 y 2
f ( x, y ) =
ey
ey

Runge-Kutta 2nd order method


Eulers method is given by
(1)
yi +1 = yi + f ( xi , yi )h
where
x0 = 0
y 0 = y ( x0 )
h = xi +1 xi
To understand the Runge-Kutta 2nd order method, we need to derive Eulers method from
the Taylor series.
3
2
dy
(xi +1 xi ) + 1 d 2y (xi +1 xi )2 + 1 d 3y (xi +1 xi )3 + ...
y i +1 = y i +
dx xi , yi
2! dx x , y
3! dx x , y
i

1
1
2
3
f ' ( xi , y i )( xi +1 xi ) + f ' ' ( xi , y i )( xi +1 xi ) + ... (2)
2!
3!
As you can see the first two terms of the Taylor series
yi +1 = yi + f (xi , y i )h
are Eulers method and hence can be considered to be the Runge-Kutta 1st order method.
The true error in the approximation is given by
f ( xi , yi ) 2 f ( xi , yi ) 3
(3)
Et =
h +
h + ...
3!
2!
So what would a 2nd order method formula look like. It would include one more term of the
Taylor series as follows.
= y i + f ( xi , y i )( xi +1 xi ) +

Runge-Kutta 2nd Order Method

08.03.3

1
f (xi , y i )h 2
2!
Let us take a generic example of a first order ordinary differential equation
dy
= e 2 x 3 y, y (0 ) = 5
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 ) =
+
x
y dx
2 x
2 x
=
e 3y +
e 3 y e 2 x 3 y
x
y
2 x
2 x
= 2e + (3) e 3 y
y i +1 = y i + f (xi , y i )h +

)
(

[(

)](

(4)

(5)

2 x

= 5e + 9 y
The 2nd order formula for the above example would be
1
y i +1 = y i + f (xi , y i )h + f (xi , y i )h 2
2!
1
= yi + e 2 xi 3 yi h + 5e 2 xi + 9 yi h 2
2!
However, we already see the difficulty of having to find f ( x, y ) in the above method. What
Runge and Kutta did was write the 2nd order method as
(6)
y i +1 = y i + (a1 k1 + a 2 k 2 )h
where
k1 = f ( x i , y i )
(7)
k 2 = f ( xi + p1 h, y i + q11 k1 h )
This form allows one to take advantage of the 2nd order method without having to
calculate f ( x, y ) .
So how do we find the unknowns a1 , a 2 , p1 and q11 . Without proof (see Appendix
for proof), equating Equation (4) and (6) , gives three equations.
a1 + a 2 = 1
1
a 2 p1 =
2
1
a 2 q11 =
2
Since we have 3 equations and 4 unknowns, we can assume the value of one of the
unknowns. The other three will then be determined from the three equations. Generally the
value of a 2 is chosen to evaluate the other three constants. The three values generally used
1
2
for a 2 are , 1 and , and are known as Heuns Method, the midpoint method and
2
3
Ralstons method, respectively.

08.03.4

Chapter 08.03

Heuns Method
1
Here a 2 = is chosen, giving
2
1
a1 =
2
p1 = 1
q11 = 1
resulting in
1
1
y i +1 = y i + k1 + k 2 h
2
2
where
k1 = f ( x i , y i )
k 2 = f ( xi + h, yi + k1 h )
This method is graphically explained in Figure 1.

(8)
(9a)
(9b)

Slope = f ( xi + h, y i + k1 h )

y i +1 predicted

Slope = f ( xi , yi )

Average Slope =

1
[ f (xi + h, yi + k1h ) + f (xi , yi )]
2

yi

xi

xi+1

Figure 1 Runge-Kutta 2nd order method (Heuns method).


Midpoint Method
Here a 2 = 1 is chosen, giving
a1 = 0
1
p1 =
2
1
q11 =
2
resulting in
yi +1 = yi + k 2 h
where

(10)

Runge-Kutta 2nd Order Method

08.03.5

k1 = f ( x i , y i )

(11a)

1
1

k 2 = f x i + h, y i + k 1 h
2
2

Ralstons Method
2
Here a 2 = is chosen, giving
3
1
a1 =
3
3
p1 =
4
3
q11 =
4
resulting in
2
1
y i +1 = y i + k1 + k 2 h
3
3
where
k1 = f ( x i , y i )

(11b)

(12)
(13a)

3
3

k 2 = f x i + h, y i + k 1 h
4
4

(13b)

Example 3
A ball at 1200 K is allowed to cool down in air at an ambient temperature of 300 K.
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
where is in K and t in seconds. Find the temperature at t = 480 seconds using RungeKutta 2nd order method. Assume a step size of h = 240 seconds.
Solution
d
= 2.2067 10 12 4 81 10 8
dt
f (t , ) = 2.2067 10 12 4 81 10 8
Per Heuns method given by Equations (8) and (9)
1
1
i +1 = i + k1 + k 2 h
2
2
k1 = f (t i , i )

k 2 = f (t i + h, i + k1 h )
i = 0, t 0 = 0, 0 = (0) = 1200
k1 = f (t 0 , o )

08.03.6

Chapter 08.03

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

1
= 1200 + ( 4.5579 ) + (0.017595)240
2

2
= 1200 + ( 2.2702 )240
= 655.16 K
i = 1, t1 = t 0 + h = 0 + 240 = 240, 1 = 655.16K
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

1
= 655.16 + ( 0.38869 ) + ( 0.20206 )240
2

2
= 655.16 + ( 0.29538)240
= 584.27 K
2 = (480) = 584.27 K
The results from Heuns method are compared with exact results in Figure 2.
The exact solution of the ordinary differential equation is given by the solution of a nonlinear 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 s is
(480) = 647.57 K

Runge-Kutta 2nd Order Method

08.03.7

Temperature, (K)

1200

Exact

800

h =120
h =240

400

h =480

0
0

100

200

300

400

500

-400

Time, t(sec)

Figure 2 Heuns method results for different step sizes.


Using a smaller step size would increase the accuracy of the result as given in Table 1 and
Figure 3 below.

Temperature, (480)

Table 1 Effect of step size for Heuns method


t %
Step size, h (480 ) Et
480
-393.87 1041.4
160.82
240
584.27 63.304
9.7756
120
651.35 -3.7762 0.58313
60
649.91 -2.3406 0.36145
30
648.21 -0.63219 0.097625
800
600
400
200
0
-200

100

200

300

400

Step size, h

-400

Figure 3 Effect of step size in Heuns method.

500

08.03.8

Chapter 08.03

In Table 2, Eulers method and the Runge-Kutta 2nd order method results are shown as a
function of step size,
Table 2 Comparison of Euler and the Runge-Kutta methods
( 480)
Step size,
h
Euler
Heun
Midpoint
Ralston
480
-987.84 -393.87 1208.4
449.78
240
110.32
584.27
976.87
690.01
120
546.77
651.35
690.20
667.71
60
614.97
649.91
654.85
652.25
30
632.77
648.21
649.02
648.61
while in Figure 4, the comparison is shown over the range of time.
1200

Temperature, (K)

1100

Midpoint

1000
900

Ralston

800

Heun
Analytical

700
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.
How do these three methods compare with results obtained if we found f ( x, y )
directly?
Of course, we know that since we are including the first three terms in the series, if the
solution is a polynomial of order two or less (that is, quadratic, linear or constant), any of the
three methods are exact. But for any other case the results will be different.
Let us take the example of
dy
= e 2 x 3 y, y (0 ) = 5 .
dx
If we directly find f ( x, y ) , the first three terms of the Taylor series gives

Runge-Kutta 2nd Order Method


y i +1 = y i + f (xi , y i )h +

where

08.03.9

1
f ( xi , y i )h 2
2!

f (x, y ) = e 2 x 3 y
f ( x, y ) = 5e 2 x + 9 y
For a step size of h = 0.2 , using Heuns method, we find
y (0.6 ) = 1.0930
The exact solution
gives

y (x ) = e 2 x + 4e 3 x

y (0.6) = e 2(0.6 ) + 4e 3(0.6 )


= 0.96239
Then the absolute relative true error is
0.96239 1.0930
t =
100
0.96239
= 13.571%
For the same problem, the results from Eulers method and the three Runge-Kutta methods
are given in Table 3.
Table 3 Comparison of Eulers and Runge-Kutta 2nd order methods
y(0.6)
Exact
Euler Direct 2nd Heun Midpoint Ralston
Value 0.96239 0.4955 1.0930
1.1012 1.0974
1.0994
t %
48.514 13.571
14.423 14.029
14.236
Appendix A
How do we get the 2nd order Runge-Kutta method equations?
We wrote the 2nd order Runge-Kutta equations without proof to solve
dy
= f ( x, y ) , y (0) = y 0
dx
as
yi +1 = yi + (a1 k1 + a 2 k 2 )h
where
k1 = f ( x i , y i )

and

k 2 = f ( xi + p1 h, yi + q11 k1 h )

a1 + a 2 = 1
1
a2 p2 =
2

(A.1)
(A.2)
(A.3a)
(A.3b)

08.03.10

Chapter 08.03

1
(A.4)
2
The advantage of using 2nd order Runge-Kutta method equations is based on not having to
find the derivative of f ( x, y ) symbolically in the ordinary differential equation
a 2 q11 =

So how do we get the above three Equations (A.4)? This is the question that is answered in
this Appendix.
Writing out the first three terms of Taylor series are
dy
1 d2y
y i +1 = y i +
h+
h 2 + O (h 3 )
2
dx xi yi
2! dx x y
i i
where
Since

(A.5)

h = xi +1 xi

dy
= f ( x, y )
dx
we can rewrite the Taylor series as
1
y i +1 = y i + f ( xi , y i )h + f ( xi , y i )h 2 + O h 3
2!
Now
f ( x, y ) f ( x, y ) dy
.
f ( x, y ) =
+
x
y dx
Hence
f
1 f
dy

+
yi +1 = yi + f ( xi , yi )h +
2! x xi , yi y xi , yi dx

( )

= y i + f ( xi , y i )h +

1 f
2 x

h2 +
xi , y i

1 f
2 y

(A.6)
(A.7)

2
h + O h 3

xi , yi

( )
( )

f ( xi , y i )h 2 + O h 3

(A.8)

xi , y i

Now the term used in the Runge-Kutta 2nd order method for k 2 can be written as a Taylor
series of two variables with the first three terms as
k 2 = f (xi + p1h, yi + q11k1h )
= f ( xi , y i ) + p1 h

Hence

f
x

xi , y i

+ q11 k1 h

f
y

( )

+ O h2

(A.9)

xi , y i

yi+1 = yi + (a1k1 + a2 k 2 )h

f
f
= y i + a1 f ( xi , y i ) + a 2 f ( xi , y i ) + p1 h
+ q11 k1 h
+ O h 2 h

x xi , yi
y xi , yi

f
f
= y i + (a1 + a 2 )hf ( xi , y i ) + a 2 p1 h 2
+ a 2 q11 f ( xi , y i )h 2
+ O h3
x xi , yi
y xi , yi

( )

( )

Runge-Kutta 2nd Order Method


08.03.11
Equating the terms in Equation (A.8) and Equation (A.10), we get
a1 + a 2 = 1
1
a 2 p1 =
2
1
a 2 q11 =
2
ORDINARY DIFFERENTIAL EQUATIONS
Topic
Runge 2nd Order Method for Ordinary Differential Equations
Summary
Textbook notes on Runge 2nd order method for ODE
Major
General Engineering
Authors
Autar Kaw
Last Revised October 13, 2010
Web Site
http://numericalmethods.eng.usf.edu

(A.10)

You might also like