Euler Method Review
Euler Method Review
In the last chapter, we saw that a computer can easily generate a slope feld for a given frst-order
differential equation. Using that slope feld we can sketch a fair approximation to the graph
of the solution y to a given initial-value problem, and then, from that graph, we fnd fnd an
approximation to y(x) for any desired x in the region of the sketched slope feld. The obvious
question now arises: Why not let the computer do all the work and just tell us the approximate
value of y(x) for the desired x ?
Well, why not?
In this chapter, we will develop, use, and analyze one method for generating a “numerical
solution” to a frst-order differential equation. This type of “solution” is not a formula or equation
for the actual solution y(x) , but two lists of numbers,
{ x0 , x1 , x2 , x3 , . . . , x N } and { y0 , y1 , y2 , y3 , . . . , y N }
with each yk approximating the value of y(xk ) . Obviously, a nice formula or equation for y(x)
would be usually be preferred over a list of approximate values, but, when obtaining that nice
formula or equation is not practical, a numerical solution is better than nothing.
The method we will study in this chapter is “Euler’s method”. It is but one of many methods
for generating numerical solutions to differential equations. We choose it as the frst numerical
method to study because is relatively simple, and, using it, you will be able to see many of the
advantages and the disadvantages of numerical solutions. Besides, most of the other methods
that might be discussed are refnements of Euler’s method, so we might as well learn this method
frst.
191
192 Euler’s Numerical Method
Y y(x) Y (x 5 , y5 )
y(x k + 1x) Lk L5
y(x k ) + 1y
1y (x 4 , y4 )
L4
I
y(x k ) --------. L3
L2 (x 3 , y3 )
X L1 (x 1 , y1 ) (x 2 , y2 )
xk 1x x k + 1x y0
x0 X
(a) (b)
Figure 9.1: (a) A single tangent line approximation for the Euler method, and (b) the
approximation of the solution curve generated by fve steps of Euler’s method.
{ x0 , x1 , x2 , x3 , . . . , x N } and { y0 , y1 , y2 , y3 , . . . , y N }
with yk ≈ y(xk ) for each k . Plotting the (xk , yk ) points, and connecting the resulting dots with
short straight lines leads to a piecewise straight approximation to the graph of the solution y(x)
as illustrated in fgure 9.1b. For convenience, let us denote this approximation generated by the
Euler method by y E,1x .
As already indicated, N will denote the number of steps taken. It must be chosen along
with 1x to ensure that x N is the maximum value of x of interest. In theory, both N and the
maximum value of x can be infnite. In practice, they must be fnite.
The precise steps of Euler’s method are outlined and illustrated in the next section.
x0 = 0 and y0 = 1 .
3. Pick a distance 1x for the step size, a positive integer N for the maximum number of
steps, and a maximum value desired for x , x max . These quantities should be chosen so
that
xmax = x0 + N 1x .
Of course, you only choose two of these values, and compute the third. Which two are
chosen depends on the problem.
For no good reason whatsoever, let us pick
1
1x = and N = 6 .
2
Then
1
xmax = x0 + N 1x = 0 + 6 · = 3 .
2
4. Write out the equations
xk+1 = xk + 1x (9.4a)
and
yk+1 = yk + 1x · f (xk , yk ) (9.4b)
Formula (9.4b) for yk+1 is based on approximation (9.2). According to that approximation,
if y(x) is the solution to our initial-value problem and yk ≈ y(xk ) , then
2. Compute x2 and y2 using equation set (9.4) with k = 1 and the values of x1 and y1
from the previous step.
For our example, equation set (9.4 ′ ) with k = 1 and the above values for x1
and y1 yields
1 1
x2 = x1+1 = x1 + 1x = + = 1 ,
2 2
and
y2 = y1+1 = y1 + 1x · f (x1 , y1 )
1 2
y1 − x1 2
= y1 +
10
" 2 #
2
11 1 11 1 290
= + − = .
10 10 10 2 250
3. Compute x3 and y3 using equation set (9.4) with k = 2 and the values of x2 and y2
from the previous step.
For our example, equation set (9.4 ′ ) with k = 2 and the above values for x2
and y2 yields
1 3
x3 = x2+1 = x2 + 1x = 1 + = ,
2 2
and
1 2
y2 − x2 2
y3 = y2+1 = y2 +
10
" 2 #
29 1 29 774,401
= + − 12 = .
250 10 250 625,000
(d), (e), … In each subsequent step, increase k by 1 , and compute x k+1 and yk+1 using
equation set (9.4) with the values of xk and yk from the previous step. Continue until
x N and y N are computed.
For our example (omitting many computational details):
With k + 1 = 4 ,
3 1
x4 = x3+1 = x3 + 1x = + = 2 ,
2 2
and
1 2
y3 − x3 2 = · · · ≈ 1.1676
y4 = y3+1 = y2 + .
10
With k + 1 = 5 ,
1 5
x5 = x4+1 = x4 + 1x = 2 + = ,
2 2
and
1 2
y4 − x4 2 = · · · ≈ 0.9039
y5 = y4+1 = y4 + .
10
With k + 1 = 6 ,
5 1
x6 = x5+1 = x5 + 1x = + = 6 ,
2 2
and
1 2
y5 − x5 2 = · · · ≈ 0.3606
y6 = y5+1 = y5 + .
10
Since we had earlier chosen N , the maximum number of steps, to be 6 , we
can stop computing.
{ x0 , x1 , x2 , x3 , . . . , x N } and { y0 , y1 , y2 , y3 , . . . , y N }
in a table for convenient reference (with a note that yk ≈ y(xk ) for each k ) as done in fgure
9.2a for our example. And, whatever the size of N , it is always enlightening to graph —
as done in fgure 9.2b for our example — the corresponding piecewise straight approximation
y = y E,1x (x) to the graph of y = y(x) by drawing straight lines between each (x k , yk ) and
(xk+1 , yk+1 ) .
Y
k xk yk
0 0 1 1.0
1 0.5 1.1000
2 1.0 1.1960
3 1.5 1.2390 0.5
4 2.0 1.1676
5 2.5 0.9039
6 3.0 0.3606 0
0 0.5 1.0 1.5 2.0 2.5 3.0 X
(a) (b)
method. That, of course, is how one should really carry out the computations in the second part
of Euler’s method.
In fact, Euler’s method may already be one of the standard procedures in your favorite com-
puter math package. Still, writing your own version is enlightening, and is highly recommended
for the good of your soul.
{ x0 , x1 , x2 , x3 , . . . , x N } and { y0 , y1 , y2 , y3 , . . . , y N }
with
yk ≈ y(xk ) for k > 0 .
What’s more, each yk+1 is based on the approximation
with y(xk ) being replaced with approximation yk when k > 0 . So we are computing approxi-
mations based on previous approximations.
Because of this, the accuracy of the approximation yk ≈ y(xk ) , especially for larger values
of k , is a serious issue. Consider the work done in the previous section: Just how well can we
trust the approximation
y(3) ≈ 0.3606
obtained for the solution to initial-value problem (9.3)? In fact, it can be shown that
Y Y
4 4
3 approx. soln. 3
2 2
true soln.
1 1
0 0
0 1 2 3 4X 0 1 /////
//// 3 /////
2 ///// 4 X
−1 −1 IIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIII
(a) (b)
dy 13
= (y − 1)2 with y(0) = − ,
dx 10
is graphed along with the graph of the approximate solution generated from Euler’s method with
1x = 1/2 . Exactly why the graphs appear so different becomes apparent when we superimpose
the slope feld in fgure 9.3b. The differential equation has an unstable equilibrium solution
y = 1 . If y(0) < 1 , as in the above initial-value problem, then the true solution y(x) should
converge to 1 as x → ∞ . Here, however, one step of Euler’s method overestimated the value
of y1 enough that (x1 , y1 ) ended up above equilibrium and in the region where the solutions
diverge away from the equilibrium. The tangent lines to these solutions led to higher and higher
values for the subsequently computed yk ’s . Thus, instead of correctly telling us that
lim y(x) = 1 ,
x→∞
Reducing the Error 199
Y y = y(x) Y y = y(x)
y(x max ) y(x max )
yN
yN
y0 y = ŷ(x)
X X
x0 x max x0 x max
(a) (b)
Figure 9.4: Two approximations y N of y(x max ) where y is the solution to y ′ = f (x, y)
with y(x 0 ) = y0 : (a) Using Euler’s method with 1x equaling the distance
from x 0 to x max . (b) Using Euler’s method with 1x equaling half the distance
from x 0 to x max (Note: ŷ is the solution to y ′ = f (x, y) with y(x 1 ) = y1 .)
lim y(x) = ∞ .
x→∞
A few other situations where blindly applying Euler’s method can lead to misleading results
are illustrated in the exercises (see exercises 9.6, 9.7, and 9.8, 9.9). And these sorts of problems
are not unique to Euler’s method. Similar problems can occur with all numerical methods for
solving differential equations. Because of this, it is highly recommended that Euler’s method
(or any other numerical method) be used only as a last resort. Try the methods developed in the
previous chapters frst. Use a numerical method only if the other methods fail to yield usable
formulas or equations.
Unfortunately, the world is flled with frst-order differential equations for which numerical
methods are the only practical choices. So be sure to skim the next section on improving the
method. Also, if you must use Euler’s method (or any other numerical method), be sure to do a
reality check. Graph the corresponding approximation on top of the slope feld for the differential
equation, and ask yourself if the approximations are reasonable. In particular, watch out that
your numerical solution does not “jump” over an unstable equilibrium solution.
is reduced.
200 Euler’s Numerical Method
1x = 1
1.0
1
1x =
0.5 2
1
true solution 1x =
4
1
1x =
0 8
0 0.5 1.0 1.5 2.0 2.5 3.0 X
Figure 9.5: Graphs of the different piecewise straight line approximations of the solution to
5y ′ − y 2 = −x 2 with y(0) = 1 obtained by using Euler’s method with
different values for the step size 1x = 1/2 . Also graphed is the true solution.
2. The slope in the piecewise straight approximation y = y E,1x (x) is recomputed at more
points, which means that this approximation can better match the bends in the slope feld
for the differential equation.
1 The graph of the “true solution” in fgure 9.5 is actually the graph of a very accurate approximation. The difference
between this graph and the graph of the true solution is less than the thickness of the curve used to sketch it.
Reducing the Error 201
where y N is the approximation to y(xmax ) generated from Euler’s method with step size 1x .
Inequality (9.5) is an error bound. It describes the worst theoretical error in using y N for
y(xmax ) . In practice, the error may be much less than suggested by this bound, but it cannot
be any worse (unless there are other sources of error). Since this bound shrinks to zero as 1x
shrinks to zero, we are assured that the approximations to y(xmax ) obtained by Euler’s method
will converge to the correct value of y(xmax ) if we repeatedly use the method with step sizes
shrinking to zero. In fact, if we know the value of M and wish to keep the error below some
small positive value, we can use error bound (9.5) to pick a step size, 1x , that will ensure the
error is below that desired value. Unfortunately,
3. Error bound (9.5) does not take into account the round-off errors that normally arise in
computations.
Let’s briefy consider the problem of round-off errors. Inequality (9.5) is only the error
bound arising from the theoretically best implementation of Euler’s method. In a sense, it is an
“ideal error bound” because it is based on all the computations being done with infnite precision.
This is rarely practical, even when using a computer math package that can do infnite precision
arithmetic — the expressions for the numbers rapidly become too complicated to be usable,
even by the computer math packages, themselves. In practice, the numbers must be converted to
approximations with fnite precision, say, decimal approximations accurate to the nearest 0.0001
as done in the table on page 197.
Don’t forget that the computations in each step involve numbers from previous steps, and
these computations are affected by the round-off errors from those previous steps. So the ultimate
error due to round-off will increase as the number of steps increases. With modern computers,
the round-off error resulting from each computation is usually very small. Consequently, as long
as the number of steps N remains relatively small, the total error due to round-off will usually
be insignifcant compared to the basic error in Euler’s method. But if we attempt to reduce
the error in Euler’s method by taking the step size very, very small, then we must take many,
many more steps to go from x 0 to the desired xmax . It is quite possible to reach a point where
the accumulated round-off error will negate the theoretic improvement in accuracy of the Euler
method described by inequality (9.5).
Better Methods
Be aware that Euler’s method is a relatively primitive method for numerically solving frst-order
initial-value problems. Refnements on the method can yield schemes in which the approxima-
tions to y(xmax ) converge to the true value much faster as the step size decreases. For example,
instead of using the tangent line approximation in each step,
yk+1 = yk + 1x · f (xk , yk ) ,
202 Euler’s Numerical Method
we might employ a “tangent parabola” approximation that better accounts for the bend in the
graphs. (However, writing a program to determine this “tangent parabola”, can be tricky.)
In other approaches, the f (xk , yk ) in the above equation is replaced with a cleverly chosen
weighted average of values of f (x, y) computed at cleverly chosen points near (xk , yk ) . The
idea is that this yields a straight a line approximation with the slope adjusted to reduce the over-
or undershooting noted a page or two ago. At least two of the more commonly used methods,
the “improved Euler method” and the “fourth-order Runge-Kutta method”, take this approach.
Numerous other methods may also worth learning if you are going to make extensive use
of numerical methods. However, an extensive discussion of numerical methods beyond Euler’s
would take us beyond the brief introduction to numerical methods intended by this author for this
chapter. So let us save a more complete discussion of these alternative methods for the future.
∗
9.5 Error Analysis for Euler’s Method
The Problem and Assumptions
Throughout this section we will be concerned with the accuracy of numerical solutions to some
frst-order initial-value problem
dy
= f (x, y) with y(x0 ) = y0 . (9.6)
dx
The precise results will be given in theorem 9.1, somewhere below. For this theorem, L is some
fnite length, and we will assume there is a corresponding rectangle in the XY –plane
1. f and its frst partial derivatives are continuous, bounded functions on R . This “bound-
edness” means there are fnite constants A , B and C such that, at each point in R ,
∂ f ∂ f
|f| ≤ A , ≤ B and ≤ C . (9.7)
∂x ∂y
2. There is a unique solution, y = y(x) , to the given initial-value problem valid over the
interval [x 0 , x0 + L] . (We’ll refer to y = y(x) as the “true solution” in what follows.)
3. The rectangle R contains the graph over the interval [x 0 , x0 + L] of the true solution.
if f and its partial derivatives are bounded on this strip. If f and its partial derivatives are not
bounded on this strip, then fnding the appropriate upper and lower limits for this rectangle is
one of the challenges in using the theorem.
whenever
{ x0 , x1 , x2 , x3 , . . . , x N } and { y0 , y1 , y2 , y3 , . . . , y N }
is a numerical solution to initial-value problem (9.6) obtained from Euler’s method with step
spacing 1x and total number of steps N satisfying
0 < 1x · N ≤ L . (9.9)
This theorem is only concerned with the error inherent in Euler’s method. Inequality (9.8)
does not take into account errors arising from rounding off numbers during computation. For a
good discussion of round-off errors in computations, the interested reader should consult a good
text on numerical analysis
To prove this theorem, we will derive a constant M that makes inequality (9.8) true. (The
impatient can look ahead to equation (9.16) on page 207.) Accordingly, for the rest of this
section, y = y(x) will denote the true solution to our initial-value problem, and
{ x0 , x1 , x2 , x3 , . . . , x N } and { y0 , y1 , y2 , y3 , . . . , y N }
will be an arbitrary numerical solution to initial-value problem (9.6) obtained from Euler’s method
with step spacing 1x and total number of steps N satisfying inequality (9.9).
Also, to simplify discussion, let us agree that, in all the following, k always denotes an
arbitrary nonnegative integer less than than N .
Preliminary Bounds
Our derivation of a value for M will be based on several basic inequalities and facts from
calculus. These include the inequalities
Z b Z b
|A + B| ≤ |A| + |B| and
ψ(s) ds ≤
|ψ(s)| ds
a a
when a < b . Of course, if |ψ(s)| ≤ K for some constant K , then, whether or not a < b ,
Z b
|ψ(s)| ds ≤ K |b − a|
a
Corollary 9.2
Assume φ is a continuous differentiable function on some interval. Assume further that φ ′ ≤ K
on this interval for some constant K . Then, for any two points a and b in this interval,
|φ(a) − φ(b)| ≤ K |b − a| .
The above corollary (with φ(x) = f (x, y(x)) and K = B + C A ) then tells us that
whenever x 0 ≤ a ≤ b ≤ x0 + L .
The second application of the above corollary is with φ(y) = f (xk , y) . Here, y is the
′ ∂ f/ ∂ f/
variable, x remains constant, and φ = ∂ y . Along with the fact that ∂ y < C on rectangle
R , this corollary immediately gives us
Let ǫk+1 be the difference between y(xk + 1x) and the above approximation,
and as
ǫk+1 = [y(xk + 1x) − y(xk )] − f (xk , y(xk )) · 1x .
Error Analysis for Euler’s Method 205
Taking the absolute value of ǫk+1 and applying the last three observations yields
Remarkably, we’ve already found an upper bound for the integrand in the last line (inequality
(9.10), with a = x and b = xk ). Replacing this integrand with this upper bound, and then
doing a little elementary integration yields
Z xk +1x
1
|ǫk+1 | ≤ (B + C A)(x − x k ) d x = (B + C A)(1x)2 .
xk 2
This last inequality combined with equation (9.12) means that we can rewrite the underlying
approximation more precisely as
E k = y(xk ) − yk .
Because y0 = y(x0 ) :
E 0 = y(x0 ) − y0 = 0 .
More generally, using formula (9.13a) for y(xk +1x) and the formula for yk+1 from Euler’s
method, we have
Cleverly rearranging the last line and taking the absolute value leads to
1
|ǫk+1 | ≤ (B + C A)(1x)2 ,
2
1
α = (B + C A) and β = 1 + 1x · C ,
2
|E k+1 | ≤ α(1x)2 + β |E k | .
Remember, E 0 = 0 . Repeatedly applying the last inequality, we then obtain the following:
|E 2 | = |E 1+1 | ≤ α(1x)2 + β |E 1 |
≤ α(1x)2 + βα(1x)2 ≤ (1 + β) α(1x)2 .
|E 3 | = |E 2+1 | ≤ α(1x)2 + β |E 2 |
≤ α(1x)2 + β (1 + β) α(1x)2
≤ α(1x)2 + β + β 2 α(1x)2 ≤ 1 + β + β 2 α(1x)2
� �
.
..
.
You may recognize S N as a partial sum for a geometric series. Whether you do or not, we have
(β − 1)S N = β S N − S N
= β 1 + β + β 2 + · · · + β N −1 − 1 + β + β 2 + · · · + β N −1
= β + β 2 + · · · + β k − 1 + β + β 2 + · · · + β N −1
= βN − 1 .
βN − 1
SN α = α
β −1
(1 + 1x · C) N − 1 (B + C A)
(1 + 1x · C) N − 1 B + C A
= · = .
1 + 1x · C − 1 2 1x · 2C
(1 + 1x · C) N − 1
|E N | ≤ α(1x)2
1x · C
Dividing out one 1x leaves us with
(1 + 1x · C) N − 1 (B + C A)
|E N | ≤ M N ,1x · 1x where M N ,1x = . (9.15)
2C
The claim of theorem 9.1 is almost proven with inequality (9.15). All we need to do now is
to fnd a single constant M such that M N ,1x ≤ M for all possible choices of M and 1x . To
this end, recall the Taylor series for the exponential,
∞
X 1 1 2 1
eX = Xn = 1 + X + X + X3 + · · · .
n! 2 6
n=0
If X > 0 then
1 2 1
1 + X < 1 + X + X + X3 + · · · = eX .
2 6
Cutting out the middle and letting X = 1x · C , this becomes
1 + 1x · C < e1x·C .
Thus, N
(1 + 1x · C) N < e1x·C = e N 1x·C ≤ e LC
(1 + 1x · C) N − 1 (B + C A)
M N ,1x = < M
2C
where
(e LC − 1)(B + C A)
M = . (9.16)
2C
And this (fnally) completes our proof of theorem 9.1 on page 203.
208 Euler’s Numerical Method
Additional Exercises
9.1. Several initial-value problems are given below, along with values for two of the three
parameters in Euler’s method: step size 1x , number of steps N , and maximum variable
of interest xmax . For each, fnd the corresponding numerical solution using Euler’s
method with the indicated parameter values. Do these problems without a calculator or
computer.
dy y 1
a. = with y(1) = −1 ; 1x = and N = 3
dx x 3
dy
b. = −8x y with y(0) = 10 ; xmax = 1 and N = 4
dx
dy 1
c. 4x + = y 2 with y(0) = 2 ; xmax = 2 and 1x =
dx 2
dy y 1
d. + = 4 with y(1) = 8 ; 1x = and N = 6
dx x 2
9.2. Again, several initial-value problems are given below, along with values for two of the
three parameters in Euler’s method: step size 1x , number of steps N , and maxi-
mum variable of interest xmax . For each, fnd the corresponding numerical solution
using Euler’s method with the indicated parameter values. Do these problems with a
(nonprogramable) calculator.
dy p 1
a. = 2x + y with y(0) = 0 ; 1x = and N = 6
dx 2
dy
b. (1 + y) = x with y(0) = 1 ; N =6 and xmax = 2
dx
dy
c. = y x with y(1) = 2 ; 1x = 0.1 and xmax = 1.5
dx
dy 1
d. = cos(y) with y(0) = 0 ; 1x = and N = 5
dx 5
9.3 a. Using your favorite computer language or computer math package, write a program
or worksheet for fnding the numerical solution to an arbitrary frst-order initial-value
problem using Euler’s method. Make it easy to change the differential equation and
the computational parameters (step size, number of steps, etc.).2, 3
b. Test your program/worksheet by using it to re-compute the numerical solutions for
the problems in exercise 9.2, above.
9.4. Using your program/worksheet from exercise 9.3 a with each of the following step sizes,
fnd an approximation for y(5) where y = y(x) is the solution to
dy p
= 3 x 2 + y2 + 1 with y(0) = 0 .
dx
2 If your computer math package uses infnite precision or symbolic arithmetic, you may have to include commands
to ensure your results are given as decimal approximations.
3 It may be easier to compute all the x ’s frst, and then compute the y ’s .
k k
Additional Exercises 209
9.5. Let y be the (true) solution to the initial-value problem considered in section 9.2,
dy
5 − y 2 = −x 2 with y(0) = 1 .
dx
For each step size 1x given below, use your program/worksheet from exercise 9.3 a
to fnd an approximation to y(3) . Also, for each, fnd the magnitude of the error (to
the nearest 0.0001 ) in using the approximation for y(3) , assuming the correct value
of y(3) is −0.23699 .
1 1 1
a. 1x = 1 b. 1x = c. 1x = d. 1x =
2 4 8
e. 1x = 0.01 f. 1x = 0.001 g. 1x = 0.0001
iii. Solve the above initial-value problem exactly using methods developed in earlier
chapters. What happens to y(x) as x → ∞ ? Compare this behavior to that of
your numerical solution. In particular, what is the approximate error in using yk
for y(xk ) when xk is large?
b. Now fnd the numerical solution to the above initial-value problem using Euler’s
method with 1x = 1/10 and N being any large integer (do this by hand, looking for
patterns in the yk ’s )). Then do the following:
i. Find a relatively simple formula describing the pattern in the yk ’s .
ii. Plot the piecewise straight approximation corresponding to this numerical solution
along with a slope feld for the above differential equation. Does this numerical
solution appear to be signifcantly better (more accurate) than the one found in part
9.7 a?
9.8. In this problem we’ll see one danger of blindly applying a numerical method to solve
an initial-value problem. The initial-value problem is
dy 3
= with y(0) = 0 .
dx 7 − 3x
a. Find the numerical solution to this using Euler’s method with step size 1x = 1/2 and
xmax = 5 . (Use your program/worksheet from exercise 9.3 a).
b. Sketch the piecewise straight approximation corresponding to the numerical solution
just found.
c. Sketch the slope feld for this differential equation, and fnd the exact solution the
above initial-value problem by simple integration.
d. What happens in the true solution as x → 7/3 ?
e. What can be said about the approximations to y(xk ) obtained in the frst part when
xk > 7/3 ?