TExcelTAD
Home › Differential Equations › Numerical Methods: Euler's Method

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 y′=f(x,y)y' = f(x, y), y(x0)=y0y(x_0) = y_0 and a step size hh:

xk+1=xk+h,yk+1=yk+h f(xk,yk).x_{k+1} = x_k + h, \qquad y_{k+1} = y_k + h\,f(x_k, y_k).

It is the first-order Taylor polynomial y(x+h)≈y(x)+hy′(x)y(x + h) \approx y(x) + hy'(x), with y′y' supplied by the equation.

python · try it yourself
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 hh cuts the error about tenfold: Euler's method is first order, its global error is roughly proportional to hh. Notice that for y′=yy' = y Euler computes (1+h)1/h(1 + h)^{1/h}, the classic limit that defines ee.

Why the error is O(h)O(h)

Each step makes a local error of about 12h2y′′\frac{1}{2}h^2y'' (the Taylor remainder). Reaching a fixed xx takes (x−x0)/h(x - x_0)/h steps, so the local errors add up to a global error of order h2⋅1h=hh^2 \cdot \frac{1}{h} = h. A method whose local error is O(hp+1)O(h^{p+1}) has global error O(hp)O(h^p) and is called order pp.

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.

python
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)
python · try it yourself
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 hh divides the error by about 2p2^p: 2 for Euler, 4 for midpoint, 16 for RK4.

A warning: instability

For y′=−20yy' = -20y, y(0)=1y(0) = 1 the exact solution decays fast. Euler with h=0.15h = 0.15 computes yk+1=(1−20h)yk=−2yky_{k+1} = (1 - 20h)y_k = -2y_k: the "solution" oscillates and explodes. Explicit methods need hh small enough for stability, here h<0.1h < 0.1. Below, h=0.05h = 0.05 happens to land exactly on zero (the factor 1−20h1 - 20h is 0), h=0.09h = 0.09 oscillates but decays (factor −0.8-0.8), and h=0.15h = 0.15 explodes. Equations that force tiny steps for stability are called stiff (Stiff Equations).

python · try it yourself
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 * dt in 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_ivp defaults to an adaptive Runge-Kutta pair of orders 4 and 5.

Exercises

Exercise 1: Two steps by hand

Use Euler's method with h=0.5h = 0.5 for y′=x+yy' = x + y, y(0)=1y(0) = 1 to estimate y(1)y(1).

Answer

y(1)≈2.5y(1) \approx 2.5 (exact: 2e−2≈3.4372e - 2 \approx 3.437).

Solution

Step 1: y1=1+0.5(0+1)=1.5y_1 = 1 + 0.5(0 + 1) = 1.5 at x=0.5x = 0.5. Step 2: y2=1.5+0.5(0.5+1.5)=2.5y_2 = 1.5 + 0.5(0.5 + 1.5) = 2.5. The exact solution is y=2ex−x−1y = 2e^x - x - 1.

Exercise 2: Predict the error

Euler's method gives an error of 0.08 with h=0.02h = 0.02. Roughly what error do you expect with h=0.005h = 0.005? And with RK4 if its error at h=0.02h = 0.02 were 0.08?

Answer

About 0.02 for Euler; about 0.08/256≈0.00030.08/256 \approx 0.0003 for RK4.

Solution

hh shrinks by a factor of 4. A first-order method's error shrinks by 4; a fourth-order method's by 44=2564^4 = 256.

Exercise 3: Stability limit (code)

For y′=−λyy' = -\lambda y, Euler multiplies yy by 1−hλ1 - h\lambda each step. For λ=50\lambda = 50, what is the largest stable hh? Check h=0.039h = 0.039 and h=0.041h = 0.041 over 200 steps.

Answer

h<2/λ=0.04h < 2/\lambda = 0.04.

Solution

Stability needs ∣1−50h∣<1\lvert 1 - 50h\rvert < 1, i.e. 0<h<0.040 < h < 0.04.

python · try it yourself
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 y′=f(x,y)y' = f(x, y) computes…

  1. yk+1=yk+h f(xk,yk)y_{k+1} = y_k + h\,f(x_k, y_k)
  2. yk+1=f(xk,yk)y_{k+1} = f(x_k, y_k)
  3. yk+1=yk+h2fy_{k+1} = y_k + h^2 f
  4. yk+1=yk/hy_{k+1} = y_k / h

Euler's method is first order. Halving hh roughly…

  1. halves the global error
  2. quarters the global error
  3. doesn't change the error
  4. doubles the error

Why can Euler's method blow up on y′=−20yy' = -20y even though the true solution decays?

  1. Round-off error
  2. The step is too large for stability: ∣1−20h∣>1\lvert 1 - 20h\rvert > 1
  3. The equation is nonlinear
  4. Euler's method never works for decay