You are on page 1of 28

Chapter 7

Numerical Methods For Ordinary


Differential Equations

7.1 First-Order Initial-Value Problems

7.1.1 Eulers Method


Given the initial-value problem
dy
= f (x, y), y(x0 ) = y0 .
dx
Euler method with step size h consists in using the iterative formula

yn+1 = yn + hf (xn , yn )

to approximate the solution y(x) of the IVP at the mesh points

xn+1 = xn + h = x0 + (n + 1)h, n = 0, 1, 2, . . . .

Note 6 (Truncation error in Eulers Method):

(a) As the truncation is performed after the first term of the Taylor series of y(x), Eulers method is
a first order method with TE = O(h2 ). This error is a local error as it applies at each and every
step as the solution develops.
(b) The global error , which applies to the final solution is O(h), since the number of operations
1
would be proportional to .
h

36
Example 30. Consider the IVP
y 0 = x + y, y(0) = 1.

(a) Use Eulers method to obtain a five-decimal approximation to y(0.5) using the step size h = 0.1.
(b) (i) Estimate the truncation error in your approximation to y(0.1) using the next two terms in
the corresponding Taylor series.
(ii) The exact value for y(0.1) is 1.11034 (to 5 decimal places) . Calculate the error between
the actual value y(0.1) and your approximation y1 . How does this error compare with the
truncation error you obtained in (i)?
(c) The exact value for y(0.5) is 1.79744 (to 5 decimal places) . Calculate the absolute error between
the actual value y(0.5) and your approximation y5 .

Answer . Here f (x, y) = x + y, x0 = 0 and y0 = 1 so that the Eulers scheme becomes

yn+1 = yn + 0.1(xn + yn ) = 0.1xn + 1.1yn .

(a) (i) y1 = 0.1x0 + 1.1y0 = 0.1(0) + 1.1(1) = 1.1


(ii) y2 = 0.1x1 + 1.1y1 = 0.1(0.1) + 1.1(1.1) = 1.22
(iii) Rounding values to 5 decimal places, the remaining values obtained in this manner are

y3 = 1.36200, y4 = 1.52820, y5 = 1.72102.

(iv) Therefore y(0.5) y5 = 1.72102.


(b) (i) The truncation error is 0.01033.
(ii) The actual error is 0.01034.
(c) 0.07642

37
Example 31. Given that the exact solution of the initial-value problem
y 0 = x + y, y(0) = 1
is y(x) = 2ex x 1. The following table shows
(a) the approximate values obtained by using Eulers method with step size h = 0.1
(b) the approximate values with h = 0.05, and the actual values of the solution at the points
x = 0.1, 0.2, 0.3, 0.4, 0.5 :

yn yn exact absolute error absolute error


xn with h = 0.1 with h = 0.05 value with h = 0.1 with h = 0.05
0.0 1.00000 1.00000 1.00000 0.00000 0.00000
0.1 1.10000 1.10500 1.11034 0.01034 0.00534
0.2 1.22000 1.23101 1.24281 0.02281 0.01180
0.3 1.36200 1.38019 1.39972 0.03772 0.01953
0.4 1.52820 1.55491 1.58365 0.05545 0.02874
0.5 1.72102 1.75779 1.79744 0.07642 0.03965

Based on the above information, give some comments.


Answer .
Comments
(a) By comparing the absolute errors , we see that Eulers method is more accurate for the smaller h.
(b) The column of data for h = 0.1 requires only 5 steps, whereas 10 steps are required to reach x = 0.5
with h = 0.05. In general, more calculations are required for smaller h. As the consequence, a
larger roundoff error is expected.
(c) The global error for Eulers method is O(h). It follows that if the step size h is halved, this error
would be approximately halved as well. This can be seen from the above table that when the step
size is halved from h = 0.1 to h = 0.05, the global error at x = 0.5 is reduced from 0.07642 to
0.03965. This is a reduction of approximately one half.

7.1.2 Heuns Method/Improved Eulers Method


In each step of this method, we compute first the auxiliary value

yn+1 = yn + hf (xn , yn ) (1)
and then the new value
h
yn+1 = yn + (f (xn , yn ) + f (xn+1 , yn+1 )) (2).
2
Note 7:
(a) This is an example of a predictor-corrector method-it uses (1) to predict a value of y(xn+1 ) and
then uses (2) to correct this value.
(b) The local TE for Heuns method is O(h3 ).
(c) The global TE for Heuns method is O(h2 ).

38
Example 32. Use Improved Eulers method with step size h = 0.1 to approximate the solution of the
IVP
y 0 = x + y, y(0) = 1
on the interval [0, 1]. The exact solution is y(x) = 2ex x 1. Make a table showing the approximate
values , the actual values together with the absolute errors.

Answer . Here f (x, y) = x + y, x0 = 0 and y0 = 1 so that



(a) yn+1 = yn + 0.1(x n + yn ) = 0.1xn + 1.1yn 

yn+1 = yn + 0.05 (xn + yn ) + (xn+1 + yn+1 )
(b) y1 = 0.1x0 + 1.1y
 0 = 0.1(0) + 1.1(1) = 1.1

y1 = y0 + 0.05 (x0 + y0 ) + (x1 + y1 ) = 1 + 0.05[1 + (0.1 + 1.1)] = 1.11
(c) y2 = 1.11 + 0.1[(0.1 + 1.11) = 1.231
y2 = 1.11 + 0.05[(0.1 + 1.11) + (0.2 + 1.231)] = 1.24205

The remaining calculations are summarized in the following table:


n xn yn exact absolute error
0 0.0 1.00000 1.00000 0.00000
1 0.1 1.11000 1.11034 0.00034
2 0.2 1.24205 1.24281 0.00076
3 0.3 1.39847 1.39972 0.00125
4 0.4 1.58181 1.58365 0.00184
5 0.5 1.79490 1.79744 0.00254
6 0.6 2.04086 2.04424 0.00338
7 0.7 2.32315 2.32751 0.00436
8 0.8 2.64558 2.65108 0.00550
9 0.9 3.01237 3.01921 0.00684
10 1.0 3.42817 3.43656 0.00839
Remark.

(a) Considerable improvement in accuracy over Eulers method.


h3 000 (0.1)3
(b) For the case h = 0.1, the TE in approximation to y(0.1) is y
6 0
= 6
(2) = 0.00033, very
close to the actual error 0.00034.

39
7.1.3 Taylor Series Method of Order p
h2 00 h3 (3) hp
yn+1 = yn + hyn0 + yn + yn + + yn(p)
2! 3! p!
Example 33.

Consider the IVP


y 0 = 2xy, y(1) = 1.
Use Taylor series method of order 2 to approximate y(1.2) using the step size h = 0.1.

Answer . Here f (x, y) = 2xy, x0 = 1, and y0 = 1.


h2
The method is yn+1 = yn + hyn0 + yn00 = yn + 0.1yn0 + 0.005yn00 , n = 0, 1, 2, . . . with xn+1 = xn + h =
2!
xn + 0.1.
(a) When n = 0 : x0 = 1
(i) y 0 = 2xy y00 = 2x0 y0 = 2(1)(1) = 2
(ii) y 00 = 2y + 2xy 0 y000 = 2y0 + 2x0 y00 = 2(1) + 2(1)(2) = 6
y1 = y0 + 0.1y00 + 0.005y000 = 1 + 0.1(2) + 0.005(6) = 1.23
(b) When n = 1 : x1 = x0 + 0.1 = 1.1
(i) y 0 = 2xy y10 = 2x1 y1 = 2(1.1)(1.23) = 2.706
(ii) y 00 = 2y + 2xy 0 y100 = 2y1 + 2x1 y10 = 2(1.23) + 2(1.1)(2.706) = 8.4132
y2 = y1 + 0.1y10 + 0.005y100 = 1.23 + 0.1(2.706) + 0.005(8.4132) = 1.542666 y(x2 ) = y(1.2)

7.1.4 Runge-Kutta Method of Order p


(a) These methods are all of the form
1
yn+1 = yn + (a1 k1 + a2 k2 + + ap kp )
6
X
where ai = 1.
(b) The Fourth-Order Runge-Kutta Method is
1
yn+1 = yn + (k1 + 2k2 + 2k3 + k4 )
6
where
k1 = hf (xn , yn )
k2 = hf (xn + 21 h, yn + 21 k1 )
k3 = hf (xn + 12 h, yn + 21 k2 )
k4 = hf (xn + h, yn + k3 )
This method is a 4th order method, so its TE is O(h5 ).

40
Example 34. Consider y 0 = x + y, y(0) = 1. Use Runge-Kutta method of order 4 to obtain an
approximation to y(0.2) using the step size h = 0.1.

Answer . Here f (x, y) = x + y, x0 = 0, y0 = 1, h = 0.1, xn+1 = xn + 0.1.


(a) k1 = 0.1(xn + yn )
(b) k2 = 0.1(xn + 0.05 + yn + 21 k1 )
(c) k3 = 0.1(xn + 0.05 + yn + 12 k2 )
(d) k4 = 0.1(xn + 0.1 + yn + k3 )
(a) For y1 :
(i) k1 = 0.1(x0 + y0 ) = = 0.1
(ii) k2 = 0.1(x0 + 0.05 + y0 + 12 k1 ) = 0.1(0 + 0.05 + 1 + 0.05) = 0.11
(iii) k3 = 0.1(x0 + 0.05 + y0 + 12 k2 ) = 0.1(0 + 0.05 + 1 + 0.055) = 0.1105
(iv) k4 = 0.1(x0 + 0.1 + y0 + k3 ) = 0.1(0 + 0.1 + 1 + 0.1105) = 0.12105
1
y1 = y0 + (k1 + 2k2 + 2k3 + k4 ) = = 1.110341667
6
(b) For y2 :
(i) k1 = 0.1(x1 + y1 ) = 0.1(0.1 + 1.110341667) = 0.121034166
(ii) k2 = 0.1(x1 + 0.05 + y1 + 12 k1 ) = 0.132085875
(iii) k3 = 0.1(x1 + 0.05 + y1 + 12 k2 ) = 0.132638460
(iv) k4 = 0.1(x1 + 0.1 + y1 + k3 ) = 0.144298012
1
y2 = y1 + (k1 + 2k2 + 2k3 + k4 ) = 1.242805142 y(x2 ) = y(0.2)
6

41
7.1.5 Multi-step Methods
The methods of Euler, Heun, and RK are called one-step methods because the approximation for
the mesh point xn+1 involves information from only the previous mesh point, xn . That is, only the
initial point (x0 , y0 ) is used to compute (x1 , y1 ) and in general, yn is needed to compute yn+1 .

Methods which use more than one previous mesh points to find the next approximation are called
multi-step methods.

7.1.6 Adams-Bashforth/Adams-Moulton Method


This method uses the Adams-Bashforth formula
h
yn+1 = yn + (55fn 59fn1 + 37fn2 9fn3 ), n 3
24
where fj = f (xj , yj ), as a predictor, and the Adams-Moulton formula
h
yn+1 = yn + (9fn+1 + 19fn 5fn1 + fn2 )
24

where fn+1 = f (xn+1 , yn+1 ), as a corrector.

Note 8: (a) This is a order 4 method with TE= O(h5 ).


(b) It is not self starting. To get started, y1 , y2 , y3 must be computed by using a method of same
accuracy or better such as the Runge-Kutta order 4 method.

Example 35. Use the Adams-Bashforth/Adams-Moulton Method with h = 0.2 to obtain an approx-
imation to y(0.8) for the solution of
y 0 = x + y, y(0) = 1.
Answer . With h = 0.2, y(0.8) y4 . We use the RK4 method as the starter method, with x0 =
0, y0 = 1 to obtain
(a) y1 = 1.242800000
(b) y2 = 1.583635920
(c) y3 = 2.044212913
(d) fn = f (xn , yn ) = xn + yn
f0 = x0 + y0 = 0 + 1 = 1
f1 = x1 + y1 = 0.2 + 1.242800000 = 1.442800000
f2 = 0.4 + 1.583635920 = 1.983635920
f3 = 0.6 + 2.044212913 = 2.644212913
0.2 0.2
(e) y4 = y3 + (55f3 59f2 + 37f1 9f0 ) = 2.044212913 + (55(2.644212913) 59(1.983635920) +
24 24
37(1.442800000) 9(1)) = 2.650719504
(f) f4 = f (x4 , y4 ) = x4 + y4 = 0.8 + 2.650719504 = 3.450719504
0.2
(g) y4 = y3 + (9f4 + 19f3 5f2 + f1 ) = 2.651055757 y(0.8)
24

42
7.1.7 Summary:Orders Of Errors For Different Methods
Method order Local TE Global TE
Euler 1 O(h2 ) O(h)
Heun 2 O(h3 ) O(h2 )
Second-Order Taylor Series 2 O(h3 ) O(h2 )
Third-Order Taylor Series 3 O(h4 ) O(h3 )
Fourth-Order Runge-Kutta 4 O(h5 ) O(h4 )
Adams-Bashforth/Adams-Moulton 4 O(h5 ) O(h4 )

43
7.2 Higher-Order Initial Value Problems
The methods use to solve first order IVPs can be applied to higher order IVPs because a higher IVP
can be replaced by a system of first-order IVPs. For example, the second-order initial value problem

y 00 = f (x, y, y 0 ), y(x0 ) = y0 , y 0 (x0 ) = y00

can be decomposed to a system of two first-order initial value problems by using the substitution
y0 = u :
y0 = u , y(x0 ) = y0
u = f (x, y, u) , u(x0 ) = y00
0

Each equation can be solved by the numerical techniques presented earlier. For example, the Euler
method for this system would be

yn+1 = yn + hun
un+1 = un + hf (xn , yn , un )

Remark. The Euler method for a general system of two first order differential equations,

u0 = f (x, y, u) , u(x0 ) = u0
y 0 = g(x, y, u) , y(x0 ) = y0

is given as follows
un+1 = un + hf (xn , yn , un )
yn+1 = yn + hg(xn , yn , un )

Reading Assignment 7.2.1. Use the Euler method to approximate y(0.2) and y 0 (0.2) in two steps,
where y(x) is the solution of the IVP

y 00 + xy 0 + y = 0, y(0) = 1, y 0 (0) = 2.

Answer . Let y 0 = u. Then the equation is equivalent to the system

y0 = u
0
u = xu y

Using the step size h = 0.1, the Euler method is given by

yn+1 = yn + 0.1un
un+1 = un + 0.1(xn un yn )

With x0 = 0, y0 = 1, u0 = y00 = 2, we get


(a) y1 = y0 + 0.1u0 = 1 + 0.1(2) = 1.2
u1 = u0 + 0.1(x0 u0 y0 ) = 2 + 0.1[(0)(2) 1] = 1.9
(b) y2 = y1 + 0.1u1 = 1.2 + 0.1(1.9) = 1.39
u2 = u1 + 0.1(x1 u1 y1 ) = 1.9 + 0.1[(0.1)(1.9) 1.2] = 1.761
That is, y(0.2) y2 = 1.39 and y 0 (0.2) u2 = 1.761.

44
= h (zi+K'1/2)

= h (zi+K'2/2)

= h (zi+K'3/2)
-0.062

0.031
7.2.1 The Linear Shooting Method
Consider the linear second order BVP

y 00 = p(x)y 0 + q(x)y + r(x), axb

with boundary conditions y(a) = , y(b) = . The shooting method replaces the BVP by two IVPs
in the following way:
(a) Let y1 be the solution of the IVP

y 00 = p(x)y 0 + q(x)y + r(x), y(a) = , y 0 (a) = 0.

Replace this second-order IVP by a system of two first-order IVPs. Then solve each of the two
first-order IVPs using, say, the fourth-order Runge-Kutta method to obtain y1 .
(b) Let y2 be the solution of the IVP

y 00 = p(x)y 0 + q(x)y, y(a) = 0, y 0 (a) = 1.

Find y2 as in Step (a).


(c) Find c1 and c2 so that y = c1 y1 + c2 y2 is the solution of the BVP:
(i) p(x)y 0 + q(x)y + r(x) = p(c1 y10 + c2 y20 ) + q(c1 y1 + c2 y2 ) + r = c1 (py10 + qy1 ) + c2 (py20 + qy2 ) + r
(ii) y 00 = c1 y100 + c2 y200 = c1 (py10 + qy1 + r) + c2 (py20 + qy2 ) = c1 (py10 + qy1 ) + c2 (py20 + qy2 ) + c1 r
(iii) (i)=(ii) r = c1 r c1 = 1
(iv) y(a) = c1 y1 (a) + c2 y2 (a) = 1 y1 (a) + c2 0 = y(a) =
(v) Choose c2 so that y(b) = :
y1 (b)
y(b) = c1 y1 (b) + c2 y2 (b) = y1 (b) + c2 y2 (b) = c2 = , if y2 (b) 6= 0
y2 (b)
(d) Therefore
y1 (b)
y(x) = y1 (x) + y2 (x)
y2 (b)
is the solution to the BVP, provided y2 (b) 6= 0.

45
Example 36. Use the shooting method (together with the fourth-order Runge-Kutta method with
1
h = ) to solve the BVP
3
y 00 = 4(y x), 0 x 1, y(0) = 0, y(1) = 2.

Answer .
y0 =

00 0 u , y(0) = 0 [1]
(a) y = 4(y x), y(0) = 0, y (0) = 0 0
u = 4(y x) , u(0) = 0 [2]
(i) Use RK4 to solve [1] and [2] : we obtain
y1 ( 13 ) = 0.02469136, y1 ( 23 ) = 0.21439821 and y1 (1) = 0.80973178

y 0 = u , y(0) = 0 [3]

00 0
(b) y = 4y, y(0) = 0, y (0) = 1
u0 = 4y , u(0) = 1 [4]
(i) Use RK4 to solve [3] and [4] : we obtain
y2 ( 31 ) = 0.35802469, y2 ( 23 ) = 0.88106488 and y2 (1) = 1.80973178

2 y1 (1) 2 + 0.80973178
(c) y(x) = y1 (x) + y2 (x) = y1 (x) + y2 (x)
y2 (1) 1.80973178
y( 31 ) = 0.53116634, y( 23 ) = 1.15351499, y(1) = 2

Note 9:

An advantage of the shooting method is that the existing programs for initial value problems
may be used.

However, the shooting method is sensitive to round-off errors and it becomes rather difficult to
use when there are more than two boundary conditions. For these reasons, we may want to use
alternative methods. This brings us to the next topic.

46
7.2.2 Finite Difference Method
Consider a second order BVP

y 00 + P (x)y 0 + Q(x)y = f (x), y(a) = , y(b) = .

Suppose a = x0 < x1 < < xn1 < xn = b with xi xi1 = h for all i = 1, 2, . . . , n. Let
yi = y(xi ), Pi = P (xi ), Qi = Q(xi ), and fi = f (xi ). Then by replacing y 0 and y 00 with their central
difference approximations in the BVP, we get
yi+1 2yi + yi1 yi+1 yi1
2
+ Pi + Qi yi = fi , i = 1, 2, . . . , n 1.
h 2h
or, after simplifying
 h  h
1 + Pi yi+1 + (h2 Qi 2)yi + (1 Pi )yi1 = h2 fi .
2 2
The last equation, known as a finite difference equation, is an approximation to the DE. It enables
us to approximate the solution at x1 , . . . , xn1 ..

Example 37. Solving BVPs Using Finite Difference Method


Use Finite Difference Method with h = 1 to approximate the solution of the BVP
x
y 00 (1 )y = x, y(1) = 2, y(3) = 1.
5
xi
Answer . Here Pi = 0, Qi = 1 + 5
, fi = xi . Hence, the difference equation is
y1 y2=-1
xi y0=2
yi+1 + (3 + )yi + yi1 = xi , i = 1
5 x0=1 x1=2 x2=3
That is,
x1
y2 + (3 + )y1 + y0 = x1
5
With x1 = 2 and the boundary conditions y0 = 2 and y2 = 1, solving the above equation gives
y1 = 0.3846

Notes : We can improve the accuracy by using smaller h. But for that we have to pay a price, i.e.
we have to solve a larger system of equations.

47
-----------
4.3.3 Finite Difference Method
Consider a second order boundary value problem (BVP)

y 00 + P (x)y 0 + Q(x)y = f (x), y(a) = , y(b) = .

Suppose a = x0 < x1 < < xn1 < xn = b with xi xi1 = h for all i = 1, 2, . . . , n. Let
yi = y(xi ), Pi = P (xi ), Qi = Q(xi ) and fi = f (xi ). Then by replacing y 0 and y 00 with their central
difference approximations in the BVP, we get
yi+1 2yi + yi1 yi+1 yi1
2
+ Pi + Qi yi = fi , i = 1, 2, . . . , n 1.
h 2h
or, after simplifying
 h  h
1 + Pi yi+1 + (h2 Qi 2)yi + (1 Pi )yi1 = h2 fi .
2 2
The last equation, known as a finite difference equation, is an approximation to the differential
equation. It enables us to approximate the solution at x1 , . . . , xn1 ..

Example 17. Solving BVPs Using Finite Difference Method


Use the above difference equation with n = 4 to approximate the solution of the BVP

y 00 4y = 0, y(0) = 0, y(1) = 5.

Answer . Here h = (1 0)/4 = 0.25, xi = 0.25i, i = 0, 1, 2, 3, 4. Hence, the difference equation is

yi+1 2.25yi + yi1 = 0, i = 1, 2, 3


y0=0 y1 y2 y3 y4=5
That is,
xo=0 x1=0.25 x2=0.5 x3=0.75 x4=1
y2 2.25y1 + y0 = 0
y3 2.25y2 + y1 = 0
y4 2.25y3 + y2 = 0

With the BCs y0 = 0 and y4 = 5, the above system becomes

y2 2.25y1 = 0
y3 2.25y2 + y1 = 0
2.25y3 + y2 = 5

Solving the system gives y1 = 0.7256, y2 = 1.6327, and y3 = 2.9479.

Notes : We can improve the accuracy by using smaller h. But for that we have to pay a price, i.e.
we have to solve a larger system of equations.

19

You might also like