Professional Documents
Culture Documents
(4.3-1)
where (xn, yn, h) is the increment function that represents an average slope over the interval
h.
(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 Eulers 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 Taylors method of second order.
yn+1 = yn + f(xn, yn)h +
where f(xn, yn) =
Therefore
1
f(xn, yn)h2
2!
f dy
f dy
df
f dx
f
=
+ y
=
+ y
dx
x dx
dx
x
dx
f dy
1 f
[
+ y
] h2
2! x
dx
g
g
+ s y +
x
(4.3-4)
f
f
+ k1 y + O(h2) }
x
f
f
+ b h2f(xn, yn) y + O(h3)
x
f
f
+ bf(xn, yn) y ]h2 + O(h3) (4.3-5)
x
f dy
1 f
[
+ y
] h2
2 x
dx
(4.3-4)
We obtain
a+b=1
1
2
1
b =
2
b =
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
a = 1 b,
1
1
, and =
2b
2b
With different set of constants, the second order Runge-Kutta method is known with different
name.
Modified Euler method:
b=
1
1
,a=
,==1
2
2
b = 1, a = 0, = = 0.5
5
b=
3
1
2
,a=
,==
4
4
3
1
3
k1 +
k2
4
4
2
2
h, yn+
k1)
3
3
Solution
Modified Euler method:
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
Heuns method:
2
2
h, yn+
k1) = (0.2)(2.6471)
3
3
dy
= f(x,y)
dx
1
(k1 + 2k2 + 2k3 + k4)
6
where
6
k1 = hf(xn, yn)
k2 = hf(xn + 0.5h, yn + 0.5k1)
k3 = hf(xn + 0.5h, yn + 0.5k2)
k4 = hf(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.
EX: Solve
dy
= x + y ; y(0) = 1, h = 0.1
dx
k1 = 0.1*(0 + 1) = 0.1*1
= 1.05
k2 = 0.1*(0.05 + 1.05) = 0.1*1.10
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
(k1,i + 2k2,i + 2k3,i + k4,i) , where i = 1, 2, ..., m and
6
k
h
= h*fi(xn, y1n , , ymn )
k2,i = h*fi(xn +
, y1n + 1,i , , ymn +
2
2
yin 1 = yin +
k1,i
k1,m
)
2
k3,i = h*fi(xn +
k
k
h
, y1n + 2,i , , ymn + 2,m )
2
2
2
dy1
= y1y2 + x , y1(0) = 1
dx
dy 2
= xy2 + y1
dx
, y2(0) = -1
= 0.1*[ (1)(-1) + 0]
= 0.1*[ (0)(-1) + 1]
= -0.1
= 0.1
= 0.95
= -0.95
= 0.1*[ (0.95)(-0.95) + 0.05]
= -0.08525
k2,2 = 0.1*(xy2 + y1)
= 0.9025
x = xn + 0.5h = 0 + (0.5)(.1) = 0.05
y1 = y1(0) + 0.5k2,1 = 1 + 0.5(-0.0852)
y2 = y2(0) + 0.5k2,2 = -1 + 0.5( 0.0902)
k3,1 = 0.1*(y1y2 + x)
= 0.9574
= -0.9549
= 0.1*[ (0.9574)(-0.9549) +
0.05] = -0.0864
k3,2 = 0.1*(xy2 + y1)
0.1*[
0.9574] = 0.0909
x = xn + h = 0 + .1 = 0.1
y1 = y1(0) + k3,1
= 1 + (-0.0864)
y2 = y2(0) + k3,2
= -1 + ( 0.0909)
8
= 0.9136
= -0.9091
(0.05)(-0.9549)
k4,1 = 0.1*(y1y2 + x)
0.1] = -0.0730
k4,2 = 0.1*(xy2 + y1)
= 0.1*[ (0.9136)(-0.9091) +
=
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.07309
k1,2 = 0.1*(xy2 + y1)
0.9139]
= 0.1*[ (0.9139)(-0.9092) +
=
0.1*[
(0.1)(-0.9092)
= 0.082298
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
dx
, y2(0) = -1
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=
0
0.0800
0.1800
0.2800
0.3800
0.4800
0.5800
0.6800
0.7800
0.8800
0.9800
1.0000
y=
1.0000
0.9290
0.8628
0.8174
0.7899
0.7779
0.7797
0.7943
0.8207
0.8585
0.9076
0.9188
-1.0000
-0.9260
-0.8480
-0.7828
-0.7275
-0.6794
-0.6364
-0.5966
-0.5581
-0.5189
-0.4770
-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;
>> [x,y]=ode23('exode',xspan,[1 -1])
x=
0
0.1000
0.2000
0.3000
0.4000
0.5000
0.6000
0.7000
0.8000
0.9000
1.0000
y=
1.0000
0.9139
0.8522
0.8106
0.7863
0.7772
0.7816
0.7986
0.8274
0.8675
0.9188
-1.0000
-0.9092
-0.8341
-0.7711
-0.7173
-0.6704
-0.6283
-0.5889
-0.5503
-0.5108
-0.4681
y=
1.0000 -1.0000
0.9139 -0.9092
10
0.2000
0.3000
0.4000
0.5000
0.6000
0.7000
0.8000
0.9000
1.0000
0.8522
0.8106
0.7863
0.7772
0.7817
0.7987
0.8274
0.8675
0.9188
11
-0.8341
-0.7711
-0.7174
-0.6705
-0.6283
-0.5889
-0.5504
-0.5108
-0.4681
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
3
2
d 3z
dz
2 d z
+
z
2z = 0
dx
dx 3
dx 2
z(0) = 1,
dz
d 2z
(0) = 0,
(0) = 1
dx
dx 2
dy1
dz
=
= y2
dx
dx
dy 2
d 2z
=
= y3
2
dx
dx
dy 3
d 3z
=
3
dx
dx
The original equation and its initial conditions is now equivalent to the following set of
equations and initial conditions
dy1
= y2 ,
dx
y1(0) = 1
dy 2
= y3 ,
dx
y2(0) = 0
dy 3
= 2y1 + y23 y12 y3 ,
dx
y3(0) = 1
12