Chapter 13
Chapter 13
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
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
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
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
Example 2
1
Given y 1 x where y(0) = 1 use the RK4 method to estimate y(0.5)
y
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 )
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
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
y( x 0 ) y1
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
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
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 )
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
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
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
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
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
Solution
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
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
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
230
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
where
p - population
t - time
p0 - population at t = 0
k - proportionality factor (constant)
231
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
Exercise 13
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-
232
Part 7 NUMERICAL SOLUTION OF ODE MAT 575
5. Estimate y(t) for t = 0.1 and t = 1, where y satisfies the initial-value problem
233