Part 7 NUMERICAL SOLUTION OF ODE MAT 575
Chapter 13
Runge-Kutta
Method
At the end of this chapter, students should be able to:
Describe fourth-order Runge-Kutta method
Apply fourth-order Runge-Kutta method for first order IVP
Apply fourth-order Runge-Kutta method for 2nd order IVP
13.1 Introduction
We could of course formulate other approximation by applying Taylor series
of higher order (more terms in the series) to obtain better accuracy in our
estimates. Remember, in general we would expect the more terms used to
calculate the approximation, the greater the accuracy for a given step size
(due to less truncation error). If f(x, y) is a relatively simple function of x and
y, then it is often possible to generate the required derivatives/partial
derivatives relatively easy. However, the necessity of calculating the higher
derivatives makes Taylor’s series formula of high order completely unsuitable
even with the use of high speed computers. The Runge-Kutta methods
(Second and Fourth orders) most frequently discussed in numerical books,
attempt to develop formulas equivalent to third, fourth, fifth or even higher
order Taylor series formulas that do not involve partial derivatives of f.
218
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
13.2 Fourth-order Runge-Kutta Method (RK4)
In this section we will discuss the fourth-order Runge-Kutta method (also
known as RK4) for solving first-order initial value problems. The fourth-order
Runge-Kutta is a numerical technique used to solve ordinary differential
f x, y , w ith initial condition y0 y 0 .
dy
equation of the form
dx
This method is more popular and favourable compared to Euler’s and Taylor’s
methods due to its less computational requirement and high accuracy. In fact,
RK4 is one of the most widely used and most successful of all one-step
formulas. This classical fourth-order Runge-Kutta formula is equivalent to a
five-term Taylor series formula (Taylor series of order 4).
h2 ' ' h3 ''' h4 iv
yi 1 y i hy'i yi yi yi
2! 3! 4!
The RK4 formula involves a weighted average values of f(x, y) taken at
different points in the interval xi ≤ x ≤ xi1 . The formula is as follows
y i 1 y i
1
K1 2K 2 2K 3 K 4
6
where,
K1 hf(xi, yi )
h K
K 2 hf xi , yi 1
2 2
h K
K 3 hf x i , y i 2
2 2
K 4 hf(xi h, y i K 3 )
The sum
1
K1 2K 2 2K 3 K 4 can be interpreted as an average
6
slope. We will omit the proof of this method due to the extremely lengthy
algebra. Calculations of this method is quite easy especially with the use of
computers, since the steps are routine and do not involve any computation of
partial derivatives of f.
Also, note that the function if f does not depend on y, then
h
K1 f(xi ) , K 2 K 3 f xi , K 4 f(xi h) .
2
219
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
Steps – RK4 method for 1st order IVP
dy
Identify y' ( x )
dx
Rewrite y' ( x) f ( x, y)
Identify x 0 , y0 y( x0 ) and h
Compute K1, K 2, K3 and K 4
Apply RK4 method
o y i 1 y i K1 2K 2 2K 3 K 4
1
6
Example 1
Use the fourth-order Runge-Kutta method with one-step to compute an
approximation value of y(0.2) to the initial value problem
y 1- x 4y , y(0) = 1
Solution
dy
Identify y( x )
dx
y 1- x 4y
Rewrite y( x) f ( x, y)
f(x,y) 1- x 4y
Identify x 0 , y0 y( x0 ) and h
x0 0 y0 1 h = 0.2
Compute K1, K 2, K3 and K 4
K1 hf(x0 , y 0 )
0.2 f (0, 1)
0.21 0 4(1)
0.2(5) 1
h K
K 2 hf x0 , y 0 1
2 2
0.2 1
0.2 f 0 , 1
2 2
0.2 f (0.1, 1.5)
0.21 0.1 4(1.5) 1.38
220
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
h K
K 3 hf x 0 , y 0 2
2 2
0.2 1.38
0 .2 f 0 ,1
2 2
0.2 f (0.1, 1.69 )
1.532
K 4 0.2 f(0 0.2, 1 1.532 )
0.2 f (0.2, 2.532 )
2.1856
Apply RK4 method
1
y(0.2) y1 y 0 (K1 2K 2 2K 3 K 4 )
6
1
1
1 2(1.38 1.532) 2.1856
6
2.5016
Example 2
1
Given y 1 x where y(0) = 1 use the RK4 method to estimate y(0.5)
y
with one step.
Solution
1
y(0.5) y1 y 0 (K1 2K 2 2K 3 K 4 )
6
f ( x, y) y 1 x
K1 hf(x0 , y 0 )
0.5 f(0,1)
0.5 11 0 0.5
h K
K 2 h f x0 , y0 1
2 2
0.5 0.5
0.5 f 0 ,1
2 2
0.5 f 0.25,1.25
0.5 1.25 1 0.25 0.6988
221
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
0.5 K
K3 h f x0 , y0 2
2 2
0.5 0.6988
K 3 0.5 f 0 , 1
2 2
0.5 f (0.25,1.3494 )
0.5 1.3494 1 0.25 0.7260
K 4 hf(x0 h, y 0 K 3 )
K 4 0.5 f(0 0.5, 1 0.7260)
0.5 f (0.5,1.7260 )
0.5 1.7260 1 0.5 0.9853
1
y(0.5) y1 y 0 (K1 2K 2 2K 3 K 4 )
6
1
1
0.5 20.6988 0.7260 0.9853
6
1 4.3349
1
6
1.7225
Actual solution is y(0.5) 1.7227
Example 3
Use the RK4 method with h = 1 to estimate y(2) for the initial value
problem
dy y
1 y(1) 1
dx x
Solution
RK4
1
y(2) y(1) (K1 2K 2 2K 3 K 4 )
6
1
y1 y 0 (K1 2K 2 2K 3 K 4 )
6
y
f ( x, y ) 1
x
222
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
K1 f(x0 , y 0 )
f(1,1)
1 1 2
h K
K 2 f x0 , y0 1
2 2
1 2
f 1 ,1
2 2
f 1.5,2
2
1 2.3333
1.5
1 2.3333
K 3 f 1 , 1
2 2
f (1.5,2.1667 )
2.6667
1 2.4445
1 .5
K 4 f(1 1, 1 2.4445)
f (2,3.4445 )
3.4445
1 2.7223
2
1
y(2) y(1) (K1 2K 2 2K 3 K 4 )
6
1
y1 y 0 (K1 2K 2 2K 3 K 4 )
6
1 2 22.3333 2.4445 2.7223
1
6
3.3797
Warm up exercise
Consider the initial value problem:
y x 2 y , 0 ≤x ≤2,
y(0) = 0, h = 0.5
dy
(i) Identify y( x )
dx
(ii) Rewrite y( x) f ( x, y)
(iii) Identify x 0 , y0 y( x0 ) and h
(iv) Compute K1, K 2, K3 and K 4
(v) Approximate the solution to y(0.5) and y(1).
223
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
13.3 Second Order Differential Equation
A linear second-order ODE has the form:
y( x) p( x)y( x) q( x)y( x) f ( x)
The equation is called homogeneous if f ( x) 0 . Otherwise, it is called
non-homogeneous.
An initial-value problem for the second-order differential equation consists
of finding a solution y of the differential equation that also satisfies initial
conditions of the form
y( x0 ) y0
y( x 0 ) y1
where y 0 and y1 are given constants.
Existence and Uniqueness
For the initial value problem, the existence and uniqueness theorem states
that if p(x), q(x) and f(x) are continuous on some interval (a,b) containing x 0 ,
then there exists a unique solution y(x) to the ode in the whole interval (a,b).
Theorem
Existence and Uniqueness
Let p(x), q(x), and f(x) be continuous on [a, b], then the differential equation
y( x) p( x)y( x) q( x)y( x) f ( x)
y( x0 ) y0
y' ( x0 ) y'0
has a unique solution defined for all x in [a, b].
224
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
13.4 RK4 for 2nd Order IVP
In this section we will discuss the fourth-order Runge-Kutta method for solving
second-order initial value problems.
When a differential equation involved higher-order derivatives, it is necessary
to convert it to an equivalent system of first-order equations. Consider, for
instance, the second-order differential equation with the initial conditions
d2 y
f ( x, y, y) , for x 0 x xn
dx 2
y( x0 ) y0
y( x 0 ) y0
To convert this initial-value problem to an equivalent one for a pair of first-
order differential equations, introduce the variables y w1 and y w 2 . Now,
the differential equations become the following pair
f1 x ,w1, ,w 2 w 2
dy du1
dx dx
d2 y d dy dy
f2 ( x, w1, w 2 )
dx 2 dx dx dx
with the initial conditions
y( x0 ) w1 ( x0 ) y0
y( x 0 ) w 2 ( x 0 ) y0
RK4 for 2nd order ODE is actually an extension of RK4 for 1st order ODE. The
approximation of y w1,i and y( xi ) w 2,i can be determined by the
following procedures.
w1,i 1 w1,i
1
K1,1 2K 2,1 2K 3,1 K 4,1
6
and
w 2,i 1 w 2,i
1
K1,2 2K 2,2 2K 3,2 K 4,2
6
K1,1 hf1(xi, w1,i, w 2,i )
K1,2 hf2 (xi, w1,i, w 2,i )
h 1 1
K 2,1 hf1 xi , w1,i K1,1, w 2,i K1,2 ,
2 2 2
h 1 1
K 2,2 hf2 xi , w1,i K1,1, w 2,i K1,2 ,
2 2 2
225
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
h 1 1
K 3,1 hf1 xi , w1,i K 2,1, w 2,i K 2,2
2 2 2
h 1 1
K 3,2 hf2 xi , w1,i K 2,1, w 2,i K 2,2
2 2 2
K 4,1 hf1xi h, w1,i K 3,1, w 2,i K 3,2
K 4,2 hf2 xi h, w1,i K 3,1, w 2,i K 3,2
Steps – RK4 Method for 2nd order IVP
Identify y( x )
Rewrite y( x) f2 ( x,u1,u2 )
f1x, u1,, u2 u2
dy
Identify
dx
Identify x 0 , y0 y( x0 ) ; y0 y( x 0 ) and h
Rewrite y( x0 ) u1(0) ; y( x 0 ) u2 (0)
Compute K1,i, K 2,i, K3,i and K 4 ,i for i = 1, 2
Compute
o w1,i 1 w1,i K1,1 2K 2,1 2K 3,1 K 4,1
1
6
o w 2,i 1 w 2,i K1,2 2K 2,2 2K 3,2 K 4,2
1
6
Example 4
Given y xy y with initial condition y(0) 1, y(0) 0 , find y(0.2) and
y(0.2) .
Solution
Rewrite equation as y xy y and h = 0.2
dy
f1( x i , w 1, w 2 ) w 2
dx
dy'
f2 ( x i , w 1, w 2 )
dx
xy y
xw 2 w 1
226
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
u1 (0) 1
u2 (0) 0
h 0.2
K1,1 hf1(xi, w1,i, w 2,i )
0.2f1x0, w1,0 , w 2,0
0.2f10,1,0 0
K1,2 hf2 (xi, w1,i, w 2,i )
0.2f2 x0 , w1,0 , w 2,0
0.2f2 0,1,0
0.2(0 1) 0.2
h 1 1
K 2,1 hf1 xi , w1,i K1,1, w 2,i K1,2
2 2 2
h 1 1
hf1 x0 , w1,0 K1,1, w 2,0 K1,2
2 2 2
0.2f10.1,1, 0.1
0.20.1 0.02
h 1 1
K 2,2 hf2 xi , w1,i K1,1, w 2,i K1,2 ,
2 2 2
h 1 1
hf2 x0 , w1,0 K1,1, w 2,0 K1,2
2 2 2
0.2f2 0.1, 1,0.1
0.20.1(0.1) 1 0.202
h 1 1
K 3,1 hf1 xi , w1,i K 2,1, w 2,i K 2,2
2 2 2
h 1 1
hf1 x0 , w1,0 K 2,1, w 2,0 K 2,2
2 2 2
0.2f10.1,1.01,0 .101
0.2(0.101) 0.0202
h 1 1
K 3,2 hf2 xi , w1,i K 2,1, w 2,i K 2,2
2 2 2
h 1 1
hf2 x0 , w1,0 K 2,1, w 2,0 K 2,2
2 2 2
0.2f2 0.1, 1.01,0.101
0.20.10.101 1.01 0.2040
227
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
K 4,1 hf1xi h, w1,i K 3,1, w 2,i K 3,2
hf1x0 h, w1,0 K 3,1, w 2,0 K 3,2
0.2f10.2, 1.0202,0.2040
0.20.20402 0.0408
K 4,2 hf2 xi h, w1,i K 3,1, w 2,i K 3,2
hf2 x0 h, w1,0 K 3,1, w 2,0 K 3,2
0.2f2 0.2,1.0202,0.2040
0.20.20.20402 1.0202 0.2122
w1,i1 w1,i
1
K1,1 2K 2,1 2K3,1 K 4,1
6
w1,1 w1,0 K1,1 2K 2,1 2K 3,1 K 4,1
1
6
1 0 20.02 0.0202 0.0408
1
6
1.0202
w 2,i1 w 2,i
1
K1,2 2K 2,2 2K3,2 K 4,2
6
w 2,1 w 2,0 K1,2 2K 2,2 2K 3,2 K 4,2
1
6
0 0.2 20.202 0.2040 0.2122
1
6
0.2040
Thus,
y(0.2) 1.0202
y(0.2) 0.2040
Example 4
Consider the second-order initial-value problem
y 2y 2y e2t sin t, for 0 t 1 with y(0) 0.4 and y(0) 0.6
Approximate y(0.2) and y(0.2) if h = 0.1.
Solution
Rewrite equation as y 2y 2y e2t sin t,
228
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
dy
f1( x i ,u1,u 2 ) u 2
dt
dy '
f2 ( x i ,u1,u 2 )
dt
e 2t sin t 2y'2y
e 2t sin t 2u 2 2u1
u1(0) 0.4
u2 (0) 0.6
h 0.1
K1,1 0.1f1t 0 , w1,0 , w 2,0
0.1f10,0.4,0.6
0.1( 0.6) 0.06
K1,2 0.1f 2 t 0 , w1,0 , w 2,0
0.1f2 0,0.4,0.6
0.1(2 0.6 2 0.4) 0.04
h 1 1
K 2,1 0.1f1 t 0 , w 1,0 K1,1, w 2,0 K1,2
2 2 2
0.1f10.05, 0.43,0.62
0.1 0.62 0.062
h 1 1
K 2,2 0.1f 2 x 0 , w 1,0 K1,1, w 2,0 K1,2
2 2 2
0.1f 2 0.05, - 0.43,0.62
0.1 e0.1 sin(0.05 ) 2( 0.43 ) 2( 0.62)
0.0325
h 1 1
K 3,1 hf1 x i , w 1,i K 2,1, w 2,i K 2,2
2 2 2
h 1 1
hf1 x 0 , w 1,0 K 2,1, w 2,0 K 2,2
2 2 2
0.2f10.1,1.01,0 .101
0.2(0.101) 0.0202
h 1 1
K 3,2 hf2 xi , w 1,i K 2,1, w 2,i K 2,2
2 2 2
h 1 1
hf2 x 0 , w 1,0 K 2,1, w 2,0 K 2,2
2 2 2
229
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
K 3,2 0.2f 2 0.1, 1.01,0.101
0.20.10.101 1.01 1.0201
K 4,1 hf1x i h, w 1,i K 3,1, w 2,i K 3,2
hf1x 0 h, w 1,0 K 3,1, w 2,0 K 3,2
0.2f10.2, 1.0202,1.0201
0.21.0201 0.2040
K 4,2 hf2 x i h, w 1,i K 3,1, w 2,i K 3,2
hf2 x 0 h, w 1,0 K 3,1, w 2,0 K 3,2
0.2f2 0.2,1.0202,1.0201
0.20.21.0201 1.0202 0.2448
1
w 1,i 1 w 1,i K1,1 2K 2,1 2K 3,1 K 4,1
6
w 1,1 w 1,0 K1,1 2K 2,1 2K 3,1 K 4,1
1
6
1 0 20.02 0.0202 0.2040
1
6
1.0474
1
w 2,i 1 w 2,i K1,2 2K 2,2 2K 3,2 K 4,2
6
w 2,1 w 2,0 K1,2 2K 2,2 2K 3,2 K 4,2
1
6
0 0.2 20.202 1.0201 0.2448
1
6
0.4815
Thus,
y(0.2) 1.0474
y(0.2) 0.4815
13.5 Application Problems
We stated that many natural laws of science could be formulated as
differential equations. In this section you will be given fast rule to set up a
differential equation for some application problems.
(a) Population Growth
dp
= kp
dt
230
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
where
p - population
t - time
p0 - population at t = 0
k - proportionality factor (constant)
(b) Radioactive Decay
dM
= - kM
dt
where
M - mass of a substance
t - time
M0 - mass at t0
k - proportionality factor (constant)
(c) Newton’s Law of Cooling
dQ
= - k(Q - Qa )
dt
where
Q - temperature of an object
t - time
Qa - temperature of the surrounding medium
k - proportionality factor (constant)
(d) Mixture Problems
dp
= (Rate of amount enters) - (Rate of amount leaves)
dt
where
gal lb
Rate of amount enters = x y
min gal
gal A lb
Rate of amount leaves = x1
min y 2 gal
(e) Electric Circuits
di Q
L + Ri + = E(t)
dt c
where
L - inductor
R - resistor
Q - charge
c - capasitor
E(t) - applied voltage
i - current
231
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
Exercise 13
1. Approximate the solution to the following first-order initial value problems
using the Fourth Runge-Kutta method to find
a) y(0.1) if y( t ) y, y(0) = 1
b) y(0.2) if y(t ) y t, y(0) = 0
c) y(0.3) if y( t ) 1 y 2 , y(0) = 0
d) y(0.1) if y ( x) 1 xy , y(0) = 1
e) y if y( x) cos x , y(0) = 0
2
g) f(1) if f ( x) 2y cos x 2 sin x , f(0) = 1
1
h) y(2) and y(3) if y ( x ) x y 3 , y(1) = 1
dx
2. Given the initial value problem tx 2 where x(0) = 2, determine x(0.5)
dt
correct to 2 decimal places using the RK4 method.
dy
3. Estimate the solution to y 2 t 2 0 , y(1) 0 w hen t 2 using RK4 with
dt
i. h 0.2 ii.h 0.1 iii.h 0.005 .
Compare your answers with the analytical solution. What is your observation?
4. Apply the RK4 method to approximate the solution to the following second-
order initial-value problem with given conditions.
232
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
a) y(0.2) if 2y 5y 3y 0 , y(0) = 3, y(0) 4
b) y(0.5) if y 3y 0 , y(0) = 1, y(0) 3
c) y(0.1) if 4y 4y y 0 , y(0) = 1, y(0) 1.5
d) y(1) if y y sin t, y(0) = 0, y(0) 2
e) y(1.1) if y 3y 2y e t , y(1) = 0, y(1) 0
5. Estimate y(t) for t = 0.1 and t = 1, where y satisfies the initial-value problem
y' ' 6y' 7y 6e2t , y(0) = 5 y’(0) = -3
by using a single step of the fourth-order Runge-Kutta method. Compare the
2 7t 2 2t
approximation with the exact answer if y ( t ) 5e t e e . Comment
5 5
on your approximations.
233