Professional Documents
Culture Documents
Mws Gen Ode TXT Runge2nd
Mws Gen Ode TXT Runge2nd
03
Runge-Kutta 2nd Order Method for
Ordinary Differential Equations
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
dy
2 y 1.3e x , y 0 5
dx
in
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
f x, y 1.3e x 2 y
08.03.1
08.03.2
Chapter 08.03
Example 2
Rewrite
ey
dy
x 2 y 2 2 sin(3 x), y 0 5
dx
in
dy
f ( x, y ), y (0) y 0 form.
dx
Solution
ey
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
(1)
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.
y i 1 y i
dy
dx
xi 1 xi
xi , y i
y i f ( xi , y i ) xi 1
1 d2y
2! dx 2
xi 1 xi 2
xi , yi
1 d3y
3! dx 3
xi 1 xi 3 ...
xi , y i
1
1
2
3
xi 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
Et
h
h ...
(3)
2!
3!
So what would a 2nd order method formula look like. It would include one more term of the
Taylor series as follows.
y i 1 y i f xi , y i h
1
f xi , y i h 2
2!
(4)
08.03.3
dy
e 2 x 3 y, y 0 5
dx
f x, y e 2 x 3 y
f x, y f x, y dy
x
y
dx
f x, y
(5)
e 2 x 3 y
x
y
2 x
3y
2 x
3y
2e 2 x ( 3) e 2 x 3 y
5e 2 x 9 y
y i 1 y i f xi , y i h
yi e 2 xi
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
y i 1 y i a1 k1 a 2 k 2 h
(6)
where
k1 f x i , y i
k 2 f xi p1 h, yi q11 k1 h
(7)
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
for a 2 are
1
2
, 1 and
, and are known as Heuns Method, the midpoint method and
2
3
Here a 2
08.03.4
Chapter 08.03
q11 1
resulting in
1
1
k1 k 2 h
2
2
y i 1 y i
(8)
where
k1 f x i , y i
k 2 f xi h, y i k1 h
This method is graphically explained in Figure 1.
(9a)
(9b)
Slope f xi h, y i k1 h
Slope f xi , yi
predicted
Average Slope
1
f x i h , y i k1 h f x i , y i
2
yi
xi
xi+1
resulting in
y i 1 y i k 2 h
where
(10)
k1 f x i , y i
(11a)
1
1
k 2 f x i h, y i k 1 h
2
2
(11b)
Ralstons Method
2
is chosen, giving
3
1
a1
3
Here a 2
08.03.5
3
4
3
p1
q11
resulting in
2
1
k1 k 2 h
3
3
y i 1 y i
where
(12)
k1 f x i , y i
(13a)
3
3
k 2 f xi h, y i k1 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
i 1 i
k1 f t i , i
k 2 f t i h, i k 1 h
i 0, t 0 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 k1h
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
08.03.6
Chapter 08.03
1
4.5579 1 0.017595 240
2
2
1200
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
2.2067 10 12 561.87 4 81 10 8
0.20206
1
1
2 1 k1 k 2 h
2
2
1
1
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
08.03.7
08.03.8
Chapter 08.03
where
f x, y e 2 x 3 y
f x, y 5e 2 x 9 y
4e 3 x
08.03.9
0.96239 1.0930
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
as
where
dy
f x, y , y 0 y 0
dx
(A.1)
y i 1 yi a1 k1 a 2 k 2 h
(A.2)
k1 f x i , y i
(A.3a)
(A.3b)
and
a1 a 2 1
1
a2 p2
2
1
a 2 q11
2
(A.4)
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
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
y i 1 y i
where
Since
dy
dx
h xi 1 xi
h
xi y i
1 d2y
2! dx 2
h 2 O h 3
xi y i
(A.5)
08.03.10
Chapter 08.03
dy
f x, y
dx
Now
f x, y
1
f xi , y i h 2 O h 3
2!
(A.6)
f x, y f x, y dy
.
x
y
dx
(A.7)
Hence
2
1 f
f
dy
h O h3
2! x xi , yi
y xi , yi
dx xi , yi
1 f
1 f
2
y i f xi , y i h
h
f xi , y i h 2 O h 3
2 x xi , yi
2 y xi , yi
y i 1 yi f xi , y i h
(A.8)
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 x i , y i p1 h
(A.9)
Hence
f
x
q11 k1 h
xi , y i
f
y
O h2
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 h2 h
x xi , yi
y xi , yi
f
f
y i a1 a 2 hf x i , y i a 2 p1 h 2
a 2 q11 f x i , y i h 2
O h 3
x x , y
y x , y
(A.10)
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