Vous êtes sur la page 1sur 31

zar97/09/03

Numerical Methods For Engineers

page 6-1

6.0 ORDINARY DIFFERENTIAL EQUATIONS


Engineer is one who, within his endowed power,
transforms problems into solutions.- zar97
The best among you is the one who has highest piety.- Meaning of Al-Ayat.

Objectives:

After completing Part Six, students should be able

1. to solve ordinary differential equation using numerical techniques.


2. to assess the reliability of the answers obtained.
3. to choose the best method for any particular problem.

Scope and Preview:

In this part, we will study numerical solutions of ordinary


differential equations (ODEs). Three main sections will be covered: the one-step methods,
the adaptive step size control, and the boundary and eigenvalue problems.
5.1 One-step methods. The one-step methods, namely Runge-Kutta techniques which
has a constant step size, will be employed for solving a single ODE. We will also solve
systems of ODEs using the same methods.
5.2 Adaptive step size control. The section is devoted to an adaptive step size method
as opposed to the constant step size methods. The techniques automatically adjust the
step size in order to minimize the truncation error. This is done in two ways: to include an
adaptive step size in the one-step methods and to introduce multistep methods. The
algorithms retain information of the previous steps in order to apply correction in
subsequent steps
5.3 Boundary value and Eigenvalue problems. Two fundamental approaches will be
used: shooting and finite-difference. For the latter problems, the polynomial and the power
methods will be covered.

zar97/09/03

Numerical Methods For Engineers

page 6-2

6.0 Introduction
For most practical cases in engineering and sciences, God creates the systems in such a
way that the equations describing the relationships depend on unknown functions and their
derivatives . In general, an ordinary differential equation of nth order has the form

dny
y ( n) f ( x , y , y ', y '', y ( 3) , . . . , y ( n 2 ) , y ( n 1) )
n
dx

(6.0-1)

where y is a function of a single independent variable x and n is a positive integer. If the


function y depends on several independent variables i.e. x1, x2, . . . , xm, the equation is
called partial differential equation. In this part, we only concentrate on the ordinary
differential equations (ODEs) of the first-order which has the form

dy
f (x, y)
dx

(6.0-2)

The numerical solution of the ODE will be in the tabular form of x and y values i.e. (xi , yi ),
i=0,1,2,..., n where yi is an estimate of the exact solution y(x) . In particular we will look at
the numerical solution of the ODE when it satisfies the initial condition y(x0) = y0. We may
write the initial-value ODEs as

dy
f ( x , y ),
dx

y ( x0 ) y0

(6.0-3)

There is no single numerical technique that is best for all first-order initial-value problems.
Therefore, we consider several approaches to obtain the solution of eq.(6.0-3).

zar97/09/03

Numerical Methods For Engineers

page 6-3

6.1 One-Step Methods


The solution of eq.(6.0-3) can be estimated in the form
New value = old value + slope x step size
or, mathematically

yi 1 yi h

(6.1-1)

where the estimated slope is used to predict the new value yi+1 using the old value yi
over a distance h. The procedure can be implemented step by step to compute a set of
new values ys and hence, trace out the trajectory of the solution.
Remark: All one-step methods can be expressed in this general form. The only difference
is the way in which the slope is estimated.

I). Eulers Method.

In Eulers method, the slope in eq.(6.1-1) is estimated directly


using the first derivative at xi :

f ( xi , yi )
where f(xi , yi ) is the differential equation evaluated at xi and yi which can be substituted
into eq.(6.1-1):

yi 1 yi f ( xi , yi ) h

i = 0,1,2, . . .

Figure 6.1-1: Graphical depiction of Eulers method.

(6.1-2)

zar97/09/03

Numerical Methods For Engineers

page 6-4

Using Eulers method to estimate the solution of ODE.


Example 6.1-1:

Use Eulers method to numerically integrate the function given below from x
= 0 to x = 2 with h = 0.5.

dy
1 x 1
f ( x, y) (
)
dx
4 y

y(0) = 1

Solution:
y ( 0.5) y1 y ( 0) f ( 0,1)( 0.5)
1 0 1
1.0 ( 0.5) (
)
4 1
1125
.
y (1.0) y2 y ( 0.5) f ( 0.5,1125
. )( 0.5)
1 0.5 1
1125
.
( 0.5) (
)
.

4 1125
129167
.
The remaining computations are shown in table below,
x
0.0
0.5
1.0
1.5
2.0

yEuler
1.00000
1.12500
1.29167
1.48522
1.69563

ytrue
1.00000
1.14564
1.32288
1.52069
1.73205

t %
1.80
2.36
2.33
2.10

zar97/09/03

Numerical Methods For Engineers

page 6-5

Using Eulers method to estimate the solution of ODE.

Use Eulers method to solve the following initial-value problem from x = 0


to x = 3 with h = 0.5. Compare the results with the true solutions.

Worksheet 6.1-1:

dy
xe y x
dx

y(0) = 0

Solution:

y (0.5) y1 y(0) f (0,0)(0.5)

0 (0.5) (0) e0 0
0.0

y (10
. ) y2 y(0.5) f (0.5,0.0)(0.5)

0.0 (0.5) (0.5) e0 0.5


0.5

The remaining computations are shown in table below,


x
0.0
0.5
1.0
1.5
2.0
2.5
3.0

yEuler
0.00000
0.00000
0.50000
1.30326
2.25699
3.36165
4.65500

ytrue
0.00000
0.23610
0.83180
1.64102
2.62308
3.79593
5.18758

t %
100
39.89
20.58
13.96
11.44
10.27

zar97/09/03

Numerical Methods For Engineers

page 6-6

Error Analysis In Eulers Method. In the single-step methods, the truncation error is
due to two sources: local truncation error and the propagated truncation error that is
carried over from the previous step. The sum of these two sources is called the total or
global truncation error.
We may gain insight of the truncation error associated with the Eulers method by deriving
the formula directly from Taylors series expansion. If the function has continuous
derivatives, TS can be expanded about a starting point (xi , yi ),

yi 1 yi yi ' h

yi '' 2 yi( 3) 3
y (n)
h
h . . . i h n Rn
2
3!
n!

where h xi 1 xi

(6.1-3)

and Rn is the remainder defined as

y ( n 1) ( ) n 1
Rn
h
( n 1)!

where xi xi 1

(6.1-4)

Now, if we substitute the derivative function f(xi , yi ) into eq.(6.1-3), we obtain

yi 1

f ' (xi , yi ) 2
f
yi f ( xi , yi )h
h ...
2

( n 1)

(xi , yi ) n
h ( h n 1 )
n!

(6.1-5)

Comparison of eq.(6.1-2) and eq.(6.1-5), we conclude that the truncation error is due to the
remaining terms in the TSE. Therefore, the true local truncation error is given by

Et

f ' (xi , yi ) 2
f
h ...
2

( n 1)

(xi , yi ) n
h ( h n 1 )
n!

(6.1-6)

For sufficiently small h, higher-order terms would decrease appreciably. The approximate
local truncation error can then be written as

Ea

f ' ( xi , yi ) 2
h
2

or

Ea ( h 2 )

(6.1-7)

zar97/09/03

Numerical Methods For Engineers

page 6-7

Error estimate for Eulers method in the solution of ODE.

Estimate the true and the approximate local truncation error associated with
the Eulers method in the first step to solve the following initial-value problem

Example 6.1-2:

y ' 2x 3 x 2 5x 2

y(0) = 0

from x = 0 to x = 3 with h = 0.5.


Solution:
Recall eq.(6.1-6) and eq.(6.1-7),

Et

f ' ( xi , yi ) 2
f
h ...
2

Ea

f ' ( xi , yi ) 2
h
2

( n 1)

( xi , yi ) n
h ( h n 1 )
n!

Derivatives of the derivative function,

f ' ( x , y ) 6x 2 2 x 5
f ' ' (x , y ) 12x 2
f ' ' ' ( x , y ) 12

At starting point x0 = 0 , y0 = 0 and h = 0.5,

f '( 0,0) 5

f ' ' (0,0) 2

f ' ' '( 0,0) 12

Et

5
(2 )
12
( 0.5) 2
( 0.5) 3
(0.5)4 0.61458
2
6
24

Ea

5
( 0.5) 2 0.625
2

zar97/09/03

Numerical Methods For Engineers

page 6-8

Remark:
Higher-order term in Taylor series expansion may be included to improve the
accuracy of the Eulers method. However, the inclusion may not be trivial especially when
the ODE is more complicated i.e. ODE that is a function of both the dependent and
independent variables as in the case of example 6.1-1 which require chain-rule
differentiation. An alternative way is developed by modifying the one-step methods.

Modification and Improvements of Eulers Method.

Two

simple

modifications are made: Heuns Method and Improved Polygon Method.

Heuns Method. The average of the derivatives at the initial and end points of the interval
is used as an improved estimate of the slope. Slope at the initial point,

yi ' f ( xi , yi )

(6.1-8)

is used to estimate the value of yi+1 ,

yi01 yi f (xi , yi ) h

Predictor equation

(6.1-9)

which may then be used to estimate the slope at the end point,

y 'i 1 f ( xi 1 , y 0i 1 )

(6.1-10)

The average of the two slopes is

yi 1

f (xi , yi ) f (xi 1 , yi01 )


yi
h
2

Corrector equation

(6.1-11)

(b)

(a)

x
Figure 6.1-2: Heuns modification-(a) predictor and (b) Corrector.

zar97/09/03

Numerical Methods For Engineers

page 6-9

Note that eq.(6.1-11) is in an iterative form. So, we can iteratively obtain the improved
estimate of yi+1 until it converge to prespecified tolerance. Note also, however, that the
convergence is not necessarily to the true solution. The termination criterion of the
corrector equation is given by
old
yinew
1 yi 1
a
100%
yinew
1

(6.1-12)

Heuns method to solve the ODE.


Example 6.1-4:

Use Heuns method to solve the following initial-value problem

dy
1 x 1
f ( x, y) (
)
dx
4 y

y(0) = 1

from x = 0 to x = 2 with h = 0.5. Compare the results with the true solutions.
Solution:

Predictor equation,

y 0 ( 0.5) y10 y (0) f ( 0,1)(0.5)


1 0 1
1.0 ( 0.5) (
)
4 1
1125
.
Corrector equation,
f (x0 , y0 ) f ( x1 , y10 )
h
2
0.25 0.33333
1.0
(0.5)
2
114583
.

y11 y0

zar97/09/03

Numerical Methods For Engineers

y12

page 6-10

f ( x0 , y0 ) f (x1 , y11 )
y0
h
2
0.25 0.32727
1.0
( 0.5)
2
114432
.
f (x0 , y0 ) f (x1 , y12 )
h
2
0.25 0.32771
1.0
( 0.5)
2
114443
.

y13 y0

Predictor equation,

y 0 (1.0) y20 y ( 0.5) f ( 0.5,114443


.
)( 0.5)
1 0.5 1
114443
.
(0.5) (
)
.

4 114443
1.30827
Corrector equation,
f (x1 , y1 ) f (x2 , y20 )
h
2
0.32767 0.38218
114443
.

( 0.5)
2
1.32189
The remaining results are shown in table below,
y12 y1

x
0.0
0.5
1.0
1.5
2.0

yHeun
1.00000
1.14443
1.32091

ytrue
1.00000
1.14564
1.32288

t %
0.11
0.15

zar97/09/03

Numerical Methods For Engineers

page 6-11

Improved Polygon Method. The slope at the mid-point of the interval is used to
extrapolate the solution at the end of the interval. The value of the unknown function at the
mid-point of the interval is

yi 1/ 2 yi f (xi , yi )

h
2

(6.1-13)

This value is used to estimate the value of the slope over the interval,

f ( xi 1/2 , yi 1/ 2 )

(6.1-14)

yi 1 yi f (xi 1/ 2 , yi 1/2 ) h

(6.1-15)

(b)

(a)

Figure 6.1-3: Improved Polygon Method (a) value at mid-point (b) improved formula.

zar97/09/03

Numerical Methods For Engineers

page 6-12

Improved Polygon method to solve the ODE.


Example 6.1-5:

Use improved polygon method to solve the following initial-value problem

dy
1 x 1
f ( x, y) (
)
dx
4 y

y(0) = 1

from x = 0 to x = 2 with h = 0.5. Compare the results with the true solutions.
Solution:

y1/2 y0 f (x0 , y0 )(0.25)


y1/2 y (0.25) y (0) f (0,1)(0.25)
1 0 1
1.0 (0.25) (
)
4 1
1.0625
y1 y0 f ( x1/ 2 , y1/ 2 )(0.5)
y1 y (0.5) y (0) f (0.25,1.0625)(0.5)
1 0.25 1
1.0 (0.5) (
)
4 1.0625
114706
.

zar97/09/03

Numerical Methods For Engineers

page 6-13

y1 1/ 2 y1 f (x1 , y1 )(0.25)
y1 1/ 2 y (0.75) y (0.5) f (0.5,114706
.
)(0.25)
1 0.5 1
114706
.
(0.25) (
)
.

4 114706
1.22879
y2 y1 f ( x11/2 , y11/2 )(0.5)
y2 y (10
. ) y (0.5) f (0.75,1.22879 )(0.5)
1 0.75 1
114706
.
(0.5) (
)
4 1.22879
1.32508

The remaining results are shown in table below,


x
0.0
0.5
1.0
1.5
2.0

yHeun
1.00000
1.14706
1.32508

ytrue
1.00000
1.14564
1.32288

t %
0.12
0.17

zar97/09/03

Numerical Methods For Engineers

page 6-14

iv) Runge-Kutta Methods.

The Runge-Kutta methods have the accuracy of the


Taylor series approach without having to evaluate the higher derivatives. The methods can
be generalized in the form

yi 1 yi ( xi , yi , h)h

(6.1-16)

where = (xi , yi , h) is called the increment or slope function or the Runge-Kutta slope
and is defined as
n

( xi , yi , h) a1k1 a2 k2 . . . an k n ai ki

(6.1-17)

i 1

where

ai constants

i 1,2,..., n.

and the ks are computed recursively by

k1 f ( xi , yi )
k2 f ( xi p1h, yi q11k1h)
k3 f ( xi p2h, yi [q21k1 q22 k 2 ]h)

(6.1-18)

n 1

kn f ( xi pn 1h, yi [ qn 1, j k j ]h)
j 1

The parameters as , ps , and qs are evaluated by setting the terms in eq.(6.1-16) equal to
the terms in the Taylor series expansion.
Remark:

1. There exists an infinite number of versions of the RK methods since one


of the parameters such as as can be selected infinitely.
2. When n = 1, the first-order RK is basically the Eulers method.
n +1
3. For lower order versions, the local truncation error is of order h and the
n
global truncation error is of order h .

zar97/09/03

Numerical Methods For Engineers

page 6-15

Second-order Runge-Kutta Method. From eq.(6.1-16), the second-order RK method is


written as

yi 1 yi [ a1k1 a2 k2 ]h

(6.1-19)

k1 f ( xi , yi )
k2 f ( xi p1h, yi q11k1h)

(6.1-20)

where

By equating eq.(6.1-19) and the TSE, the following relationships among parameters are
obtained

a1 a2 1
1
a2 p1
2
1
a2q11
2

(6.1-21)

Since we have four unknowns and only three equations, we may assume the value for one
of the parameters, for example, we may assume the value for a2, then solve for the other
three unknowns. An infinite number of 2nd-order versions are obtained as a result of this.
Remark:

Eventhough there are infinitely many versions exists, they yield exactly the
same results if the solution of the ODEs is a constant, a linear, or a
quadratic.

Commonly used 2nd-order RK versions:


. Heuns method with a single corrector (a2 = 1/2). Eq.(6.1-21) gives

a1 = 1/2,

p1 = 1, and

q11 = 1

and the solution is given by

k k2
yi 1 yi 1
h
2

(6.1-22)

where

k1 f ( xi , yi )
k 2 f ( xi h, yi k1h)

(6.1-23)

zar97/09/03

Numerical Methods For Engineers

page 6-16

. Improved Polygon method (a2 = 1). Eq.(6.1-21) gives

a1 = 0,

p1 = 1/2,

and

q11 = 1/2

and the solution is given by

yi 1 yi k2h

(6.1-24)

where

k1 f ( xi , yi )

(6.1-25)

k 2 f ( xi 21 h, yi 21 k1h)
. Ralstons method (a2 = 2/3). ). Eq.(6.1-21) gives

a1 = 1/3,

p1 = 3/4,

and

q11 = 3/4

and the solution is given by

k 2k2
yi 1 yi 1
h
3

(6.1-22)

where

k1 f ( xi , yi )
k 2 f ( xi 34 h, yi 43 k1h)
Remark:

(6.1-23)

This last method provides a minimum bound on the local truncation errors
for the 2nd-order RK methods.

Using 2nd-order Runge-Kutta method to solve the ODE.


Example 6.1-6:

Use Ralstons (2nd-order RK) method to solve the following initial-value

problem

dy
1 x 1
f ( x, y) (
)
y(0) = 1
dx
4 y
from x = 0 to x = 2 with h = 0.5. Compare the results with the true solutions.
Solution:

zar97/09/03

For i = 0,

Numerical Methods For Engineers

page 6-17

step1:

k1 f ( x0 , y0 )

1 0 1
0.25

4 1

k 2 f ( x0 0.375, y0 0.09375)

1 0.375 1
0.31429

4 1.09375

step2:

k1 2 k2
(0.5)
3
0.25 2 ( 0.31429 )
y1 1.0 (0.5)

y1 y0

114643
.
For i = 1,

step1:

k1 f ( x1 , y1 )

1 0.5 1
0.32710

4 114643
.

k 2 f ( x1 0.375, y1 012266
.
)

1 0.875 1
0.36936

4 1.26909

step2:

k1 2 k2
( 0.5)
3
0.32710 2 ( 0.36936)
y2 114643
.
(0.5)

y2 y1

1.32407

The remaining results are shown in table below,


x
0.0
0.5
1.0
1.5
2.0

yRalston
1.00000
1.14643
1.32407

ytrue
1.00000
1.14564
1.32288
1.52069
1.73205

t %
0.07
0.09

zar97/09/03

Numerical Methods For Engineers

page 6-18

Using Eulers method to estimate the solution of ODE.

Use Ralstons method to solve the following initial-value problem from x =


0 to x = 2 with h = 0.5. Compare the results with the true solutions.

Worksheet 6.1-2:

dy
xe y x
dx

y(0) = 0

Solution:
i = 0, step1:

k1 f ( xi , yi ):

k1 f ( x0 , y0 ) 0

k 2 f ( xi 43 h, yi 34 k1h):

k2 f (0 0.375,0 0)
0.375(e 0 1)
0.75

step2:

k 2k2
y1 y (0.5) y0 1
h
3
0 0.75
0.0 (0.5)

3
0.25

The remaining computations are shown in table below,


x
0.0
0.5
1.0
1.5
2.0

yRalston
0.00000
0.25000
0.85263
1.95365

ytrue
0.00000
0.23610
0.83180
1.64102
2.62308

t %
5.89
2.50
19.05

zar97/09/03

Numerical Methods For Engineers

page 6-19

Third-order Runge-Kutta Method. From eq.(6.1-16), the second-order RK method is


written as

yi 1 yi [a1k1 a2 k2 a3k 3 ]h

(6.1-24)

k1 f ( xi , yi )
k2 f ( xi p1h, yi q11k1h)
k 3 f ( xi p2h, yi [q21k1 q22 k 2 ]h)

(6.1-25)

where

which have eight unknowns. By equating eq.(6.1-19) and the TSE, we obtain six equations.
Thus, we must specify two of them in order to determine the remaining six parameters.
Remark:

1. Eventhough there are infinitely many versions exists, they yield exactly
the same results if the solution of the ODEs is a cubic.
4
3
2. The local and global truncation errors are (h ) and (h ), respectively.

One common version is given by

k 4k 2 k 3
yi 1 yi 1
h
6

(6.1-26)

where

k1 f ( xi , yi )
k 2 f ( xi 21 h, yi 21 k1h)

(6.1-27)

k 3 f ( xi h, yi [ k1 2 k2 ]h)
Remark:

If the derivative function depends only on the independent variable x, the


3rd-order RK method is equivalent to the Simpsons 1/3 rule.

zar97/09/03

Numerical Methods For Engineers

page 6-20

Using 3nd-order Runge-Kutta method to solve the ODE.

Use 3rd-order RK method to solve the following initial-value problem


dy
1 x 1
f ( x, y) (
)
y(0) = 1
dx
4 y
from x = 0 to x = 2 with h = 0.5. Compare the results with the true solutions.

Example 6.1-7:

Solution:
For i = 0,

step1:

k1 f ( x0 , y0 )

1 0 1
0.25

4 1

k 2 f ( x0 0.25, y0 0.0625)

1 0.25 1
0.29412

4 1.0625

k 3 f ( x0 0.5, y0 0.16912)

1 0.5 1
0.32075

4 116912
.

step2:
k 4 k2 k 3
y1 y0 1
(0.5)
6

0.25 4(0.29412) 0.32075


y1 1.0 (0.5)
.
114560
6

For i = 1,

step1:

k1 f ( x1 , y1 )

1 0.5 1
0.32734

4 114560
.

k 2 f ( x1 0.25, y1 0.08184)
k 3 f ( x1 0.5, y1 019276
.
)

1 0.75 1
0.35643

4 1.22744

1 10
. 1
0.37359

4 1.33836

zar97/09/03

Numerical Methods For Engineers

page 6-21

step2:
k 4 k2 k 3
y2 y1 1
(0.5)
6

0.32734 4(0.35643) 0.37359


y2 114560
.
(0.5)
1.32282
6

For i = 2,

step1:

k1 f ( x2 , y2 )

1 1.0 1
0.37798

4 1.32282

k 2 f ( x2 0.25, y2 0.09450)
k 3 f ( x2 0.5, y2 0.41577)

1 1.25 1
0.39688

4 1.41732

1 15
. 1
0.35949

4 1.73859

step2:
k 4 k 2 k3
y3 y2 1
(0.5)
6

0.37798 4(0.39688) 0.35949


y3 1.32282 (0.5)
.
151657
6

The remaining results are shown in table below,


x
0.0
0.5
1.0
1.5
2.0

y3rd-order
1.00000
1.14560
1.32282
1.51657
1.72843

ytrue
1.00000
1.14564
1.32288
1.52069
1.73205

t %
0.003
0.005
0.271
0.209

zar97/09/03

Numerical Methods For Engineers

page 6-22

Fourth-order Runge-Kutta Methods. This is the most widely used RK method. Similar
to the previous order, there are an infinite number of versions.
The most common one is called the classical fourth-order RK method and is given by

k 2 k2 2 k 3 k 4
yi 1 yi 1
h
6

(6.1-28)

where

k1 f ( xi , yi )
k 2 f ( xi 12 h, yi 12 hk1 )

(6.1-29)

k 3 f ( xi 21 h, yi 12 hk 2 )
k 4 f ( xi h, yi hk 3 )
Remark:

If the derivative function depends only on the independent variable x, the


4th-order RK method is also equivalent to the Simpsons 1/3 rule.

Using 4th-order Runge-Kutta method to solve the ODE.

Use 4th-order RK method to solve the following initial-value problem


dy
1 x 1
f ( x, y) (
)
y(0) = 1
dx
4 y
from x = 0 to x = 2 with h = 0.5. Compare the results with the true solutions.

Example 6.1-8:

Solution:
For i = 0,

step1:

k1 f ( x0 , y0 )

1 0 1
0.25

4 1

k 2 f ( x0 0.25, y0 0.0625)

1 0.25 1
0.29412

4 1.0625

k 3 f ( x0 0.25, y0 0.0.07353)
k 4 f ( x0 0.5, y0 0.14555)

1 0.25 1
0.29110

4 1.07353

1 0.5 1
0.32735

4 114555
.

zar97/09/03

Numerical Methods For Engineers

page 6-23

step2:
k 2 k2 2 k 3 k 4
y1 y0 1
(0.5)
6

0.25 2(0.29412) 2(0.29110) 0.32735


y1 1.0 (0.5)
.
114565
6

For i = 1, step1:

k1 f ( x1 , y1 )

1 0.5 1
0.32733

4 114565
.

k 2 f ( x1 0.25, y1 0.08183)

1 0.75 1
0.35642

4 1.22748

k 3 f ( x1 0.25, y1 0.08911)

1 0.75 1
0.35432

4 1.23476

k 4 f ( x1 0.5, y1 0.17716)

1 1.0 1
0.37798

4 1.32281

step2:
k 2 k2 2 k 3 k 4
y1 y0 1
(0.5)
6

0.32733 2(0.35642) 2(0.35432) 0.37798


y1 114565
.
(0.5)
1.32288
6

The remaining results are shown in table below,


x
0.0
0.5
1.0
1.5
2.0

y4th-order
1.00000
1.14565
1.32288

ytrue
1.00000
1.14564
1.32288
1.52069
1.73205

t %
0.0009
0.0002

zar97/09/03

Numerical Methods For Engineers

page 6-24

Using the Runge-Kutta method to estimate the solution of ODE.

Use 4th-order RK method to solve the following initial-value problem from


x = 0 to x = 2 with h = 0.5. Compare the results with the true solutions.

Worksheet 6.1-3:

dy
xe y x
dx
Solution:
For i = 0,

y(0) = 0

step1:

k1 f ( x0 , y0 ) (0)( e0 1) 0
k 2 f ( x0 0.25, y0 0) (0.25)( e0 1) 0.5
k 3 f ( x0 0.25, y0 0125
. ) (0.25)( e0.125 1) 0.47062
k 4 f ( x0 0.5, y0 0.23531) (0.5)( e0.23531 1) 0.89516

step2:
k 2 k2 2 k 3 k 4
y1 y0 1
(0.5)
6

0 2(0.5) 2(0.47062) 0.89516


y1 0.0 (0.5)
0.23637
6

The remaining computations are shown in table below,


x
0.0
0.5
1.0
1.5
2.0

yRalston
0.00000
0.23637

ytrue
0.00000
0.23610
0.83180
1.64102
2.62308

t %
0.11

zar97/09/03

Numerical Methods For Engineers

page 6-25

Higher-order Runge-Kutta Methods. The fifth- or higher-order versions are used when
more accurate results are needed. However, the gain in accuracy is usually offset by the
added computational effort and complexity. Reader may refer textbook page 604.

B. Systems of Differential Equations.

There are situations when one


has to solve a system of simultaneous differential equations. The system of n first-order
differential equations can be represented in general as

y '1 f1 ( x , y1 , y2 , . . ., yn )
y '2 f 2 ( x , y1 , y2 , . . ., yn )

y ' n f n ( x , y1 , y2 , . . ., yn )

(6.1-30a)

with n initial conditions,

y1 ( x0 ) y1,0
y1 ( x0 ) y2,0

(6.1-31a)

yn ( x0 ) yn,0
where x0 is a starting value of x.
Eq.(6.1-30) and eq.(6.1-31) can be represented in a vector form as

y f ( x , y)

or


y ' f ( x, y )

(6.1-30b)

y( x0 ) y0

or

y ( x0 ) y0

(6.1-31b)

and

All the methods discussed early for a single differential equation can be used to solve the
system of differential equations. This is achieved by applying the one-step method for
every equation at each step before proceeding to the next step.
Note: There may be cases when several hundred simultaneous differential equations
occurred in engineering and scientific applications.

zar97/09/03

Numerical Methods For Engineers

page 6-26

Using one-step method to solve a system of ODE.


Example 6.1-8:

Solve the following set of differential equations using Eulers method

y '1 0.4 y1 0
y '2 0.2 y1 0.3y2 5 0

y1 (0) 6
y2 ( 0) 4

from x = 0 to x = 2 with h = 0.5.


Solution: The above equations may expressed as

y '1 0.4 y1
y ' 2 5 0.2 y1 0.3y2
Then applying Eulers method at each step,

y1 ( 0.5) 6 [0.4 (6)]( 0.5) 4.8


y2 (0.5) 4 [5 0.2 ( 6) 0.3(4 )]( 0.5) 6.6
Note that the old value of y1 is used in the computation of y2 instead of the new value.
Continuing in the same manner yields

y1 (1.0) 4.8 [0.4 (4.8)](0.5) 384


.
y2 (1.0) 6.6 [5 0.2 (4.8) 0.3( 6.6)](0.5) 6.64
and the remaining results are
x
0.0
0.5
1.0
1.5
2.0

y1
6
4.8
3.84
3.072
2.4576

y2
4
5.7
6.64
7.76
8.7888

zar97/09/03

Numerical Methods For Engineers

page 6-27

Using one-step method to solve a system of ODE.


Example 6.1-9:

Solve the problem in example 6.1-9 using the 4th-order RK method.

Solution:

y '1 f 1 ( x , y1 , y2 ) 0.4 y1
y ' 2 f 2 ( x , y1 , y2 ) 5 0.2 y1 0.3y2
Step1: calculate all the k1 s,

k1,1 f1 (0,6,4 ) 0.4 (6) 2.4


k1,2 f 2 (0,6,4 ) 5 0.2 (6) 0.3(4 ) 2.6
step2: calculate the values of y1 and y2 needed for the calculation of the k2 s,

y1 12 hk1,1 6 12 ( 0.5)(2.4 ) 5.4


y2 12 hk1, 2 4 12 ( 0.5)(2.6) 4.65
Step3: calculate all the k2 s,

k 2 ,1 f 1 ( 0.25,5.4 ,4.65) 0.4(5.4 ) 2.16


k 2 ,2 f 2 ( 0.25,5.4 ,4.65) 5 0.2 (5.4 ) 0.3( 4.65) 2.525
step4: calculate the values of y1 and y2 needed for the calculation of the k3 s,

y1 12 hk 2 ,1 6 12 (0.5)(2.16) 5.46
y2 12 hk 2 ,2 4 12 (0.5)( 2.525) 4.63125
Step5: calculate all the k3 s,

k 3,1 f 1 (0.25,5.46,4.63125) 0.4 (5.46) 2.184


k 3, 2 f 2 ( 0.25,5.46,4.63125) 5 0.2 (5.46) 0.3(4.63125) 2.51863

zar97/09/03

Numerical Methods For Engineers

page 6-28

step6: calculate the values of y1 and y2 needed for the calculation of the k4 s,

y1 hk 3,1 6 (0.5)(2.184) 4.908


y2 hk 3, 2 4 (0.5)(2.51863) 5.25932
Step7: calculate all the k4 s,

k 4 ,1 f 1 ( 0.25,4.908,5.25932 ) 0.4 (4.908) 1.9632


k 4 ,2 f 2 ( 0.25,4.908,5.25932 ) 5 0.2 (4.908) 0.3(5.25932 ) 2.44061
The values of yk(0.5) can be computed using eq.(6.1-28)

k 2k 2 2 k 3 k 4
yk ,1 yk ,0 1
( 0.5)
6

(0.5)
y1,1 6.0
2.4 2(2.16) 2(2.184 ) 1.9632 4.9124
6
( 0.5)
y2 ,1 4.0
2.6 2(2.525) 2( 2.51863) 2.44061 5.26066
6
and the remaining solutions are
x
0.0
0.5
1.0
1.5
2.0

y1
6
4.9124

y2
4
5.26066

zar97/09/03

Numerical Methods For Engineers

page 6-29

Higher-order differential equations.


Higher-order differential equations may be converted into a set of first-order simultaneous
equations. The solution method is similar to the methods discussed early. In general, an
nth-order differential equation has the form

dny
y (n ) ( x ) f (x , y , y ', y '' , y ( 3) , . . . , y ( n 2 ) , y (n 1) )
n
dx

(6.1-32)

with initial conditions,

y ( k ) (x0 ) y0( k )

for

k 0,1,2, . . ., n 1

(6.1-33)

where k represents the kth derivative of the function and both y and f are real-valued
functions.
Eq.(6.1-32) may be reduced to a system of 1st-order simultaneous equations by
introducing the new variables in the following manner

y1 (x ) y (x )
y2 ( x ) y ' ( x ) y '1
y3 ( x ) y ' ' ( x ) y ' 2

(6.1-34)

yn ( x ) y (n 1) (x ) y 'n 1 (x )
and

f ( x , y1 , y2 , . . ., yn 1 ) y 'n ( x )

Therefore, solving eq.(6.1-32) is equivalent to solving the following set of simultaneous


equations,

y '1 y2 ( x )
y '2 y3 (x )

y 'n 1 ( x ) yn ( x )
y 'n ( x ) f (x , y1 , y2 , . . ., yn 1 )

(6.1-34)

zar97/09/03

Numerical Methods For Engineers

page 6-30

Using one-step method to solve a system of ODE.


Example 6.1-10: Convert the following problem into simultaneous first-order equations and

solve using Eulers method


d 2y
dy
y
x 2 1 with initial conditions y ( 0) 5, y ' (0) 2
2
dx
dx
from x = 0 to x = 2 with h = 1.
Solution:

Rearranging the above equation into the form of eq.(6.1-32),

y ' ' f (x , y , y ' ) yy ' x 2 1


Introducing new variables,

y1 ( x ) y (x )

and

y2 ( x ) y ' (x )

we obtain two simultaneous equations as

y '1 (x ) y2 (x )

y1 (0) 5

y ' 2 ( x ) y1 (x ) y2 (x ) x 2 1

y2 ( 0) 2

Then applying Eulers method at each step,

y1 (1.0) 5.0 [ 2.0](1.0) 7.0


y2 (1.0) 2.0 [5.0(2.0) ( 0) 2 1](1.0) 13.0
y1 ( 2.0) 7.0 [13.0](1.0) 20.0
y2 (2.0) 13.0 [7.0(13.0) (1.0)2 1](1.0) 106

x
0.0
0.5
2.0

y1
5.0
7.0
20.0

y2
2.0
13.0
106.0

zar97/09/03

Numerical Methods For Engineers

page 6-31

Using one-step method to solve a system of ODE.


Worksheet 6.1-4:

For a nonlinear self-sustained oscillation, the Vanderpols equation can be

expressed as
3

d 2y
dy
dy
0.3 0.3 3y 0 with i.cs: y ( 0) y '( 0) 1
2
dx
dx
dx
Convert the problem into simultaneous first-order equations and solve by Eulers method from
x = 0 to x = 2 with h = 1.
3

Solution:

Rearranging the above equation into the form of eq.(6.1-32),

y ' ' f ( x , y , y ' ) 01


. y '01
. ( y' )3 y
Introducing new variables,

y1 ( x ) y (x )

and

y2 ( x ) y ' (x )

we obtain two simultaneous equations as

y '1 (x ) y2 (x )

y1 ( 0) 1

y ' 2 ( x ) 01
. y2 01
. ( y2 ) 3 y1

y2 (0) 1

Then applying Eulers method at each step,

y1 (1.0) 1.0 [1.0](1.0) 2.0


y2 (1.0) 1.0 [ 01
. (1) 01
. (1) 3 1](1.0) 0.0
y1 ( 2.0) 2.0 [0.0](1.0) 2.0
y2 (2.0) 0.0 [ 01
. (0.0) ( 0.0) 3 2.0](1.0) 2.0

x
0.0
0.5
2.0

y1
1.0
2.0
2.0

y2
1.0
0.0
-2.0

Vous aimerez peut-être aussi