Numerical Solutions of Ordinary Differential Equations
Numerical Solutions of Ordinary Differential Equations
Table of Contents:
1. Learning Outcomes:
After studying this chapter, you should be able to
➢ identify the initial value problem for the first order ordinary
differential equations;
➢ obtain the solution or the initial value problems by using Euler's
method;
➢ obtain the solution of IVPs using Runge-Kutta methods of second
and fourth order;
➢ extrapolate the approximate value of the solutions obtained by the
Runge-Kutta methods of second and fourth order;
2. Introduction:
Solving differential equations, both ordinary and partial, is one of the
most useful and important application of numerical analysis. There are
mainly two common problems we face in finding the numerical solution of
a differential equation. The first one is: When one finds a numerical
solution is sufficiently close to the exact solution? The second problem is
the instability of numerical solution. The actual solution to the problem of
interest is stable, but the errors incurred in the numerical solution are
magnified in such a way that the numerical solution is obviously
incompatible with the actual solution. A numerical method that gives
accurate results and is stable with the least amount of computation time
often requires that it be started with a somewhat less accurate method
and then continued with a more accurate technique. There are many
starting techniques and methods that are used to continue a solution. In
this chapter we shall introduce two such methods namely, Euler's method
and Runge-Kutta Method of second order and fourth order to obtain
numerical solution of ordinary differential equations (ODEs). Initially we
shall focus on solving the first order equations, since, every nth–order
equation is equivalent to a system of n first–order equations. To begin
with, we shall recall few basic concepts from the theory of differential
equations which we shall be referring quite often.
3. Basic Concepts:
In this section, we shall start with few definitions from differential
equations and define some concepts which are involved in the numerical
solution of differential equations.
Definition: An equation relating an unknown function (dependent
variables) to its various derivatives with respect to known functions
(independent variables) is called a differential equation, thus
d2 f
2
+ 2 x f = ex (1)
dx
2u 2u
+ =0 (2)
x 2 y 2
are examples of differential equations.
Value Additions:
Differential equations involving derivatives w.r.t. a single independent
variable are called ordinary differential equations (ODEs) whereas, A
partial differential equations (PDEs) contains partial derivatives w.r.t.
more than one independent variable. Eqn. (1) is an ODE, while Eqn. (2)
is a PDE.
is 3 and 4 respectively.
The general solution of an nth order linear equation is a family of
solutions containing n arbitrary constants. In order to determine these
arbitrary constants, n conditions are required. If these conditions are
given at one point, then these conditions are known as initial conditions
and the differential equation together with the initial conditions is called
an initial value problem (IVP).
If the n conditions are prescribed at more than one point then these
conditions are known as boundary conditions. The differential equation
together with the boundary condition is known as a boundary value
problem (BVP).
We will need to study numerical methods for the solution of the first order
IVP
y ' = f ( y, t ) , y (t 0 ) = y0 …(4)
Starting with the initial values, the solution are hence constructed step by
step through a series of equal intervals in the independent variables so
that as soon as the solution has been carried to x = x i; the next step will
be to evaluate the change in the solution through the interval Δx = h of x i
to xi + 1.
Let us take the interval t 0 ,b over which the solution of the IVP (4) is
solution of the IVP (4), will produce approximate values yk , at the grid
points tk .
Let us now discuss how to construct numerical methods and related basic
concepts with reference to a simple ODE
dy
= y, y (t 0 ) = y 0 , t a, b (5)
dt
Let us define the grid point by
tk = t0 + kh, k = 0,1,. . . . .n , where t0 = a and t0 + n h = b
Separating the variables and integrating, we find that the exact solution
of Eqn. (5) is
y ( t ) = y ( t0 ) e
( t − t0 )
(6)
y ( t n ) = y ( t0 ) e
( tn − t0 )
and
( tn + 1 − t0 )
y ( t n +1 ) = y ( t 0 ) e
Dividing, we get,
y ( t n +1 ) e tn + 1 (t − t )
= tn = e n + 1 n
y ( tn ) e
Hence we have,
y ( t n +1 ) = e h y ( t n ) (7)
value y ( t0 ) .
e h =1 + h + o h ( 2
) (8)
e h =1 + h +
2 h2
2
+o h ( 3
) (9)
e h =1 + h + +
2 h2
2
+
3 h3
6
(
+o h
4
) (10)
and so on.
Now retain (p+l) terms in the expansion of e h and denote the
4. Euler’s Method:
Let us consider the first-order equation which can put in the form
y = f ( y, t ) (12)
dy
where y = . If yi and yi at ti are known, then Eq. (12) can be used to
dt
give yi +1 and yi +1 at ti +1 .
Institute of Lifelong Learning, University of Delhi Pg. 6
Numerical Solutions of Ordinary Differential Equations
h = 0.1. Find the exact error, if the exact value is y (1) = 3.436564.
dy
Example 3: Solve the IVP 3 + 5 y 2 = sin t , y (0.3) = 5 by Euler's method using
dt
h = 0.3. Find the value of y ( 0.9 ) .
= f ( y, t ) = ( sin t − 5 y 2 ) , y (0.3) = 5
dy 1
dt 3
Euler’s method is given by yi +1 = yi + h f ( yi , ti )
where
h = 0.3, for i = 0, t0 = 0.3, y0 = 5,
y1 = y0 + h f ( y0 , t0 ) = 5 + 0.3
1
3
( )
sin ( 0.3) − 5 ( 5 ) = − 7.4704
2
y2 = y1 + h f ( y1 , t1 ) = − 7.4704 + 0.3
1
3
( )
sin ( 0.6 ) − 5 ( −7.4704 ) = − 35.318
2
dy 4 t
Example 4: Solve the IVP = , y (0) =1 by Euler's method using h = 0.5.
dt y
where
Institute of Lifelong Learning, University of Delhi Pg. 8
Numerical Solutions of Ordinary Differential Equations
Value Addition:
Euler’s method is simpler to use since we do not have to compute higher
derivatives at each point. It could also be used to solve higher order
equations.
5. Runge-Kutta Methods:
In order to produce accurate results using Taylor’s method, derivatives of
higher order must be evaluated. This may be difficult, or the higher-order
derivatives may be inaccurate. Methods that require only the first-order
derivative and give results with the same order of truncation error as
Taylor’s method maintain the higher-order derivatives are called the
Runge-Kutta method. Estimates of the derivative must be made at points
Value Addition:
Since Euler's method is of first, it requires h to be very small to attain the
desired accuracy. Hence, very often, the number of steps to be carried
out becomes very large. In such cases, we need higher order methods
like Runge-Kutta to obtain the required accuracy in a limited number of
steps.
yi +1 = h i , (16)
for the curve in Figure 1; and, if we used i = fi +1, the approximation would
be too small. Hence, the correct i needed to give the exact yi +1 lies in the
interval fi fi +1.
Slope = fi
Slope = fi +1
yi =f (yi,ti )
ti h ti + 1
yi y
Figure 1. Curve showing approximations to yi +1i +,1 using slopes fi and fi +1
The trick is to find a technique that will give a good approximation to the
i = a i + bi (17)
where i = f ( yi , ti ) (18)
i = f ( yi + q h i , ti + p h ) (19)
f f
i = f ( yi , ti ) +( yi , ti ) y + ( yi , ti ) t + o (h 2 )
y t
f f
= fi + q h fi ( yi , ti ) + p h ( yi , ti ) t + o (h 2 ) (20)
y t
Where we have used y = qhfi and t = ph, as required by eqn. (19). Eqn.
yi +1 = yi + h i = yi + h ( a i + bi )
f f
= yi + h ( a fi + b fi ) + h 2 b q fi ( yi , ti ) + b p f i ( yi , ti ) + o(h 3 ), (21)
y t
where we have substituted for i and i from eqn. (18) and (20),
h2
yi +1 = yi + h yi + yi
2
h2
= yi + h f ( yi , ti ) + f ( yi , ti ) (22)
2
f y f t f f f f
f= + =y + = f + (23)
y t t t y t y t
Thus, we have
Institute of Lifelong Learning, University of Delhi Pg. 11
Numerical Solutions of Ordinary Differential Equations
h2 f f
yi +1 = yi + h f ( yi , ti ) + fi y ( yi , ti ) + t ( yi , ti ) (24)
2
Comparing this with eqn. (21), we find that (equating terms in like
powers of h)
a + b = 1, b q = ½, bp=½ (25)
yi +1 = yi + h ( a i + bi )
h
= yi + f ( yi , ti ) + f ( yi + h f i , ti + h ) (26)
2
h h
yi +1 = yi + hi = yi + h f yi + fi , ti + (27)
2 2
Value Addition:
1
yi +1 = yi + i + 3i (28)
4
where
i = h f ( yi , ti )
2 2h
i = h f yi + i , ti +
3 3
yi +1 = yi +i (29)
where
i = h f ( yi , ti )
h
i = h f yi + i , ti +
2 2
1
yi +1 = yi + ( i + i ) (30)
2
where
i = h f ( yi , ti )
i = h f ( yi + i , ti + h )
Example 5: Solve the IVP y ' =1+ y 2 , y (0) = 0 and find y ( 0.4 ) with h = 0.2 using
Compare the results with the exact solution y(t)=tant, and find the errors.
0 = h f ( y0 , t0 ) = h (1 + y0 2 ) = 0.2 (1 + 0 ) = 0.2
2 0 2h 2 0 2 0.2
2 2
0 = h f y0 + , t0 + = h 1 + y0 + = 0.2 1 + 0 +
3 3 3 3
= 0.2035556
Thus,
Institute of Lifelong Learning, University of Delhi Pg. 13
Numerical Solutions of Ordinary Differential Equations
1 1
y 1 = y ( 0.2 ) = y0 + 0 + 30 = 0 + 0.2 + 3 0.2035556 = 0.2026667
4 4
To calculate y2 , we need ,
= h 1 + y1 +
3
2 0.2082148
2
= 0.2 1 + 0.2026667 +
3
= 0.223321245
Thus,
y 2 = y ( 0.4 )
1 1
= y1 + 1 + 31 = 0.2026667 + 0.2082148 + 3 0.223321245
4 4
= 0.422211334
0 = h f ( y0 , t0 ) = h (1 + y0 2 ) = 0.2 (1 + 0 ) = 0.2,
0 h 0 0.2
2 2
0 = h f y0 + , t0 + = h 1 + y0 + = 0.2 1 + 0 + = 0.202
2 2 2 2
y1 = y ( 0.2 ) = y0 +0 = 0 + 0.202 = 0.202
1 = h f ( y1 , t1 ) = h (1 + y12 ) = 0.2 1 + (0.202) 2 = 0.2081608,
h 0.2081608
2 2
1 = h f y1 + 1 , t1 + = h 1 + y1 + 1 = 0.2 1 + 0.202 +
2 2 2 2
= 0.21873704
y2 = y ( 0.4 ) = y1 +1 = 0.202 + 0.21873704 = 0.42073704
c) Heun's method :
0 = h f ( y0 , t0 ) = h (1 + y0 2 ) = 0.2 (1 + 0 ) = 0.2
0 = h f ( y0 + 0 , t0 + h ) = h 1 + ( y0 + 0 ) = 0.2 1 + ( 0 + 0.2 ) = 0.208
2 2
1 1
y1 = y ( 0.2 ) = y0 + (0 +0 ) = 0 + ( 0.2 + 0.208) = 0.204
2 2
0 = h f ( y0 , t0 ) = h ( t0 2 + y0 2 ) = 0.1( 0 + 1) = 0.1
2 2 h 2
2 2
2h
0 = h f y0 + 0 , t0 + = h t0 + + y0 + 0
3 3 3 3
2 0.1 2 0.1
2 2
= 0.1 0 + + 1 + = 0.11422222
3 3
Thus,
1 1
y 1 = y ( 0.1) = y0 + 0 + 30 =1+ 0.1+ 3 0.11422222 = 1.11066667
4 4
To calculate y2 , we need,
2 2 h 2
2 2
2h
1 = h f y1 + 1 , t1 + = h t1 + + y1 + 1
3 3 3 3
2 0.1
2
2 0.12435805
2
Thus,
1 1
y 2 = y ( 0.2 ) = y1 + 1 + 31 =1.11066667 + 0.12435805 + 3 0.1452392
4 4
= 1.25068558
c) Heun's method :
0 = h f ( y0 , t0 ) = h ( t0 2 + y0 2 ) = 0.1( 0 + 1) = 0.1
0 = h f ( y0 + 0 , t0 + h ) = h ( t0 + h ) + ( y0 + 0 ) = 0.1 0.01+ 1.21 = 0.122
2 2
1 1
y1 = y ( 0.1) = y0 + (0 +0 ) =1 + ( 0.1+ 0.122 ) =1.111
2 2
1 = h f ( y1 , t1 ) = h ( t12 + y12 ) = 0.1 0.01 + (1.111) = 0.1244321
2
1 = h f ( y1 + 1 , t1 + h ) = h ( t1 + h ) + ( y1 + 1 )
2 2
= 0.1 0.04 + 1.52629247 = 0.15662925
1 1
y2 = y ( 0.2 ) = y1 + (1 +1 ) =1.111+ ( 0.1244321+ 0.15662925) =1.25153068
2 2
Example 7: Solve the IVP y ' = − t y 2 , y (2) =1 and find y ( 2.2 ) with h = 0.1 using
= 0.1 − 2 + 1 − = − 0.16605
2 2
c) Heun's method :
0 = h f ( y0 , t0 ) = h ( − t0 y0 2 ) = 0.1( 2 ) = − 0.2
0 = h f ( y0 + 0 , t0 + h ) = h − ( t0 + h )( y0 + 0 ) = 0.1 − 2.1(1 − 0.2 ) = − 0.1344
2 2
1 1
y1 = y ( 2.1) = y0 + (0 +0 ) =1+ ( − 0.2 − 0.1344 ) = 0.8328
2 2
1 = h f ( y1 , t1 ) = h ( − t1 y1 ) = 0.1 − 2.1( 0.8328 ) = − 0.14564673
2 2
1 = h f ( y1 + 1 , t1 + h ) = h − ( t1 + h ) ( y1 + 1 )
2
= 0.1 − 2.22 ( 0.8328 − 0.14564673) = − 0.10482388
2
1 1
y2 = y ( 2.2 ) = y1 + (1 +1 ) = 0.8328 + ( − 0.14564673 − 0.10482388 ) = 0.70756469
2 2
y
Example 8: Solve the IVP y ' = 3 t + , y (0) =1 and find y ( 0.2 ) with h = 0.1
2
using the following R-K methods of second order
1 1
y 1 = y ( 0.1) = y0 +0 + 30 =1+ 0.05 + 3 0.07166667 = 1.06625
4 4
To calculate y2 , we need ,
y1
1 = h f ( y1 , t1 ) = h 3 t1 + = 0.1 0.3 + 0.533125 = 0.0833125
2
2 2h 2 h y
1 = h f y1 + 1 , t1 + = h 3 t1 + + 1 + 1
3 3 3 2 3
0.0833125
= 0.1 0.5 + 0.533125 + = 0.10608958
3
Thus,
1 1
y 2 = y ( 0.2 ) = y1 + 1 + 31 =1.06625 + 0.0833125 + 3 0.10608958
4 4
= 1.16664531
c) Heun's method :
0 = h f ( y0 , t0 ) = h (1 + y0 2 ) = 0.2 (1 + 0 ) = 0.2
0 = h f ( y0 + 0 , t0 + h ) = h 1 + ( y0 + 0 ) = 0.2 1 + ( 0 + 0.2 ) = 0.208
2 2
1 1
y1 = y ( 0.2 ) = y0 + (0 +0 ) = 0 + ( 0.2 + 0.208) = 0.204
2 2
1 = h f ( y1 , t1 ) = h (1 + y12 ) = 0.2 1 + ( 0.204 ) = 0.2083232
2
The Runge-Kutta Method of Fourth order is perhaps the most widely used
method for solving ODEs. One such method results in
h
( ) ( )
yi +1 = yi + i + 2 − 2 i + 2 + 2 i + wi
6
(31)
where
i = f ( yi , ti )
h h
i = f yi + i , ti +
2 2
h h h
i = f yi + (i −i ) − (i − 2i ) , ti + (32)
2 2 2
h
wi = f yi − (i − i ) + h i , ti + h .
2
i = f ( yi , ti )
h h
i = f yi + i , ti +
2 2
h h
i = f yi + i , ti + (34)
2 2
wi = f ( yi + h i , ti + h )
steps.
y0 = 0.2,
0 = f ( y0 , t0 ) = 4 − 2 y0 t0 = 4 − 2 0.2 0 = 4,
h h h h
0 = f y0 + 0 , t0 + = 4 − 2 y0 + 0 t0 +
2 2 2 2
0.1 0.1
= 4 − 2 0.2 + 4 = 3.96,
2 2
h h h h
0 = f y0 + 0 , t0 + = 4 − 2 y0 + 0 t0 +
2 2 2 2
0.1 0.1
= 4 − 2 0.2 + 3.96 = 3.96,
2 2
w0 = f ( y0 + h 0 , t0 + h ) = 4 − 2 ( y0 + h 0 )( t0 + h )
= 4 − 2 ( 0.2 + 0.1 3.96 )( 0.1 ) = 3.88
Thus,
h
y 1 = y0 + 0 + 20 + 2 0 + w0
6
0.1
= 0.2 + ( 3.96 + 7.92 + 7.92 + 3.88) = 0.595
6
For next iteration, we start from y1 , and calculate the values of 1 ,1 , 1 and
w1 . They are
y0 =1,
0 = f ( y0 , t0 ) = y0 + t0 =1 + 0 =1,
h h h h 0.1 0.1
0 = f y0 + 0 , t0 + = y0 + 0 + t0 + = 1 + 1 + =1.1,
2 2 2 2 2 2
h h h h 0.1 0.1
0 = f y0 + 0 , t0 + = y0 + 0 + t0 + = 1+ 1.1 + =1.105,
2 2 2 2 2 2
Thus,
h 0.1
y 1 = y0 + 0 + 20 + 2 0 + w0 =1+ (1+ 2.2 + 2.210 +1.2105 ) =1.11034
6 6
For next iteration, we start from y1 , and calculate the values of 1 ,1 , 1 and
w1 . They are
steps.
y0 = 0,
0 = f ( y0 , t0 ) = 2 y0 + 3 et = 2 0 + 3 e0 = 3,
0
h
h h h t0 + 0.1
0 = f y0 + 0 , t0 + = 2 y0 + 0 + 3 e 2 = 2 0 + 3 + 3 e0.05
2 2 2 2
= 3.453813289,
h
h h h t0 +
0 = f y0 + 0 , t0 + = 2 y0 + 0 + 3 e 2
2 2 2
0.1
= 20 + 3.453813289 + 3 e0.05 = 3.499194618,
2
w0 = f ( y0 + h 0 , t0 + h ) = 2 ( y0 + h 0 ) + 3 e( 0
t + h)
Thus,
h
y 1 = y0 +0 + 20 + 2 0 + w0
6
0.1
=0+ ( 3 + 2 3.453813289 + 2 3.499194618 + 4.015351678 ) = 0.3486894582
6
For next iteration, we start from y1 , and calculate the values of 1 ,1 , 1 and
w1 . They are
h
h h h t1 +
1 = f y1 + 1 , t1 + = 2 y1 + 1 + 3e
2
2 2 2
0.1
0.1 0.1+
= 2 0.3486894582 + 4.012891671 + 3 e 2
= 4.584170812,
2
h
h h h t1 +
1 = f y1 + 1 , t1 + = 2 y1 + 1 + 3 e
2
2 2 2
0.1
0.1 0.1+
= 2 0.3486894582 + 4.584170812 + 3 e 2
= 4.641298726,
2
w1 = f ( y1 + h 1 , t1 + h ) = 2 ( y1 + h 1 ) + 3 e( t1 + h )
= 2 ( 0.3486894582 + 0.1 4.641298726 ) + 3 e0.2 = 6.887058455
Thus,
h
y 2 = y1 + 1 + 21 + 2 1 + w1
6
0.1
= 0.3486894582 + ( 4.012891671+ 2 4.584170812 + 2 4.641298726 + 6.887058455)
6
= 0.83787094
y0 = 0,
0 = f ( y0 , t0 ) = 2 y0 + 3 et = 2 0 + 3 e0 = 3,
0
h
h h h t0 +
0 = f y0 + 0 , t0 + = 2 y0 + 0 + 3 e 2
2 2 2
0.1
= 20 + 3 + 3 e0.05 = 3.453813289,
2
h h h
0 = f y0 + (0 −0 ) − (0 − 20 ) , t0 +
2 2 2
h
h h t0 +
= 2 y0 + (0 −0 ) − (0 − 20 ) + 3 e 2
2 2
0.1
0.1 0.1
= 20 + − 0.4538133 + 3.9076266 +3e
2
= 3.480397056,
2 2
h
w0 = f y0 − (0 − 0 ) + h 0 , t0 + h
2
h
= 2 y0 − (0 − 0 ) + h 0 + 3e
( t0 + h )
= 4.015351678
2
Thus,
h
( ) (
y 1 = y0 + 0 + 2 − 2 0 + 2 + 2 0 + w0
6 )
=0+
0.1
6
( ( ) ( )
3 + 2 − 2 3.453813289 + 2 + 2 3.480397056 + 4.015351678 )
= 0.3486894582
For next iteration, we start from y1 , and calculate the values of 1 ,1 , 1 and
w1 . They are
1 = f ( y1 , t1 ) = 2 y1 + 3 et = 4.012891671,
1
h
h h h t1 +
1 = f y1 + 1 , t1 + = 2 y1 + 1 + 3 e 2
= 4.584170812,
2 2 2
h h h
1 = f y1 + (1 −1 ) − (1 − 21 ) , t1 +
2 2 2
h
h h t1 +
= 2 y1 + (1 −1 ) − (1 − 21 ) + 3 e 2 = 4.617635569,
2 2
h
w1 = f y1 − (1 − 1 ) + h 1 , t1 + h
2
h
= 2 y1 − (1 − 1 ) + h 1 + 3 e( t1 + h) = 5.289846936
2
Thus,
h
( ) ( )
y 2 = y1 + 1 + 2 − 2 1 + 2 + 2 1 + w1 = 0.8112507529
6
Summary:
Step 2: Find y1 = y0 + h f ( y0 , t0 )
and 2
Exercise:
1. Use Euler’s method to Solve the IVP
dy
= 4 − 2 y t , y (0) = 0.2. Find y (0.4) with h = 0.1 .
dt
1
2. Solve the IVP y ' = , y ( 4 ) = 4, by Euler's method using h = 0.1. Carry
x −4 y
2
out the solution for five time steps. Find the exact error.
3. Use Euler method to find the solution of y ' = t + y , given y ( 0 ) = 1. Find the
order
Compare the results with the exact solution y ( t ) =13 e t/2 − 6 t −12, and find the
errors.
Institute of Lifelong Learning, University of Delhi Pg. 27
Numerical Solutions of Ordinary Differential Equations
9. Solve the IVP y ' = 2 y + 3 et , y (0) = 0 and find y ( 2.1) and y ( 2.2 ) taking h = 0.1
h
yi +1 = yi + i +2i +2i + wi
6
where
i = f ( yi ,ti )
h h
i = f yi + i ,ti +
2 2
h h
i = f yi + i ,ti +
2 2
wi = f ( yi + hi ,ti +h)
E Euler’s Method:
Institute of Lifelong Learning, University of Delhi Pg. 28
Numerical Solutions of Ordinary Differential Equations
yi+1 =hyi ,
1
yi +1 = yi + ( i + i )
2
where
i = h f ( yi , ti )
i = h f ( yi + i , ti + h )
yi +1 = yi +i
where
i = h f ( yi , ti )
h
i = h f yi + i , ti +
2 2
given by
1
yi +1 = yi + i + 3i
4
where
i = h f ( yi , ti )
2 2h
i = h f yi + i , ti +
3 3
given by
h
( ) ( )
yi +1 = yi + i + 2 − 2 i + 2 + 2 i + wi
6
Where
i = f ( yi , ti )
h h
i = f yi + i , ti +
2 2
h h h
i = f yi + (i −i ) − (i − 2i ) , ti +
2 2 2
h
wi = f yi − (i − i ) + h i , ti + h .
2
References: