Professional Documents
Culture Documents
Ode
Ode
Ode
CHAPTER VII
Numerical Solution of Ordinary Differential Equations (ODE)
I. Definition
An equation that consists of derivatives is called a differential equation. Differential equations have
applications in all areas of science and engineering. Mathematical formulation of most of the physical
and engineering problems lead to differential equations. So, it is important for engineers and
scientists to know how to set up differential equations and solve them.
Differential equations are of two types
1) ordinary differential equation (ODE)
2) partial differential equations (PDE).
An ordinary differential equation is that in which all the derivatives are with respect to a single
independent variable. Examples of ordinary differential equation include
d2y dy dy
1) 2 y 0, ( 0) 2, y ( 0) 4 ,
dx 2
dx dx
d3y d2y dy d2y dy
2) 3
3 2
5 y sin x , 2
(0) 12, ( 0) 2 , y ( 0 ) 4
dx dx dx dx dx
Note: In this first part, we will see how to solve ODE of the form
dy
f x, y , y 0 y 0
dx
In another section, we will discuss how to solve higher order ordinary differential equations or coupled
(simultaneous) differential equations.
But first, How to write a first order differential equation in the above form?
Example 1
dy
2 y 1.3e x , y 0 5
dx
is rewritten as
dy
1.3e x 2 y , y 0 5
dx
In this case
f x, y 1.3e x 2 y
Example 2
dy
ey x 2 y 2 2 Sin(3x ), y 0 5
dx
is rewritten as
dy 2 Sin(3x ) x 2 y 2
, y 0 5
dx ey
In this case
2 Sin(3x ) x 2 y 2
f x, y
ey
Swiss mathematician who was tutored by Johann Bernoulli. He worked at the Petersburg Academy
and Berlin Academy of Science. He had a phenomenal memory, and once did a calculation in his
head to settle an argument between students whose computations differed in the fiftieth decimal
place. Euler lost sight in his right eye in 1735, and in his left eye in 1766. Nevertheless, aided by his
phenomenal memory (and having practiced writing on a large slate when his sight was failing him),
he continued to publish his results by dictating them. Euler was the most prolific mathematical writer
of all times finding time (even with his 13 children) to publish over 800 papers in his lifetime. He
won the Paris Academy Prize 12 times. When asked for an explanation why his memoirs flowed so
easily in such huge quantities, Euler is reported to have replied that his pencil seemed to surpass
him in intelligence. Franois Arago said of him "He calculated just as men breathe, as eagles
sustain themselves in the air" (Beckmann 1971, p. 143; Boyer 1968, p. 482).
dy
f x, y , y 0 y 0
dx
At x 0 , we are given the value of y y 0 . Let us call x 0 as x 0 . Now since we know the
slope ofy with respect to x , that is, f x, y , then at x x0 , the slope is f x0 , y 0 . Both x0
and y 0 are known from the initial condition y x0 y 0 .
True value
y1,
x0,y0 Predicted
value
Step size, h
y1 y 0
Slope
x1 x0
f x0 , y 0
Thus
y1 y0 f x0 , y0 x1 x0
y1 y0 f x0 , y0 h .
We are able now to use the value of y1 (an approximate value of y at x x1 ) to calculate y 2 ,
which is the predicted value at x 2 ,
y2 y1 f x1 , y1 h
x 2 x1 h
Based on the above equations, if we now know the value of y y i at xi , then
yi 1 yi f xi , yi h
This formula is known as the Eulers method and is illustrated graphically in Figure 2. In some books,
it is also called the Euler-Cauchy method.
True Value
A ball at 1200K is allowed to cool down in air at an ambient temperature of 300K. Assuming
heat is lost only due to radiation, the differential equation for the temperature of the ball is
given by
d
2.2067 10 12 4 81 108 , 0 1200 K
dt
Ordinary Differential Equations 101
Find the temperature at t 480 seconds using Eulers method. Assume a step size of
h 240 seconds.
Numerical Methods for Eng [ENGR 391] [Lyes KADEM 2007]
The problem was solved again using a smaller step size. The results are given below in Table 1.
h=240
For smaller steps
Step size, h 480 Et |t | %
480 -987.81 1635.4 252.54
240 110.32 537.26 82.964
120 546.77 100.80 15.566
60 614.97 32.607 5.0352
30 632.77 14.806 2.2864
Figure 4. Comparison of Eulers method with exact solution for different step sizes.
The exact solution of the ordinary differential equation is given by the solution of a non-linear equation
as:
300
0.92593 ln 1.8519 tan 1 0.333 10 2 0.22067 10 3 t 2.9282
300
The solution to this nonlinear equation is
647.57 K
It can be seen that Eulers method has large errors. This can be illustrated using Taylor series.
dy 1 d2y 1 d3y
y i 1 y i xi 1 xi xi 1 xi 2 xi 1 xi 3 ...
dx xi , yi 2! dx 2 xi , y i
3! dx 3 xi , y i
1 1
f ' ( xi , yi ) xi 1 xi f ' ' ( xi , yi ) xi 1 xi ...
2 3
yi 1 yi f ( xi , yi )
2! 3!
As you can see the first two terms of the Taylor series
f xi , y i 2 f xi , yi 3
Et h h ...
2! 3!
The true error hence is approximately proportional to the square of the step size, that is, as the step
size is halved, the true error gets approximately quartered. However from Table 1, we see that as the
step size gets halved, the true error only gets approximately halved. This is because the true error
being proportioned to the square of the step size is the local truncation error, that is, error from one
point to the next. The global truncation error is however proportional only to the step size as the error
keeps propagating from one point to another.
yi 1 yi f xi , yi h
f xi , yi 2 f xi , yi 3
Et h h ...
2! 3!
Now let us consider a 2nd order method formula. This new formula would include one more term of
the Taylor series as follows:
1
y i 1 yi f xi , yi h f xi , yi h 2
2!
dy
e 2 x 3 y, y 0 5
dx
f x, y e 2 x 3 y
Now since y is a function of x,
f x, y f x , y dy
f x , y
x y dx
x
e 2 x 3 y y e 2 x
3 y e 2 x 3 y
2e 2 x ( 3) e 2 x 3 y
5e 2 x 9 y
The 2nd order formula would be
1
y i 1 y i f xi , y i h
f xi , y i h 2
2!
yi 1 y i e 2 xi 3 y i h
1
2!
5e 2 xi 9 y i h 2
You could easily notice the difficulty of having to find f x, y in the above method. What Runge
and Kutta did was write the 2nd order method as
yi 1 yi a1k1 a 2 k 2 h
where
k1 f xi , yi
k2 f xi p1h, yi q11k1h
This form allows us to take advantage of the 2nd order method without having to calculate f x, y .
But, how do we find the unknowns a1 , a2 , p1 and q11 ? Equating the above equations:
1
y i 1 yi f xi , yi h f xi , yi h 2 and yi 1 yi a1k1 a 2 k 2 h
2!
gives three equations.
a1 a2 1
1
a2 p1
2
1
a2 q11
2
Since we have 3 equations and 4 unknowns, we can assume the value of one of the unknowns. The
other three will then be determined from the three equations. Generally the value of a2 is chosen to
1 2
evaluate the other three constants. The three values generally used for a2 are , 1 and , and
2 3
are known as Heuns Method, Midpoint method and Ralstons method, respectively.
resulting in
1 1
yi 1 yi k1 k 2 h
2 2
where
k1 f x i , y i
k 2 f x i h, y i k 1 h
y Slope f xi h, yi k1h
Slope f xi , yi
yi+1, predicted
1
Average Slope f xi h, yi k1h f xi , yi
2
yi
x
xi xi+1
a1 0
1
p1
2
1
q11
2
resulting in
yi 1 yi k2 h
where
k1 f xi , yi
1 1
k 2 f x i h , y i k1 h
2 2
Example
A ball at 1200K is allowed to cool down in air at an ambient temperature of 300K. Assuming
heat is lost only due to radiation, the differential equation for the temperature of the ball is
given by
d
2.2067 10 12 4 81 108 , 0 1200 K
dt
Find the temperature at t 480 seconds using Runge-Kutta 2nd order method (with Heuns
method). Assume a step size of h 240 seconds.
- Additional resources to analyze the above example
Using smaller step size would increases the accuracy of the result as given in Table 1 and Figure 3
below.
Now, let us compare Eulers method and Runge-Kutta 2nd order method results:
Figure 8. Comparison of Euler and Runge Kutta methods with exact results over time.
NOTE: How do these three methods compare with results obtained if we found f x, y directly?
We know that since we are including first three terms in the series, if the solution is a polynomial of
order two or less (that is, quadratic, linear or constant), any of the three methods are exact. But for
any other case the results will be different.
dy
e 2 x 3 y, y 0 5
dx
If we directly find the f x, y , the first three terms of Taylor series gives
1
yi 1 yi f xi , yi h f xi , yi h 2
2!
where
f x, y e 2 x 3 y
f x, y 5e 2 x 9 y
y 0.6 1.0930
y x e 2 x 4e 3 x
gives
y 0.6 e 2 0.6 4e 3 0.6
0.96239
Then the absolute relative true error is
0.96239 1.0930
t 100
0.96239
13.571%
For the same problem, the results from the Euler and the three Runge-Kutta method are given below
yi 1 yi a1k1 a2 k2 a3k3 a4 k 4 h
where knowing the value of y y i at xi , we can find the value of y=yi+1 at xi 1 , and
h xi 1 xi
The above equation is equated to the first five terms of Taylor series
2 3
dy 1d y 1d y
x , y xi 1 xi x , y xi 1 xi xi 1 xi
2 3
yi 1 yi 2 3 xi , yi
dx i i
2! dx i i
3! dx
4
1d y
xi , y i xi 1 xi 4
4! dx 4
dy
Knowing that f x, y and xi 1 xi h
dx
1 1 1
yi 1 yi f xi , yi h f ' xi , yi h 2 f '' xi , yi h3 f ''' xi , yi h 4
2! 3! 4!
Based on equating the above equations, one of the popular solutions used is
1
yi 1 yi k1 2k2 2k3 k4 h
6
k1 f xi , yi
1 1
k2 f xi h, yi k1h
2 2
1 1
k3 f xi h, yi k 2 h
2 2
k4 f xi h, yi k3h
Solving the same example as above, but considering Runge-Kutta 4 th order method, gives the
following results:
Figure 9. Comparison of Runge-Kutta 4th order method with exact solution for different step sizes.
The effect of step size on the value of the calculated temperature at t=480 seconds, can be seen on
the following table and figure.
Step size,
h 480 Et | t | %
480 -90.278 737.85 113.94
240 594.91 52.660 8.1319
120 646.16 1.4122 0.21807
60 647.54 0.033626 0.0051926
30 647.57 0.00086900 0.00013419
We can also compae the exact results with Eulers method (Runge-Kutta 1 st order method), Heuns
method (Runge-Kutta 2nd order method) and Runge-Kutta 4th order method.
Figure 11. Comparison of Runge-Kutta methods of 1st, 2nd, and 4th order.
There are other versions of the fourth order method just like there are several versions of the second
order methods. Formula developed by Kutta is
1
yi 1 yi k1 3k 2 3k 3 k 4 h
8
where
k1 f xi , yi
1 1
k 2 f xi h, y i hk1
3 3
2 1
k 3 f xi h, y i hk1 hk 2
3 3
k 4 f xi h, yi hk1 hk 2 hk 3
Until now, we have solved using Eulers and Runge-Kutta methods first order ordinary
differential equations of the form
dy
f x , y , y 0 y0
dx
But now, the question is: what do we do to solve simultaneous or coupled differential
equations or differential equations that are higher than first order? For example an n th order
differential equations:
dny d n1 y dy
an n
a n 1 n 1
a1 ao y f x
dx dx dx
By assuming
y z1
dy dz1
z2
dx dx
d2y dz 2
z3
dx 2 dx
d n 1 y dzn 1
zn
dx n 1 dx
d n y dzn 1 d n 1 y dy
n
an 1 n 1
a1 a0 y f x
dx dx an dx dx
1
= a an 1 zn a1 z2 a0 z1 f x
n
dz1
z2 f1 z1 , z2 , , x
dx
dz2
z3 f 2 z1 , z2 , , x
dx
dzn 1
an1 zn a1 z2 a0 z1 f x
dx an
Example
Rewrite the following differential equation as a set of first order differential equations:
d2y dy
3 2
2 5 y e x , y 0 5, y 0 7
dx dx
e 2 z 5 y , z 0 7 .
dz 1 x
dx 3
Now one can apply any of numerical methods used for first order ordinary differential
equations to solve the above first order differential equations.