Professional Documents
Culture Documents
Autar Kaw
1. understand the Runge-Kutta 2nd order method for ordinary differential equations
and how to use it to solve problems.
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
dy
+ 2 y = 1.3e − x , y (0 ) = 5
dx
in
dy
= f ( x, y ), y (0) = y 0 form.
dx
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 1 of 17
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
Example 2
Rewrite
dy
ey + x 2 y 2 = 2 sin(3 x), y (0 ) = 5
dx
in
dy
= f ( x, y ), y (0) = y 0 form.
dx
Solution
dy
ey + 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
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 2 of 17
yi +1 = yi + f ( xi , yi )h (1)
where
x0 = 0
y 0 = y ( x0 )
h = xi +1 − xi
To understand the Runge-Kutta 2nd order method, we need to derive Euler’s method
from the Taylor series.
2 3
dy
yi +1 = y i + (xi +1 − xi ) + 1 d y
2
(xi +1 − xi )2 + 1 d y
3
(xi +1 − xi )3 + ...
dx xi , y i 2! dx xi , y i
3! dx xi , y i
1 1
= y i + f ( xi , y i )( xi +1 − xi ) + f ' ( xi , y i )( xi +1 − xi ) + f ' ' ( xi , y i )( xi +1 − xi ) + ... (2)
2 3
2! 3!
As you can see the first two terms of the Taylor series
yi +1 = y i + f ( xi , yi )h
are Euler’s method and hence can be considered to be the Runge-Kutta 1st order
method.
f ′( xi , yi ) 2 f ′′( xi , yi ) 3
Et = h + h + ...
2! 3!
(3)
So what would a 2nd order method formula look like. It would include one more term of
the Taylor series as follows.
1
yi +1 = y i + f ( xi , y i )h + f ′( xi , y i )h 2 (4)
2!
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 3 of 17
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
f ′( x, y ) = +
∂x ∂y dx
(5)
=
∂ −2 x
∂x
(
e − 3y +
∂ −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
1
yi +1 = y i + f ( xi , y i )h + f ′( xi , y i )h 2
2!
( )
= yi + e − 2 xi − 3 yi h +
1
2!
(
− 5e −2 xi + 9 yi h 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
yi +1 = y i + (a1 k1 + a 2 k 2 )h (6)
where
k1 = f ( x i , y i )
This form allows one to take advantage of the 2nd order method without having to
calculate f ′( x, y ) .
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 4 of 17
So how do we find the unknowns a1 , a2 , 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 a2 is chosen to evaluate the other three constants. The three values
1 2
generally used for a2 are , 1 and , and are known as Heun’s Method, the midpoint
2 3
method and Ralston’s method, respectively.
Heun’s Method
1
Here a 2 = is chosen, giving
2
1
a1 =
2
p1 = 1
q11 = 1
resulting in
1 1
yi +1 = y i + k1 + k 2 h
2 2
(8)
where
k1 = f ( x i , y i )
(9a)
Source URL: http://numericalmethods.eng.usf.edu/
Saylor URL: http://www.saylor.org/courses/me205/
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 5 of 17
k 2 = f ( xi + h, yi + k1h )
(9b)
y Slope = f ( xi + h, y i + k1h )
1
Average Slope = [ f (xi + h, yi + k1h ) + f (xi , yi )]
2
yi
xi xi+1
x
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
(10)
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 6 of 17
where
k1 = f ( x i , y i )
(11a)
1 1
k 2 = f x i + h, y i + k 1 h
2 2
(11b)
Ralston’s Method
2
Here a 2 = is chosen, giving
3
1
a1 =
3
3
p1 =
4
3
q11 =
4
resulting in
1 2
yi +1 = y i + k1 + k 2 h
3 3
(12)
where
k1 = f ( x i , y i )
(13a)
3 3
k 2 = f x i + h, y i + k 1 h
4 4
(13b)
Example 3
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 7 of 17
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
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 (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 )
= f (0,1200)
(
= −2.2067 × 10 −12 1200 4 − 81 × 10 8 )
= −4.5579
k 2 = f (t 0 + h, θ 0 + k1h )
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 8 of 17
= 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
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 (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
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 9 of 17
= 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
θ (480) = 647.57 K
1200
Temperature, θ (K)
Exact h =120
800
h =240
400
h =480
0
0 100 200 300 400 500
-400
Time, t(sec)
Using a smaller step size would increase the accuracy of the result as given in Table 1
and Figure 3 below.
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 10 of 17
Table 1 Effect of step size for Heun’s method
Step size,
θ (480) Et ∈t %
h
800
Temperature, θ (480)
600
400
200
0
0 100 200 300 400 500
-200
Step size, h
-400
In Table 2, Euler’s method and the Runge-Kutta 2nd order method results are shown as
a function of step size,
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 11 of 17
Step size, θ ( 480)
1200
1100
1000 Midpoint
Temperature, θ(K)
900 Ralston
800 Heun
700 Analytical
600
Euler
500
0 100 200 300 400 500 600
Time, t (sec)
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 12 of 17
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.
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
1
yi +1 = yi + f ( xi , y i )h + f ′( xi , y i )h 2
2!
where
f ( x , y ) = e −2 x − 3 y
f ′( x, y ) = −5e −2 x + 9 y
y (0.6 ) = 1.0930
y ( x ) = e −2 x + 4e −3 x
gives
= 0.96239
0.96239 − 1.0930
∈t = × 100
0.96239
= 13.571%
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 13 of 17
For the same problem, the results from Euler’s method and the three Runge-Kutta
methods are given in Table 3.
y(0.6)
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
(A.1)
as
yi +1 = yi + (a1k1 + a 2 k 2 )h
(A.2)
where
k1 = f ( x i , y i )
(A.3a)
and
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 14 of 17
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.
1 d2y
yi +1 = y i +
dy
dx
h+
2! dx 2
( )
h 2 + O h3
xi yi xi y i
(A.5)
where
h = xi +1 − xi
Since
dy
= f ( x, y )
dx
f ′( xi , y i )h 2 + O(h 3 )
1
yi +1 = y i + f ( xi , y i )h + (A.6)
2!
Now
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 15 of 17
∂f ( x, y ) ∂f ( x, y ) dy
f ′( x, y ) = + .
∂x ∂y dx
(A.7)
Hence
1 ∂f ∂f 2
yi +1 = yi + f ( xi , yi )h +
2! ∂x
+
∂y
×
dy
dx
h + O h 3
( )
xi , yi xi , yi xi , yi
1 ∂f 1 ∂f
= y i + f ( xi , y i )h +
2 ∂x
h2 +
2 ∂y
f ( xi , y i )h 2 + O h 3( ) (A.8)
xi , y i 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
∂f ∂f
= f ( xi , y i ) + p1 h
∂x
+ q11 k1 h
∂y
+ O h2 ( ) (A.9)
xi , y i xi , y i
Hence
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 ) h2
∂x ∂y
xi , y i xi , y i
∂f ∂f
= y i + (a1 + a 2 )hf (xi , y i ) + a 2 p1 h 2
∂x
+ a 2 q11 f (xi , y i )h 2
∂y
+ O h3( )
xi , y i xi , y i
(A.10)
a1 + a 2 = 1
1
a 2 p1 =
2
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 16 of 17
1
a 2 q11 =
2
Attributed to: University of South Florida: Holistic Numerical Methods Institute Saylor.org
Page 17 of 17