DifferentialEquations
Chapter 8 · Section 8.4

8.4 Numerical Methods I: Euler's Method

Graphical methods give pictures, and series give good approximations near the initial point. Engineers usually need something else: reliable numbers for the solution over a whole interval, for any reasonable . Numerical methods provide exactly that. They do not produce a formula; they produce a table of approximate values at chosen points.

This section develops the simplest numerical method, due to Euler. It is too crude for serious work, but every better method (Sections 8.5 to 8.7) is built on the same ideas, and Euler's method is where those ideas, especially the different kinds of error, are easiest to see.

The setting

We want to approximate the solution of

on an interval . We assume that this problem has a unique solution there (Section 8.3 says when). Choose a step size and the equally spaced grid points

A numerical method produces numbers with . We keep the two symbols apart: is the true value, which we usually do not know, and is what we compute.

Deriving Euler's method

We give three short derivations. They lead to the same formula, and each one teaches something different.

1. Follow the tangent line

At the starting point we know the slope of the solution exactly: . Over a short step, the curve stays close to its tangent line, so we walk along the tangent:

At we do not know the true solution any more, but the direction field still gives the slope of the solution curve through this point. So we take another tangent step from there, and so on. The result is a broken line, the Euler polygon, built from line elements of the direction field (Figure 8.4.1).

2. Cut off a Taylor series

If is continuous, Taylor's theorem with remainder gives, for some between and ,

Since , dropping the last term and replacing by gives Euler's formula. This view tells us the size of what we threw away: a term proportional to . We will use it below.

3. Approximate an integral

Integrating the equation over one step, as in Section 8.3,

Approximating the integral by a rectangle of width and height equal to the integrand at the left end, , gives Euler's formula once more. Better ways of approximating this integral (a trapezoid, Simpson's rule) lead to the better methods of the next sections.

Definition 8.4.1 (Euler's method)

With step size and , Euler's method for , computes

Figure 8.4.1. Two Euler steps (drawn with a large to make the errors visible). After the first step we are on a different solution curve (dashed). The local error is what one step adds, measured from the solution through the current point; the global error is the total distance from the true solution.
Method in a nutshell: Euler's method
  1. Choose and the number of steps (so that is the end of the interval).
  2. Start with , .
  3. Compute the slope .
  4. Update and . Record .
  5. Repeat steps 3 and 4 times. Keep 6 or more significant digits in intermediate values.

Euler's method at work

We use the equation whose direction field we sketched in Section 8.1. It has the exact solution , so we can measure the errors.

Example 8.4.1 (A first table)

Use Euler's method with to approximate the solution of , on . Compare with the exact values.

Solution
  1. The formula. Here , so
  2. First step. , so at .
  3. Second step. , so at .
  4. Continue. The full table (6 decimals; error = exact − Euler):
    exact error
    00.01.000000−1.0000001.0000000.000000
    10.20.800000−0.7600000.8212690.021269
    20.40.648000−0.4880000.6896800.041680
    30.60.550400−0.1904000.6111880.060788
    40.80.5123200.1276800.5906710.078351
    51.00.5378560.4621440.6321210.094265
  5. Observations. The error grows from step to step. At it is , which is of the true value. Every Euler value lies below the exact one. The reason is concavity: all the points involved lie above the inflection curve of Example 8.1.3, where solution curves are concave up, and a tangent line of a concave-up curve lies below the curve. Each step therefore lands a little below the solution curve it started on.
Example 8.4.2 (Halving the step)

Repeat Example 8.4.1 with . How does the error at change?

Solution
  1. The formula. , now with 10 steps. The first step gives .
  2. Table.
    exacterror
    0.10.9000000.9051630.005163
    0.20.8110000.8212690.010269
    0.30.7339000.7491820.015282
    0.40.6695100.6896800.020170
    0.50.6185590.6434690.024910
    0.60.5817030.6111880.029485
    0.70.5595330.5934150.033882
    0.80.5525800.5906710.038092
    0.90.5613220.6034300.042109
    1.00.5861890.6321210.045931
  3. Compare. At the error dropped from to , a factor of ; at , from to . Halving roughly halves the error, at the price of twice as many steps. Figure 8.4.2 shows both polygons.
Figure 8.4.2. Euler polygons for , with and , continued to . Both lag below the concave-up exact solution; the gap is roughly halved when is halved.

Use the calculator below to run Euler's method yourself. It lists every intermediate quantity and, when you give the exact solution, the error at each step. Start with the preset (Example 8.4.2), then try with and check that the error at is about halved again.

Nothing in the method requires linearity. Here is a nonlinear problem from engineering.

Example 8.4.3 (A draining tank)

By Torricelli's law, the water depth (in m) in a tank emptying through a hole in its bottom satisfies, for a certain tank, with ( in minutes). Use Euler's method with to estimate the depth for . Compare with the exact solution .

Solution
  1. Exact solution (check). Separating, , so , i.e. and . It is valid until the tank is empty at min.
  2. Euler steps. . For example, and .
  3. Table.
    exacterror
    0.04.000000−1.2000004.0000000.000000
    0.53.400000−1.1063453.4225000.022500
    1.02.846827−1.0123532.8900000.043173
    1.52.340651−0.9179512.4025000.061849
    2.01.881675−0.8230451.9600000.078325
  4. Check by halving. With (8 steps) we get , error : again about half of .

How large is the error?

A numerical answer without an idea of its error is of little use. Three kinds of error are at work.

  • Truncation error: the error caused by the method itself, because it replaces the curve by straight pieces (the Taylor terms we dropped). It is present even with perfect arithmetic.
  • Round-off error: the error caused by doing arithmetic with finitely many digits.
  • Errors in the data ( or coefficients of known only approximately). We do not study these here.

Sizes of errors are reported in two ways. The error at is (its absolute value is the absolute error). The percentage relative error is

which compares the error with the size of the quantity itself. (It is meaningless when is zero or nearly so.) In Example 8.4.1 the percentage relative error at is ; with it is .

Local truncation error

Definition 8.4.2 (Local truncation error)

The local truncation error of Euler's method at step is the error made in a single step that starts from the exact value:

By the Taylor formula of the second derivation, . So if on the interval,

We say the local truncation error is of order , written : halving divides it by about .

Example 8.4.4 (Measuring the local error)

For , , compute the error of the very first Euler step for and , and compare with .

Solution
  1. Why the first step? The first step starts from the exact value , so its error is exactly the local truncation error .
  2. . and , so .
  3. . and , so .
  4. Compare. The ratio is : halving divided the local error by about . With we have , and equals and , very close to the measured values.

Global truncation error

The error we actually care about is the global error , the total distance from the truth after steps. It is built from two ingredients: each step adds a new local error, and each step also carries forward the error accumulated so far.

A rough count already shows the answer. To cross an interval of length we take steps, each adding an error of size about . Adding them up gives about . So the global error should be of order , one power lower than the local error. The following theorem makes this precise.

Theorem 8.4.1 (Global error of Euler's method)

Suppose is continuous on the strip , , and satisfies there a Lipschitz condition with . Suppose the solution of , has a continuous second derivative on with . Then the Euler approximations with step satisfy In particular, the global error is : Euler's method is a first-order method.

Why it works

Subtract the Euler formula from the exact relation : By the Lipschitz condition the bracket is at most in size, and . Hence The old error is amplified by at most per step, and a new piece of size is added. Starting from and unrolling this inequality gives a geometric sum: using and .

Example 8.4.5 (The error bound and the halving table)

For , : (a) evaluate the bound of Theorem 8.4.1 at for ; (b) tabulate the actual error at as is halved repeatedly, starting from .

Solution
  1. Constants. , so on the whole strip. Also is positive and increasing on , so .
  2. (a) Bound. The actual error is . The bound is about three times too large, but it is guaranteed, and it shows the right dependence on .
  3. (b) Halving table. (Exact value .)
    stepserrorratio to previous error
    0.250.5378560.094265–
    0.1100.5861890.0459312.05
    0.05200.6094380.0226822.02
    0.025400.6208480.0112722.01
    0.0125800.6265010.0056192.01
    0.006251600.6293150.0028052.00
    0.0031253200.6307190.0014022.00
    0.00156256400.6314200.0007012.00
  4. Reading the table. The ratios approach , and error settles near (the limit is ). So error at . To guarantee three correct decimals (error below ) we need , that is, 500 steps. Accuracy is expensive with a first-order method: each extra correct digit costs ten times more work.

Round-off error

Every arithmetic operation on a computer or calculator rounds its result to a fixed number of digits. Each Euler step therefore adds a small rounding error, say of size up to . Over steps these can add up to roughly , which grows as shrinks. A simple model of the total error is

a decreasing truncation part plus an increasing round-off part. Such a function has a minimum at : making smaller than makes the answer worse. The model is a worst case (rounding errors of different steps partly cancel), but its shape is real.

To see this, we repeated the computation of Example 8.4.5 while rounding to 4 decimal places after every step, as one might on a hand calculator:

steps absolute error, full precisionabsolute error, 4 decimals kept
640.0156250.0070290.006721
2560.0039060.0017520.001921
10240.0009770.0004380.000321
40960.0002440.0001090.013179
163840.0000610.0000270.197779

Down to about both columns agree in size. Beyond that, the rounded computation falls apart: with , every increment smaller than in size is rounded away completely, so the computed stops changing wherever . Figure 8.4.3 shows the two behaviours on log–log axes.

Figure 8.4.3. Error at against (log–log; decreases to the right). In full double precision the points lie on a line of slope : error , the signature of a first-order method. Keeping only 4 decimals per step, the error stops improving near and then grows as round-off takes over.
What this means in practice

Computers work with about 16 significant digits, so for Euler's method round-off becomes visible only for extremely small , long after the run time has become absurd. The practical lesson is different: reducing is an expensive and, eventually, futile way to buy accuracy. The efficient cure is a method whose error falls faster than , such as the improved Euler method (, Section 8.5) or the Runge–Kutta method (, Section 8.6).

A warning: when the step is too large

Error estimates like Theorem 8.4.1 describe what happens as . With a step that is too large, something worse than inaccuracy can happen.

Example 8.4.6 (Instability)

Apply Euler's method to , (exact solution , a fast decay) with and with . For which do the Euler values decay like the true solution?

Solution
  1. The Euler formula. , so
  2. . The factor is : , a decay like the true solution (which has at ).
  3. . The factor is : . The values oscillate and grow, while the true solution is practically zero (Figure 8.4.4).
  4. Condition. The Euler values decay exactly when , that is, . For they decay but alternate in sign; for they decay without oscillation.
Figure 8.4.4. , . With Euler's method decays like the exact solution; with the computed values oscillate with growing amplitude. This is numerical instability, not just inaccuracy.

Equations containing fast-decaying components (large negative ) appear in circuit and chemical-reaction models; they are called stiff. For them, the step size of Euler's method is limited by stability rather than by accuracy. When a numerical solution oscillates wildly where the direction field says it should not, suspect instability and reduce .

Summary

  • Euler's method follows tangent lines from grid point to grid point.
  • Its local truncation error is ; its global error is . Halving halves the error and doubles the work.
  • Round-off error grows as shrinks, so there is a smallest useful step; and a step that is too large can make the computation unstable.

Euler's method uses the slope only at the beginning of each step. Section 8.5 shows how averaging two slopes gives a method whose error falls like , at almost no extra cost.

Exercises

Give numerical answers to 6 decimal places.

  1. Use Euler's method with to approximate for , . Show that and compare with the exact value .
    Answer ; exact ; error (Euler overestimates here).
  2. For , , take three Euler steps with . Compare with the exact solution at .
    Answer , , ; exact ; error .
  3. For , , use to approximate at . Compare with the exact value .
    Answer ; exact .
  4. For , , use to approximate . The exact solution is .
    Answer ; exact ; error .
  5. For , , use to approximate and compare with the exact solution .
    Answer , , ; exact ; Euler is too high by .
  6. A tank drains according to , (depth in m, in min). Take three Euler steps with and compare with the exact solution .
    Answer ; exact .
  7. For , , show that the local truncation error of the first step is , and that it is approximately for small . Evaluate both for .
    Answer and . Taylor: . For : versus .
  8. Show that Euler's method for , gives . If we reach a fixed in steps (), show that as .
    Answer . Then (a standard limit).
  9. Euler's method with gives an error of at some point. Predict the error at the same point with . About what is needed for an error of ?
    Answer Error : about for ; for .
  10. If the error at behaves like , then should cancel most of it (here is the Euler value with step ). Test this with the unrounded values and from Examples 8.4.1 and 8.4.2.
    Answer ; error (a slight overshoot), about 19 times smaller in size than the error of . (This trick is called Richardson extrapolation.)
  11. Suppose the total error of a computation is modelled by . Find the step size that minimizes and the minimum error.
    Answer gives ; .
  12. For , for which step sizes do the Euler values decay to zero? For which do they decay without changing sign?
    Answer . Decay iff , i.e. ; no sign change iff , i.e. .