2.7: Euler's Method
( \newcommand{\kernel}{\mathrm{null}\,}\)
If an initial value problem
y′=f(x,y),y(x0)=y0
cannot be solved analytically, it is necessary to resort to numerical methods to obtain useful approximations to a solution of Equation ???. we will consider such methods in this chapter.
We’re interested in computing approximate values of the solution of Equation ??? at equally spaced points x0, x1, …, xn=b in an interval [x0,b]. Thus,
xi=x0+ih,i=0,1,…,n,
where
h=b−x0n.
we will denote the approximate values of the solution at these points by y0, y1, …, yn; thus, yi is an approximation to y(xi). we will call
ei=y(xi)−yi
the error at the ith step. Because of the initial condition y(x0)=y0, we will always have e0=0. However, in general ei≠0 if i>0.
We encounter two sources of error in applying a numerical method to solve an initial value problem:
- The formulas defining the method are based on some sort of approximation. Errors due to the inaccuracy of the approximation are called truncation errors.
- Computers do arithmetic with a fixed number of digits, and therefore make errors in evaluating the formulas defining the numerical methods. Errors due to the computer’s inability to do exact arithmetic are called roundoff errors.
Since a careful analysis of roundoff error is beyond the scope of this book, we will consider only truncation errors.
Euler’s Method
The simplest numerical method for solving Equation ??? is Euler’s method. This method is so crude that it is seldom used in practice; however, its simplicity makes it useful for illustrative purposes. Euler’s method is based on the assumption that the tangent line to the integral curve of Equation ??? at (xi,y(xi)) approximates the integral curve over the interval [xi,xi+1]. Since the slope of the integral curve of Equation ??? at (xi,y(xi)) is y′(xi)=f(xi,y(xi)), the equation of the tangent line to the integral curve at (xi,y(xi)) is
y=y(xi)+f(xi,y(xi))(x−xi).
Setting x=xi+1=xi+h in Equation ??? yields
yi+1=y(xi)+hf(xi,y(xi))
as an approximation to y(xi+1). Since y(x0)=y0 is known, we can use Equation ??? with i=0 to compute
y1=y0+hf(x0,y0).
However, setting i=1 in Equation ??? yields
y2=y(x1)+hf(x1,y(x1)),
which isn’t useful, since we don’t know y(x1). Therefore we replace y(x1) by its approximate value y1 and redefine
y2=y1+hf(x1,y1).
Having computed y2, we can compute
y3=y2+hf(x2,y2).
In general, Euler’s method starts with the known value y(x0)=y0 and computes y1, y2, …, yn successively by with the formula
yi+1=yi+hf(xi,yi),0≤i≤n−1.
The next example illustrates the computational procedure indicated in Euler’s method.
Example 2.7.1
Use Euler’s method with h=0.1 to find approximate values for the solution of the initial value problem
y′+2y=x3e−2x,y(0)=1
at x=0.1,0.2,0.3.
Solution
We rewrite Equation ??? as
y′=−2y+x3e−2x,y(0)=1,
which is of the form Equation ???, with
f(x,y)=−2y+x3e−2x,x0=0,andy0=1.
Euler’s method yields
y1=y0+hf(x0,y0)=1+(0.1)f(0,1)=1+(0.1)(−2)=0.8,y2=y1+hf(x1,y1)=0.8+(0.1)f(0.1,0.8)=0.8+(0.1)(−2(0.8)+(0.1)3e−0.2)=0.640081873,y3=y2+hf(x2,y2)=0.640081873+(0.1)(−2(0.640081873)+(0.2)3e−0.4)=0.512601754.
We’ve written the details of these computations to ensure that you understand the procedure. However, in the rest of the examples as well as the exercises in this chapter, we will assume that you can use a programmable calculator or a computer to carry out the necessary computations.
Examples Illustrating The Error in Euler’s Method
Example 2.7.2
Use Euler’s method with step sizes h=0.1, h=0.05, and h=0.025 to find approximate values of the solution of the initial value problem
y′+2y=x3e−2x,y(0)=1
at x=0, 0.1, 0.2, 0.3, …, 1.0. Compare these approximate values with the values of the exact solution
y=e−2x4(x4+4),
which can be obtained by the method of Section 2.1. (Verify.)
Table 2.7.1 shows the values of the exact solution Equation ??? at the specified points, and the approximate values of the solution at these points obtained by Euler’s method with step sizes h=0.1, h=0.05, and h=0.025. In examining this table, keep in mind that the approximate values in the column corresponding to h=0.05 are actually the results of 20 steps with Euler’s method. We haven’t listed the estimates of the solution obtained for x=0.05, 0.15, …, since there’s nothing to compare them with in the column corresponding to h=0.1. Similarly, the approximate values in the column corresponding to h=0.025 are actually the results of 40 steps with Euler’s method.
x | h=0.1 | h=0.05 | h=0.025 | Exact |
---|---|---|---|---|
0.0 | 1.000000000 | 1.000000000 | 1.000000000 | 1.000000000 |
0.1 | 0.800000000 | 0.810005655 | 0.814518349 | 0.818751221 |
0.2 | 0.640081873 | 0.656266437 | 0.663635953 | 0.670588174 |
0.3 | 0.512601754 | 0.532290981 | 0.541339495 | 0.549922980 |
0.4 | 0.411563195 | 0.432887056 | 0.442774766 | 0.452204669 |
0.5 | 0.332126261 | 0.353785015 | 0.363915597 | 0.373627557 |
0.6 | 0.270299502 | 0.291404256 | 0.301359885 | 0.310952904 |
0.7 | 0.222745397 | 0.242707257 | 0.252202935 | 0.261398947 |
0.8 | 0.186654593 | 0.205105754 | 0.213956311 | 0.222570721 |
0.9 | 0.159660776 | 0.176396883 | 0.184492463 | 0.192412038 |
1.0 | 0.139778910 | 0.154715925 | 0.162003293 | 0.169169104 |
Table 2.7.1: Numerical solution of y′+2y=x3e−2x, y(0)=1, by Euler’s method.
You can see from Table 2.7.1 that decreasing the step size improves the accuracy of Euler’s method. For example,
yexact(1)−yapprox(1)≈{0.0293withh=0.1,0.0144 with h=0.05,0.0071 with h=0.025.
Based on this scanty evidence, you might guess that the error in approximating the exact solution at a fixed value of x by Euler’s method is roughly halved when the step size is halved. You can find more evidence to support this conjecture by examining Table 2.7.2, which lists the approximate values of yexact−yapprox at x=0.1, 0.2, …, 1.0.
x | h=0.1 | h=0.05 | h=0.0.25 |
---|---|---|---|
0.1 | 0.0187 | 0.0087 | 0.0042 |
0.2 | 0.0305 | 0.0143 | 0.0069 |
0.3 | 0.0373 | 0.0176 | 0.0085 |
0.4 | 0.0406 | 0.0193 | 0.0094 |
0.5 | 0.0415 | 0.0198 | 0.0097 |
0.6 | 0.0406 | 0.0195 | 0.0095 |
0.7 | 0.0386 | 0.0186 | 0.0091 |
0.8 | 0.0359 | 0.0174 | 0.0086 |
0.9 | 0.0327 | 0.0160 | 0.0079 |
1.0 | 0.0293 | 0.0144 | 0.0071 |
Table 2.7.2: Errors in approximate solutions of y′+2y=x3e−2x, y(0)=1, obtained by Euler’s method.
Example 2.7.3
Tables 2.7.2 and 2.7.4 show analogous results for the nonlinear initial value problem
y′=−2y2+xy+x2, y(0)=1,
except in this case we cannot solve Equation ??? exactly. The results in the “Exact” column were obtained by using a more accurate numerical method known as the Runge-Kutta method with a small step size. They are exact to eight decimal places.
x | h=0.1 | h=0.05 | h=0.025 | Exact |
---|---|---|---|---|
0.0 | 1.000000000 | 1.000000000 | 1.000000000 | 1.000000000 |
0.1 | 0.800000000 | 0.821375000 | 0.829977007 | 0.837584494 |
0.2 | 0.681000000 | 0.707795377 | 0.719226253 | 0.729641890 |
0.3 | 0.605867800 | 0.633776590 | 0.646115227 | 0.657580377 |
0.4 | 0.559628676 | 0.587454526 | 0.600045701 | 0.611901791 |
0.5 | 0.535376972 | 0.562906169 | 0.575556391 | 0.587575491 |
0.6 | 0.529820120 | 0.557143535 | 0.569824171 | 0.581942225 |
0.7 | 0.541467455 | 0.568716935 | 0.581435423 | 0.593629526 |
0.8 | 0.569732776 | 0.596951988 | 0.609684903 | 0.621907458 |
0.9 | 0.614392311 | 0.641457729 | 0.654110862 | 0.666250842 |
1.0 | 0.675192037 | 0.701764495 | 0.714151626 | 0.726015790 |
Table 2.7.3: Numerical solution of y′=−2y2+xy+x2, y(0)=1, by Euler’s method.
Since we think it is important in evaluating the accuracy of the numerical methods that we will be studying in this chapter, we often include a column listing values of the exact solution of the initial value problem, even if the directions in the example or exercise don’t specifically call for it. If quotation marks are included in the heading, the values were obtained by applying the Runge-Kutta method in a way that’s explained in Section 3.3. If quotation marks are not included, the values were obtained from a known formula for the solution. In either case, the values are exact to eight places to the right of the decimal point.
x | h=0.1 | h=0.05 | h=0.025 |
---|---|---|---|
0.1 | 0.0376 | 0.0162 | 0.0076 |
0.2 | 0.0486 | 0.0218 | 0.0104 |
0.3 | 0.0517 | 0.0238 | 0.0115 |
0.4 | 0.0523 | 0.0244 | 0.0119 |
0.5 | 0.0522 | 0.0247 | 0.0121 |
0.6 | 0.0521 | 0.0248 | 0.0121 |
0.7 | 0.0522 | 0.0249 | 0.0122 |
0.8 | 0.0522 | 0.0250 | 0.0122 |
0.9 | 0.0519 | 0.0248 | 0.0121 |
1.0 | 0.0508 | 0.0243 | 0.0119 |
Table 2.7.4: Errors in approximate solutions of y′=−2y2+xy+x2, y(0)=1, obtained by Euler’s method.
Truncation Error in Euler’s Method
Consistent with the results indicated in Tables 2.7.1 - 2.7.4, we will now show that under reasonable assumptions on f there’s a constant K such that the error in approximating the solution of the initial value problem
y′=f(x,y),y(x0)=y0,
at a given point b>x0 by Euler’s method with step size h=(b−x0)/n satisfies the inequality
|y(b)−yn|≤Kh,
where K is a constant independent of n.
There are two sources of error (not counting roundoff) in Euler’s method:
- The error committed in approximating the integral curve by the tangent line Equation ??? over the interval [xi,xi+1].
- The error committed in replacing y(xi) by yi in Equation ??? and using Equation ??? rather than Equation ??? to compute yi+1.
Euler’s method assumes that yi+1 defined in Equation ??? is an approximation to y(xi+1). We call the error in this approximation the local truncation error at the ith step, and denote it by Ti; thus,
Ti=y(xi+1)−y(xi)−hf(xi,y(xi)).
we will now use Taylor’s theorem to estimate Ti, assuming for simplicity that f, fx, and fy are continuous and bounded for all (x,y). Then y″ exists and is bounded on [x0,b]. To see this, we differentiate
y′(x)=f(x,y(x))
to obtain
y″(x)=fx(x,y(x))+fy(x,y(x))y′(x)=fx(x,y(x))+fy(x,y(x))f(x,y(x)).
Since we assumed that f, fx and fy are bounded, there’s a constant M such that
|fx(x,y(x))+fy(x,y(x))y′(x)|≤Mx0<x<b
which implies that
|y″(x)|≤M,x0<x<b
Since xi+1=xi+h, Taylor’s theorem implies that
y(xi+1)=y(xi)+hy′(xi)+h22y″(˜xi),
where ˜xi is some number between xi and xi+1. Since y′(xi)=f(xi,y(xi)) this can be written as
y(xi+1)=y(xi)+hf(xi,y(xi))+h22y″(˜xi),
or, equivalently,
y(xi+1)−y(xi)−hf(xi,y(xi))=h22y″(˜xi).
Comparing this with Equation ??? shows that
Ti=h22y″(˜xi).
Recalling Equation ???, we can establish the bound
|Ti|≤Mh22,1≤i≤n.
Although it may be difficult to determine the constant M, what is important is that there’s an M such that Equation ??? holds. We say that the local truncation error of Euler’s method is of order h2, which we write as O(h2).
Note that the magnitude of the local truncation error in Euler’s method is determined by the second derivative y″ of the solution of the initial value problem. Therefore the local truncation error will be larger where |y″| is large, or smaller where |y″| is small.
Since the local truncation error for Euler’s method is O(h2), it is reasonable to expect that halving h reduces the local truncation error by a factor of 4. This is true, but halving the step size also requires twice as many steps to approximate the solution at a given point. To analyze the overall effect of truncation error in Euler’s method, it is useful to derive an equation relating the errors
ei+1=y(xi+1)−yi+1andei=y(xi)−yi.
To this end, recall that
y(xi+1)=y(xi)+hf(xi,y(xi))+Ti
and
yi+1=yi+hf(xi,yi).
Subtracting Equation ??? from Equation ??? yields
ei+1=ei+h[f(xi,y(xi))−f(xi,yi)]+Ti.
The last term on the right is the local truncation error at the ith step. The other terms reflect the way errors made at previous steps affect ei+1. Since |Ti|≤Mh2/2, we see from Equation ??? that
|ei+1|≤|ei|+h|f(xi,y(xi))−f(xi,yi)|+Mh22.
Since we assumed that fy is continuous and bounded, the mean value theorem implies that
f(xi,y(xi))−f(xi,yi)=fy(xi,y∗i)(y(xi)−yi)=fy(xi,y∗i)ei,
where y∗i is between yi and y(xi). Therefore
|f(xi,y(xi))−f(xi,yi)|≤R|ei|
for some constant R. From this and Equation ???,
|ei+1|≤(1+Rh)|ei|+Mh22,0≤i≤n−1.
For convenience, let C=1+Rh. Since e0=y(x0)−y0=0, applying Equation ??? repeatedly yields
|e1|≤Mh22|e2|≤C|e1|+Mh22≤(1+C)Mh22|e3|≤C|e2|+Mh22≤(1+C+C2)Mh22⋮|en|≤C|en−1|+Mh22≤(1+C+⋯+Cn−1)Mh22.
Recalling the formula for the sum of a geometric series, we see that
1+C+⋯+Cn−1=1−Cn1−C=(1+Rh)n−1Rh
(since C=1+Rh). From this and Equation 2.7.40,
|y(b)−yn|=|en|≤(1+Rh)n−1RMh2.
Since Taylor’s theorem implies that
1+Rh<erh
(verify),
(1+Rh)n<enRh=eR(b−x0)(since nh=b−x0).
This and Equation ??? imply that
|y(b)−yn|≤Kh,
with
K=MeR(b−x0)−12R.
Because of Equation ??? we say that the global truncation error of Euler’s method is of order h, which we write as O(h).