Numerical Methods: Euler's Method
Most differential equations have no formula for their solution. Numerical methods produce approximate values at a grid of points instead. The simplest is Euler's method: from the current point, follow the tangent line for a short step. This lesson covers the method and its error; the full toolbox (Runge-Kutta, adaptive steps, stiffness) is in Numerical Methods.
The method
For , and a step size :
It is the first-order Taylor polynomial , with supplied by the equation.
def euler(f, x0, y0, h, n):
x, y = x0, y0
for _ in range(n):
y = y + h * f(x, y)
x = x + h
return y
f = lambda x, y: y # y' = y, y(0) = 1, exact y(1) = e
import math
for n in (10, 100, 1000):
approx = euler(f, 0.0, 1.0, 1 / n, n)
print(f"h = {1/n:<6} y(1) = {approx:.6f} error = {math.e - approx:.6f}")h = 0.1 y(1) = 2.593742 error = 0.124539
h = 0.01 y(1) = 2.704814 error = 0.013468
h = 0.001 y(1) = 2.716924 error = 0.001358
Each tenfold reduction of cuts the error about tenfold: Euler's method is first order, its global error is roughly proportional to . Notice that for Euler computes , the classic limit that defines .
Why the error is
Each step makes a local error of about (the Taylor remainder). Reaching a fixed takes steps, so the local errors add up to a global error of order . A method whose local error is has global error and is called order .
Improving it: the midpoint and RK4 ideas
Better methods sample the slope at more than one point per step. The midpoint method takes a half Euler step, evaluates the slope there and uses that slope for the full step; it is second order. The classical Runge-Kutta method (RK4) averages four slopes and is fourth order.
def rk4_step(f, x, y, h):
k1 = f(x, y)
k2 = f(x + h / 2, y + h / 2 * k1)
k3 = f(x + h / 2, y + h / 2 * k2)
k4 = f(x + h, y + h * k3)
return y + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4)import math
def solve(step, f, h):
x, y = 0.0, 1.0
for _ in range(round(1 / h)):
y = step(f, x, y, h); x += h
return y
euler = lambda f, x, y, h: y + h * f(x, y)
mid = lambda f, x, y, h: y + h * f(x + h / 2, y + h / 2 * f(x, y))
def rk4(f, x, y, h):
k1 = f(x, y); k2 = f(x + h/2, y + h/2*k1); k3 = f(x + h/2, y + h/2*k2); k4 = f(x + h, y + h*k3)
return y + h / 6 * (k1 + 2*k2 + 2*k3 + k4)
f = lambda x, y: y
for name, step in (("Euler", euler), ("midpoint", mid), ("RK4", rk4)):
errs = [abs(math.e - solve(step, f, h)) for h in (0.1, 0.05)]
print(f"{name:8s} error h=0.1: {errs[0]:.2e} h=0.05: {errs[1]:.2e} ratio {errs[0]/errs[1]:.1f}")Euler error h=0.1: 1.25e-01 h=0.05: 6.50e-02 ratio 1.9
midpoint error h=0.1: 4.20e-03 h=0.05: 1.09e-03 ratio 3.9
RK4 error h=0.1: 2.08e-06 h=0.05: 1.36e-07 ratio 15.3
Halving divides the error by about : 2 for Euler, 4 for midpoint, 16 for RK4.
A warning: instability
For , the exact solution decays fast. Euler with computes : the "solution" oscillates and explodes. Explicit methods need small enough for stability, here . Below, happens to land exactly on zero (the factor is 0), oscillates but decays (factor ), and explodes. Equations that force tiny steps for stability are called stiff (Stiff Equations).
for h in (0.05, 0.09, 0.15):
y = 1.0
for _ in range(round(1.5 / h)):
y += h * (-20 * y)
print(f"h = {h}: y(1.5) = {y:.3e}")h = 0.05: y(1.5) = 0.000e+00
h = 0.09: y(1.5) = -2.252e-02
h = 0.15: y(1.5) = 1.024e+03
In software
position += velocity * dt; velocity += accel * dtin a game loop is Euler's method. Most engines use the "semi-implicit" variant (update velocity first, then position with the new velocity), which keeps orbits and springs from gaining energy (Game Physics).scipy.integrate.solve_ivpdefaults to an adaptive Runge-Kutta pair of orders 4 and 5.
Exercises
Exercise 1: Two steps by hand
Use Euler's method with for , to estimate .
Answer
(exact: ).
Solution
Step 1: at . Step 2: . The exact solution is .
Exercise 2: Predict the error
Euler's method gives an error of 0.08 with . Roughly what error do you expect with ? And with RK4 if its error at were 0.08?
Answer
About 0.02 for Euler; about for RK4.
Solution
shrinks by a factor of 4. A first-order method's error shrinks by 4; a fourth-order method's by .
Exercise 3: Stability limit (code)
For , Euler multiplies by each step. For , what is the largest stable ? Check and over 200 steps.
Answer
.
Solution
Stability needs , i.e. .
for h in (0.039, 0.041):
print(h, abs(1 - 50 * h) ** 200)0.039 3.50526662488287e-05
0.041 17292.580815160865
Test yourself
One Euler step for computes…
Euler's method is first order. Halving roughly…
- halves the global error
- quarters the global error
- doesn't change the error
- doubles the error
Why can Euler's method blow up on even though the true solution decays?
- Round-off error
- The step is too large for stability:
- The equation is nonlinear
- Euler's method never works for decay