You are on page 1of 8

Chapter 4 dfggdgddgdfgdfddgfdefzeffreferfsrfe

Numerical Solution of Initial-Value Problems

4.3 Runge-Kutta Method

The Runge-Kutta Method is an explicit method that achieves the accuracy of a Taylor series
approach without requiring the calculation of higher derivatives. The general form of Runge-
Kutta method can be written as

yn+1 = yn + (xn, yn, h)h (4.3-1)

where (xn, yn, h) is the increment function that represents an average slope over the interval
h.
reetetrrtetrtetetteretetet
terteteertertert
terteertrr
(xn, yn, h)h = a1k1 + a2k2 +  + amkm (4.3-2)

In the above expression k1/h is the slope at x1, k2/h is the slope at x2, and km/h is the slope at
xm for which x1 < x2 < xm  x1 + h. With this definition, the first order Runge-Kutta method is
just Euler’s method.

(xn, yn, h)h = a1k1 = (1)h f(xn, yn)

where a1 = 1 and k1 = h f(xn, yn)

The second order Runge-Kutta method is written as

yn+1 = yn + ak1 + bk2 (4.3-3)

where k1 = h f(xn, yn) and k2 = h f(xn + h, yn+ k1). The four constants a, b, , and  can be
determined by comparing equation (4.3-3) with Taylor’s method of second order.

1
yn+1 = yn + f(xn, yn)h + f’(xn, yn)h2
2!

df f dx f dy f f dy
where f’(xn, yn) = = + y = + y
dx x dx dx x dx

1 f f dy
Therefore yn+1 = yn + f(xn, yn)h + [ + y ] h2 (4.3-4)
2! x dx

From Taylor series expansion of a function with two variables

g g
g(x + r, y + s) = g(x, y) + r + s y + 
x

4
The constant k2 = h f(xn + h, yn+ h) can be expressed as

f f
k2 = h{ f(xn, yn) + h + k1 y + O(h2) }
x

Substitutions of k1 and k2 into yn+1 = yn + ak1 + bk2 yields

f f
yn+1 = yn + a hf(xn, yn) + b hf(xn, yn) + bh2 + b h2f(xn, yn) y + O(h3)
x

The equation can be rearranged to

f f
yn+1 = yn + [a + b] hf(xn, yn) + [b + bf(xn, yn) y ]h2 + O(h3) (4.3-5)
x

Compare equation (4.3-5) with equation (4.3-4)

1 f f dy
yn+1 = yn + f(xn, yn)h + [ + y ] h2 (4.3-4)
2 x dx

We obtain

a+b=1
1
b =
2
1
b =
2

Since there are only three equations for four unknowns a, b, , and , we have infinite
number of second order Runge-Kutta methods depending on the constants chosen. Solving
for a, , and  in terms of b we obtain

1 1
a = 1  b, = , and  =
2b 2b

With different set of constants, the second order Runge-Kutta method is known with different
name.

1 1
Modified Euler method: b= ,a= ,==1
2 2

yn+1 = yn + ak1 + bk2= yn + 0.5k1 + 0.5k2

yn+1 = yn + 0.5h [ f(xn, yn) + h f(xn + h, yn+ hf(xn, yn))]

Midpoint method: b = 1, a = 0,  =  = 0.5

5
yn+1 = yn + ak1 + bk2 = yn + k2 = yn + h[f(xn + 0.5h, yn+ 0.5h f(xn, yn))]

3 1 2
Heun’s method: b= ,a= ,==
4 4 3

1 3
yn+1 = yn + ak1 + bk2 = yn + k1 + k2
4 4

2 2
k1 = h f(xn, yn) , k2 = h f(xn + h, yn+ k1) = h f(xn + h, yn+ k1)
3 3

Example 4.3-1 _____________________________________________________

Solve the following ordinary differential equation (ODE) using various Runge-Kutta method
of order 2 with x = 0.2

dy
= x + y0.5, at x = 1, y = 2
dx

Solution

Modified Euler method: yn+1 = yn + 0.5h [ f(xn, yn) + h f(xn + h, yn+ hf(xn, yn))]

f(xn, yn) = 1 + 20.5 = 2.4142

yn+ hf(xn, yn) = 2 + (0.2)(2.4142) = 2.4828  f(xn + h, yn+ hf(xn, yn)) = 2.7757

yn+1 = 2 + (0.5)(0.2)[ 2.4142 + 2.7757] = 2.5190

Midpoint method: yn+1 = yn + h[f(xn + 0.5h, yn+ 0.5h f(xn, yn))]

f(xn, yn) = 1 + 20.5 = 2.4142, f(xn + 0.5h, yn+ 0.5h f(xn, yn) = 2.5971

yn+1 = 2 + (0.2)(2.5971) = 2.5194

Heun’s method: yn+1 = yn + 0.25k1 + 0.75k2


k1 = h f(xn, yn) = (0.2)( 2.4142)

2 2
k2 = h f(xn + h, yn+ k1) = (0.2)(2.6471)
3 3

yn+1 = 2 + (0.2)[ (0.25)(2.4142) + (0.75)(2.6471)] = 2.5193


dy
Fourth order Runge-Kutta method for = f(x,y)
dx
1
yn+1 = yn + (k1 + 2k2 + 2k3 + k4)
6
where
k1 = hf(xn, yn) k2 = hf(xn + 0.5h, yn + 0.5k1)

6
k3 = hf(xn + 0.5h, yn + 0.5k2) k4 = hf(xn + h, yn + k3)

k2/h and k3/h are the slopes at the mid point. Each of the k/h represents a slope over the given
interval. k1/h is the slope at the beginning of the interval and k4/h is the slope at the end of
interval. Therefore the sum (k1 + 2k2 + 2k3 + k4) / (6h) can be interpreted as a weighted
average of the slope.
dy
EX: Solve = x + y ; y(0) = 1, h = 0.1
dx
k1 = 0.1*(0 + 1) = 0.1*1 => y = y(0) + 0.5k1 = 1 + 0.5*0.1*1 = 1.05
k2 = 0.1*(0.05 + 1.05) = 0.1*1.10 => y = y(0) + 0.5k2 = 1 + 0.5*0.1*1.10 = 1.055
k3 = 0.1*(0.05 + 1.055) = 0.1*1.105 => y = y(0) + k3 = 1 + 1*1.105 = 1.1105
k4 = 0.1*(0.1 + 1.1105) = 0.1*1.2105
y(0.1) = 1 + 0.1*(1 + 2*1.1 + 2*1.105 + 1.2105)/6 = 1.11034
k1 = 0.1*(0.1 + 1.11034) = 0.1*1.21034
=> y = y(0.1) + 0.5k1 = 1.11034 + 0.5*0.1*1.21034 = 1.170857
k2 = 0.1*(0.15 + 1.170857) = 0.1*1.32857
=> y = y(0.1) + 0.5k2 = 1.11034 + 0.5*0.1*1.32857 = 1.17638285

Fourth-order Runge-Kutta method for system of first order differential equations

dy1
= f1(x, y1, y2, …, ym)
dx
dy 2
= f2(x, y1, y2, …, ym) ......
dx
dym
= fm(x, y1, y2, …, ym)
dx
1
yin 1 = yin + (k1,i + 2k2,i + 2k3,i + k4,i) , where i = 1, 2, ..., m and
6
h k k
k1,i = h*fi(xn, y1n , …, ymn ) k2,i = h*fi(xn + , y1n + 1,i , …, ymn + 1,m
2 2 2

)
h k k
k3,i = h*fi(xn + , y1n + 2 ,i , …, ymn + 2,m )
2 2 2
k4,i = h*fi(xn + h, y1n + k3,i, …, ymn + k3,m)

The idea behind the solution to a system of differential equations is similar to the solution of
a single differential equation. All the k’s values divided by step size h are just the slopes of
the curves evaluated at the appropriate x and yi.

7
dy1
EX: Solve = y1y2 + x , y1(0) = 1
dx
dy 2
= xy2 + y1 , y2(0) = -1
dx

using fourth order Runge-Kutta method with step size h = 0.1

Solution

x = xn = 0, y1 = 1, y2 = -1
k1,1 = 0.1*(y1y2 + x) = 0.1*[ (1)(-1) + 0] = -0.1
k1,2 = 0.1*(xy2 + y1) = 0.1*[ (0)(-1) + 1] = 0.1

x = xn + 0.5h = 0 + (0.5)(.1) = 0.05


y1 = y1(0) + 0.5k1,1 = 1 + 0.5(-0.1) = 0.95
y2 = y2(0) + 0.5k1,2 = -1 + 0.5( 0.1) = -0.95
k2,1 = 0.1*(y1y2 + x) = 0.1*[ (0.95)(-0.95) + 0.05] = -0.08525
k2,2 = 0.1*(xy2 + y1) = 0.1*[ (0.05)(-0.95) + 0.95] = 0.9025

x = xn + 0.5h = 0 + (0.5)(.1) = 0.05


y1 = y1(0) + 0.5k2,1 = 1 + 0.5(-0.0852) = 0.9574
y2 = y2(0) + 0.5k2,2 = -1 + 0.5( 0.0902) = -0.9549
k3,1 = 0.1*(y1y2 + x) = 0.1*[ (0.9574)(-0.9549) + 0.05] = -0.0864
k3,2 = 0.1*(xy2 + y1) = 0.1*[ (0.05)(-0.9549) + 0.9574] = 0.0909

x = xn + h = 0 + .1 = 0.1
y1 = y1(0) + k3,1 = 1 + (-0.0864) = 0.9136
y2 = y2(0) + k3,2 = -1 + ( 0.0909) = -0.9091
k4,1 = 0.1*(y1y2 + x) = 0.1*[ (0.9136)(-0.9091) + 0.1] = -0.0730
k4,2 = 0.1*(xy2 + y1) = 0.1*[ (0.1)(-0.9091) + 0.9136] = 0.0823

y1(0.1) = y1(0) + (k1,1 + 2k2,1 + 2k3,1 + k4,1)/6


y1(0.1) = 1 + [(-0.1) + 2(-0.08525) + 2(-0.0864) + (-0.0730)]/6 = 0.9139
y2(0.1) = y2(0) + (k1,2 + 2k2,2 + 2k3,2 + k4,2)/6
y2(0.1) = -1 + [(0.1) + 2(0.09025) + 2(0.0909) + (0.0823)]/6 = -0.9092

x = xn = 0.1, y1 = 0.9139, y2 = -0.9092


k1,1 = 0.1*(y1y2 + x) = 0.1*[ (0.9139)(-0.9092) + 0.1] = -0.07309
k1,2 = 0.1*(xy2 + y1) = 0.1*[ (0.1)(-0.9092) + 0.9139] = 0.082298

8
Example 4.3-2. Solve the following first order system for y1 and y2 at x = 1.

dy1
= y1y2 + x , y1(0) = 1
dx
dy2
= xy2 + y1 , y2(0) = -1
dx
using fourth order Runge-Kutta method with step size h = 0.1
Solution

The MATLAB routines ode23 and ode45 can be used to solve the system. A MATLAB
function must be created to evaluate the slopes as a column vector. The function name in this
example is exode(x, y) which must be saved first in the hard drive with the same name
exode.m.

-------------------------------
% Example 4.3-2 function to evaluate the slopes
function y12 = exode(x,y)
y12(1,1)=y(1)*y(2)+x;
y12(2,1)=x*y(2)+y(1);
-------------------------------

The command ode23 or ode45 is then evaluated from the command windows. MATLAB will
set the step size to achieve a preset accuracy that can be changed by user. We will use both
ode23 and ode45.

[x,y]=ode23('exode',[0; 1],[1; -1])

x= y=
0 1.0000 -1.0000
0.0800 0.9290 -0.9260
0.1800 0.8628 -0.8480
0.2800 0.8174 -0.7828
0.3800 0.7899 -0.7275
0.4800 0.7779 -0.6794
0.5800 0.7797 -0.6364
0.6800 0.7943 -0.5966
0.7800 0.8207 -0.5581
0.8800 0.8585 -0.5189
0.9800 0.9076 -0.4770
1.0000 0.9188 -0.4681

The independent variable can also be specified at certain locations between the initial and
final values and MATLAB will provide the dependent value at these locations. However, the
step size h is still controlled by the error tolerance.

>> xspan=0:.1:1;

9
>> [x,y]=ode23('exode',xspan,[1 -1])

x= y=
0 1.0000 -1.0000
0.1000 0.9139 -0.9092
0.2000 0.8522 -0.8341
0.3000 0.8106 -0.7711
0.4000 0.7863 -0.7173
0.5000 0.7772 -0.6704
0.6000 0.7816 -0.6283
0.7000 0.7986 -0.5889
0.8000 0.8274 -0.5503
0.9000 0.8675 -0.5108
1.0000 0.9188 -0.4681

>> [x,y]=ode45('exode',xspan,[1 -1])

x= y=
0 1.0000 -1.0000
0.1000 0.9139 -0.9092
0.2000 0.8522 -0.8341
0.3000 0.8106 -0.7711
0.4000 0.7863 -0.7174
0.5000 0.7772 -0.6705
0.6000 0.7817 -0.6283
0.7000 0.7987 -0.5889
0.8000 0.8274 -0.5504
0.9000 0.8675 -0.5108
1.0000 0.9188 -0.4681

10
Differential equations of higher order, or systems containing equations of mixed order can be
transformed to a set of first order differential equations. For example, consider the following
equation and initial conditions

d 3z 2
2 d z  dz 
3

+ z     2z = 0
dx 3 dx 2  dx 

dz d 2z
z(0) = 1, (0) = 0, (0) =  1
dx dx 2

We can define new variables as follows

dz dy1
z = y1  = = y2
dx dx

d 2z dy 2
= = y3
dx 2
dx

d 3z dy 3
=
dx 3
dx

The original equation and its initial conditions is now equivalent to the following set of
equations and initial conditions

dy1
= y2 , y1(0) = 1
dx

dy 2
= y3 , y2(0) = 0
dx

dy 3
= 2y1 + y23  y12 y3 , y3(0) =  1
dx

11

You might also like