TExcelTAD
Home › Numerical Methods › Euler's Method and Convergence

Euler's Method

Euler's method follows the tangent line for a short step, then repeats. It is the simplest IVP solver, rarely used for serious computation, but every concept needed for better methods (local and global error, order of accuracy, stability) shows up in it clearly. A first introduction with slope fields is in Euler's Method; here we analyse it.

The method

With step size hh and ti=a+iht_i = a + ih:

ui+1=ui+h f(ti,ui).u_{i+1} = u_i + h\,f(t_i, u_i).
python · try it yourself
import numpy as np

def euler(f, a, b, u0, n):
    h = (b - a) / n
    t = np.linspace(a, b, n + 1)
    u = np.zeros((n + 1,) + np.shape(u0)); u[0] = u0
    for i in range(n):
        u[i + 1] = u[i] + h * f(t[i], u[i])
    return t, u

f = lambda t, u: np.sin((t + u) ** 2)              # no formula for the solution
t, u = euler(f, 0, 4, -1.0, 400)
print(f"u(4) with n = 400: {u[-1]:.6f}")
from scipy.integrate import solve_ivp
ref = solve_ivp(f, (0, 4), [-1.0], rtol=1e-12, atol=1e-12).y[0, -1]
print(f"reference        : {ref:.6f}")
u(4) with n = 400: -1.880224
reference        : -1.880751

Local and global error

The local truncation error is what one step commits when started from the exact solution. By Taylor's theorem, u(t+h)=u(t)+hu′(t)+h22u′′(ξ)u(t + h) = u(t) + hu'(t) + \frac{h^2}{2}u''(\xi), so Euler's step misses by O(h2)O(h^2). Over b−ah\frac{b - a}{h} steps these errors accumulate (and propagate) into a global error of O(h)O(h): Euler's method is first order.

In general, a one-step method with local error O(hp+1)O(h^{p+1}) has global error O(hp)O(h^p), provided ff is Lipschitz (the propagation factor is at most eL(b−a)e^{L(b - a)}).

python · try it yourself
import numpy as np

f = lambda t, u: -2 * t * u                    # exact: u = exp(-t^2)
exact = np.exp(-4.0)
prev = None
for n in (50, 100, 200, 400, 800):
    h = 2 / n; u = 1.0
    for i in range(n):
        u += h * f(i * h, u)
    err = abs(u - exact)
    print(f"n = {n:3d}: error {err:.3e}" + ("" if prev is None else f", ratio {prev / err:.2f}"))
    prev = err
n =  50: error 2.474e-03
n = 100: error 1.229e-03, ratio 2.01
n = 200: error 6.125e-04, ratio 2.01
n = 400: error 3.058e-04, ratio 2.00
n = 800: error 1.528e-04, ratio 2.00

Halving hh halves the error: first order. To get 6 more digits, Euler needs a million times more steps; that is why higher-order methods exist.

Absolute stability

For the test equation u′=λuu' = \lambda u with Re⁡λ<0\operatorname{Re}\lambda < 0, the exact solution decays. Euler gives ui+1=(1+hλ)uiu_{i+1} = (1 + h\lambda)u_i, which decays only if ∣1+hλ∣<1\lvert 1 + h\lambda\rvert < 1. For real negative λ\lambda:

h<2∣λ∣.h < \frac{2}{\lvert\lambda\rvert}.

Larger steps produce growing oscillations, however accurate the method would otherwise be.

python · try it yourself
lam = -50.0
for h in (0.01, 0.039, 0.041, 0.05):
    u = 1.0
    for _ in range(100):
        u *= 1 + h * lam
    print(f"h = {h}: factor per step {1 + h * lam:+.2f}, u after 100 steps = {u:.3e}")
h = 0.01: factor per step +0.50, u after 100 steps = 7.889e-31
h = 0.039: factor per step -0.95, u after 100 steps = 5.921e-03
h = 0.041: factor per step -1.05, u after 100 steps = 1.315e+02
h = 0.05: factor per step -1.50, u after 100 steps = 4.066e+17

This limit, harmless for slowly varying problems, becomes the main obstacle for stiff problems (Stiff Equations).

In software

  • No library uses plain Euler for accuracy, but it appears everywhere as a building block: as the predictor in predictor-corrector schemes, in particle systems and simple game physics, and as gradient descent (Differential Equations in Software).
  • Stochastic differential equations are commonly simulated with the Euler-Maruyama method.

Exercises

Exercise 1: By hand

Take two Euler steps with h=0.5h = 0.5 for u′=t−uu' = t - u, u(0)=1u(0) = 1.

Answer

u(0.5)≈0.5u(0.5) \approx 0.5, u(1)≈0.5u(1) \approx 0.5.

Solution

u1=1+0.5(0−1)=0.5u_1 = 1 + 0.5(0 - 1) = 0.5; u2=0.5+0.5(0.5−0.5)=0.5u_2 = 0.5 + 0.5(0.5 - 0.5) = 0.5. (Exact: u(1)=2e−1≈0.7358u(1) = 2e^{-1} \approx 0.7358.)

Exercise 2: Stability limit

What is the largest stable Euler step for u′=−200uu' = -200u?

Answer

h<0.01h < 0.01.

Solution

h<2200h < \frac{2}{200}.

Exercise 3: Steps for a target accuracy (code)

Euler's error for the lesson's problem is about ChC h. Estimate CC from n=100n = 100 and predict the nn needed for error 10−510^{-5}; check it.

Answer

C≈0.06C \approx 0.06, so about 12,30012{,}300 steps; the check gives an error of 0.99×10−50.99 \times 10^{-5}.

Solution
python · try it yourself
import numpy as np
f = lambda t, u: -2 * t * u
def run(n):
    h = 2 / n; u = 1.0
    for i in range(n):
        u += h * f(i * h, u)
    return abs(u - np.exp(-4.0))
C = run(100) / (2 / 100)
n = int(np.ceil(C * 2 / 1e-5))
print(f"C = {C:.4f}, predicted n = {n}, error with that n = {run(n):.2e}")
C = 0.0615, predicted n = 12292, error with that n = 9.93e-06

Test yourself

Euler's method has global error…

  1. O(h2)O(h^2)
  2. O(h)O(h)
  3. O(h4)O(h^4)
  4. O(1)O(1)

For u′=λuu' = \lambda u with real λ<0\lambda < 0, Euler is stable when…

  1. h<2/∣λ∣h < 2/\lvert\lambda\rvert
  2. always
  3. h>2/∣λ∣h > 2/\lvert\lambda\rvert
  4. h<∣λ∣h < \lvert\lambda\rvert

The local truncation error of a method is…

  1. the total error at the end
  2. the error of one step started from the exact solution
  3. the rounding error
  4. the step size