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 and :
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, , so Euler's step misses by . Over steps these errors accumulate (and propagate) into a global error of : Euler's method is first order.
In general, a one-step method with local error has global error , provided is Lipschitz (the propagation factor is at most ).
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 = errn = 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 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 with , the exact solution decays. Euler gives , which decays only if . For real negative :
Larger steps produce growing oscillations, however accurate the method would otherwise be.
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 for , .
Answer
, .
Solution
; . (Exact: .)
Exercise 2: Stability limit
What is the largest stable Euler step for ?
Answer
.
Solution
.
Exercise 3: Steps for a target accuracy (code)
Euler's error for the lesson's problem is about . Estimate from and predict the needed for error ; check it.
Answer
, so about steps; the check gives an error of .
Solution
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…
For with real , Euler is stable when…
- always
The local truncation error of a method is…
- the total error at the end
- the error of one step started from the exact solution
- the rounding error
- the step size