MTH2212 – Computational Methods and
Statistics
Numerical Solutions of Ordinary
Differential Equations
Lecture 12:
Initial Value Problem: Runge-Kutta Methods
Objectives
Introduction
Runge-Kutta Methods
Second-order Runge-Kutta
Heun Method
Midpoint Method
Ralston’s Method
Fourth-order Runge-Kutta
System of ODE
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 2
Introduction
A fundamental source of error
in Euler’s method is that the
derivative at the beginning of
the interval is assumed to
apply across the entire
interval.
Improving the estimate of the
slope will improve the
solution.
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 3
Runge-Kutta Methods
Runge-Kutta (RK) methods achieve the accuracy of a Taylor
series without requiring the calculation of higher
derivatives.
For a differential equation of the form:
dy
f ( x, y )
dx
The estimate of the solution is given by
yi 1 yi ( xi , yi , h) h
where ( xi , yi , h) is the increment function which is the
slope over the interval
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 4
Runge-Kutta Methods
The increment function can be written in general form as
a1k1 a2 k 2 a3 k 3 ... an k n
where a1, a2, a3,…, an are constants and k1, k2, k3,…, kn are
k1 f ( xi , yi )
k 2 f ( xi p1h, yi q11 k1 h)
k 3 f ( xi p 2 h, yi q21k1 h q22 k 2 h)
.
.
.
k n f ( xi pn 1h, yi q n1,1k1 h qn1, 2 k 2 h ... qn 1,n 1k n1h)
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 5
Runge-Kutta Methods
Where p1, p2,…,pn-1 and q1, q2,…,qn-1 are all constants
n is order number of the RK method
k’s are recurrence functions. Because each k is a functional
evaluation, this recurrence makes RK methods efficient for
computer calculations.
Various types of RK methods can be devised by employing
different number of terms in the increment function as
specified by n.
Once n is chosen, values of a’s, p’s, and q’s are evaluated by
setting general equation equal to terms in a Taylor series
expansion.
First-order RK method (n = 1) is Euler’s method.
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 6
Second-order Runge-Kutta
The second-order version of Runge-Kutta methods is
yi 1 yi (a1k1 a 2 k 2 ) h (i)
where
k1 f ( xi , yi )
k 2 f ( xi p1 h, yi q11 k1h)
Evaluate values for a1, a2, p1 and q11 by setting equation (i) equal
to a Taylor series expansion to the second-order term (see Box
25.1 p.703 for details).
Three equations are derived to solve four unknown constants
a1 a2 1
a2 p1 1 / 2
a2 q11 1 / 2
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 7
Second-order Runge-Kutta
Since there are three equations with four unknowns, we
must assume a value of one of the unknowns to determine
the other three.
Suppose that we specify a value for a2, then
a1 1 a2
1
p1 q11
2a 2
There are an infinite number of second-order RK methods
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 8
Second-order Runge-Kutta
Because we can choose an infinite number of values for a2,
there are an infinite number of second-order RK methods.
Every version would yield exactly the same results if the
solution to ODE were quadratic, linear, or a constant.
However, they yield different results if the solution is more
complicated (typically the case).
Three of the most commonly used RK second-order
methods are:
Heun’s Method with a Single Corrector (a2=1/2)
The Midpoint Method (a2=1)
Raltson’s Method (a =2/3)
2
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 9
Heun Method with a Single Corrector
a2 is set to a2=1/2
a1 1 a2 1 / 2
p1 q11 1 / 2a 2
Substituting into equation (i)
1 1
yi 1 yi ( k1 k 2 ) h
Where 2 2
k1 f ( xi , yi )
k 2 f ( xi h, yi k1h)
k1 is the slope at the beginning of the interval and k2 is the
slope at the end of the interval.
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 10
The Midpoint Method
a2 is set to a2=1
a1 0
1
p1 q11
2
Substituting into equation (i)
yi 1 yi k 2 h
Where
k1 f ( xi , yi )
1 1
k 2 f ( xi h, yi k1h)
2 2
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 11
Ralston’s Method
a2 is set to a2=2/3
a1 1 / 3
p1 q11 3 / 4
Substituting into equation (i)
1 2
yi 1 yi ( k1 k 2 ) h
3 3
Where
k1 f ( xi , yi )
3 3
k 2 f ( xi h, yi k1 h)
4 4
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 12
Second-order Runge-Kutta
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 13
Fourth-order Runge-Kutta
This is the most popular of the RK methods
1
yi 1 yi (k1 2k 2 2k 3 k 4 ) h
6
Where
k1 f ( xi , yi )
1 1
k 2 f ( xi h, yi k1 h)
2 2
1 1
k 3 f ( xi h, yi k 2 h)
2 2
k 4 f ( xi h, yi k 3 h)
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 14
Example 1
Integrate using fourth-order Runge-Kutta method:
dy
4e0.8 x 0.5 y
dx
Using h = 0.5 with y(0) = 2 from x = 0 to 0.5.
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 15
Example 1 - Solution
Compute the slope at the beginning of the interval:
k1 f ( x i , yi ) f (0,2) 4e 0.8( 0 ) 0.5(2) 3
Use this slope to compute the value of y and slope at midpoint
y (0.25) 2 3(0.25) 2.75
k 2 f (0.25,2.75) 4e 0.8( 0.25 ) 0.5(2.75) 3.510611
Use this slope to calculate another value of y and slope at the
midpoint
y (0.25) 2 3.510611(0.25) 2.877653
k3 f (0.25,2.877653) 4e 0.8( 0.25) 0.5(2.877653) 3.446785
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 16
Example 1 - Solution
Use the this slope to compute the slope at the end of the
interval:
y (0.5) 2 3.071785(0.5) 3.723392
k 4 f (0.5,3.723392) 4e 0.8( 0.5) 0.5(3.723392) 4.105603
Evaluate the final prediction at the end of the interval
1
yi 1 yi (k1 2k 2 2k3 k 4 ) h
6
1
y (0.5) 2 (3 2 3.510611 2 3.446785 4.105603) 0.5
6
3.751669
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 17
System of Equations
Many practical problems in engineering require the solution of a
system of simultaneous ODE rather than a single equation
dy1
dx f1 ( x, y1 , y2 ,..., yn )
dy2 f ( x, y , y ,..., y )
dx 2 1 2 n
.
.
.
dy
n f n ( x, y1 , y2 ,..., yn )
dx
Solution requires that n initial conditions be known at the starting
value x.
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 18
Procedure for Solution Using RK Methods
1. Compute the slopes for all variables at the beginning of the
interval (set of k1’s).
2. Use these slopes to make predictions of the dependent
variable at the midpoint of the interval (y1, y2,…). Use the
midpoint values to compute a first set of slopes at the
midpoint of the interval (set of k2’s)
3. The new set of slopes k2 are used to make another set of
midpoint predictions (y1, y2,…). Use the latest set of
midpoint predictions (y1, y2,…) to compute the second set
of midpoint slopes (set of k3’s)
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 19
Procedure for Solution Using RK Methods
4. Use the values of k3’s to make predictions at the end of the
interval (y1, y2,…). Use the predictions at the end of the
interval to compute the endpoint slopes (set of k4’s)
5. Combine the k’s into a set of increment functions and
brought back to the beginning to make the final prediction
of (y1, y2,…).
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 20
Example 2
Solve the following set of differential equations using the 4th
order RK method, assuming that at x = 0, y1 = 4 and y2 = 6.
Integrate to x = 2 with a step size of 0.5
dy1 dy1
dx 0.5 y dx 0.5 y f1 ( x, y1 , y 2 )
dy 2 4 0.3 y 0.1 y dy 2 4 0.3 y 0.1 y f ( x, y , y )
dx 2 1
dx 2 1 2 1 2
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 21
Example 2 - Solution
1. Compute the slopes for all variables at the beginning of the
interval (set of k1’s).
k1,1 f1 (0,4,6) 0.5(4) 2
k1, 2 f 2 (0,4,6) 4 0.3(6) 0.1(4) 1.8
2. Use set of k1’s to make predictions of (y1, y2,…) at the midpoint
of the interval. Use these (y1, y2,…) to compute a first set of
slopes at the midpoint of the interval (set of k2’s)
h
y1 (0.25) y1 (0) k1,1 4 (2)(0.25) 3.5
2
h
y2 (0.25) y2 (0) k1, 2 6 (1.8)(0.25) 6.45
2
k 2,1 f1 (0.25,3.5,6.45) 0.5(3.5) 1.75
k 2, 2 f 2 (0.25,3.5,6.45) 4 0.3(6.45) 0.1(3.5) 1.715
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 22
Example 2 - Solution
3. Use the set of k2’s to make another set of midpoint
predictions (y1, y2,…). Use the latest set of (y1, y2,…) to
compute the second set of midpoint slopes (set of k3’s)
h 0.5
y1 (0.25) y1 (0) k2,1 4 (1.75)( ) 3.5625
2 2
h 0 .5
y2 (0.25) y2 (0) k2, 2 6 (1.715)( ) 6.42875
2 2
k3,1 f1 (0.25,3.5625,6.42875) 0.5(3.5625) 1.78125
k3, 2 f 2 (0.25,3.5625,6.42875) 4 0.3(6.42875) 0.1(3.5625) 1.715125
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 23
Example 2 - Solution
4. Use the values of k3’s to make predictions at the end of the
interval (y1, y2,…). Use the predictions at the end of the interval
to compute the endpoint slopes (set of k4’s)
y1 (0.5) y1 (0) k3,1h 4 (1.78125)(0.5) 3.109375
y2 (0.5) y2 (0) k3, 2 h 6 (1.715125)(0.5) 6.857563
k 4,1 f1 (0.5,3.109375,6.857563) 0.5(3.109375) 1.554688
k 4, 2 f 2 (0.5,3.109375,6.857563) 4 0.3(6.857563) 0.1(3.109375) 1.631794
5. Combine the k’s into a set of increment functions and brought
back to the beginning to make the final prediction of (y1, y2,…).
1
y1 (0.5) 4 [( 2) 2(1.75) 2(1.78125) (1.554688)] 0.5 3.115234
6
1
y2 (0.5) 6 [(1.8) 2(1.715) 2(1.715125) (1.631794)] 0.5 6.857670
6
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 24
Example 2 - Solution
Repeating the procedure for the remaining steps gives the
following results
x y1 y2
0.0 4 6
0.5 3.115234 6.857670
1.0 2.426171 7.632106
1.5 1.889523 8.326886
2.0 1.471577 8.946865
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 25
Assignment # 5
Computational Methods
22.13, 25.4, 25.8, 25.9, 28.33
Statistics
Check with Dr Faiz
Dr. M. Hrairi MTH2212 - Computational Methods and Statistics 26