Concept units

Euler's method: small steps along a field

Intermediate

Read these first if they are new to you

The idea in one paragraph

Euler's method finds where a particle goes in a velocity field. Read the arrow where you stand, take a short straight step along it, and repeat from the new point. Each step uses the arrow at the start of the step, so on a curved path each step drifts a little off. Smaller steps drift less, but they cost more, because every step reads the field once. When the paths are straight lines traveled at a steady speed, even one step lands exactly.

Each step moves along the arrow where you stand

Choose a short time h, the step size. Start at position x0 at time t0. Each step reads the arrow at the current position and time, and moves along it for time h:

xk+1 = xk + h × v(xk, tk),   tk+1 = tk + h.

The rule is "distance = velocity × time", applied as if the arrow stayed the same for the whole step. Over a short step the arrow barely changes, so the guess is close. To cover a time span T in n steps, use h = T / n.

Worked example: a decaying number

Take the one-dimensional field v(x) = −x: the arrow always points back to 0, and it is longer when x is farther away. Start at x = 1 and go to time 1. The true path is x(t) = e−t, because its slope at every time is −e−t = −x, which is exactly the arrow (see exponentials and the chain rule). So the true answer is e−1 ≈ 0.3679.

  1. With n = 2, h = 0.5. Step 1: the arrow at 1 is −1, so x = 1 + 0.5 × (−1) = 0.5.
  2. Step 2: the arrow at 0.5 is −0.5, so x = 0.5 + 0.5 × (−0.5) = 0.25.
  3. The error is 0.3679 − 0.25 = 0.1179.
steps nstep size hEuler's x(1)error
20.500.25000.1179
40.250.31640.0515
100.100.34870.0192
1000.010.36600.0018

From h = 0.1 to h = 0.01, the step shrinks 10 times and the error shrinks about 10 times. The error is roughly proportional to the step size. That is why Euler's method is called a first-order method.

Your turn. The field v(x, t) = 2t depends only on the time. Start at x = 0 at t = 0 and take n = 2 steps to t = 1. The true answer is x(1) = 1, because x = t2. What does Euler's method give?

Answer

Step 1 reads the arrow at t = 0, which is 0, so x stays 0. Step 2 reads it at t = 0.5, which is 1, so x = 0 + 0.5 × 1 = 0.5. The error is 0.5. With 4 steps the arrows are 0, 0.5, 1 and 1.5, so x = 0.25 × 3 = 0.75, and the error halves to 0.25.

euler.py

import numpy as np

def euler(v, x, t0, t1, n):
    # Follow the field v from time t0 to t1 in n equal steps.
    h = (t1 - t0) / n
    t = t0
    for i in range(n):
        x = x + h * v(x, t)            # move along the arrow where you stand
        t = t + h
    return x

def decay(x, t):                       # the true path is x(t) = exp(-t)
    return -x

exact = np.exp(-1.0)
for n in [2, 4, 10, 100]:
    x = euler(decay, 1.0, 0.0, 1.0, n)
    print(f"n={n:3}  h={1 / n:.2f}  x(1)={x:.4f}  error={np.abs(x - exact):.4f}")
n=  2  h=0.50  x(1)=0.2500  error=0.1179
n=  4  h=0.25  x(1)=0.3164  error=0.0515
n= 10  h=0.10  x(1)=0.3487  error=0.0192
n=100  h=0.01  x(1)=0.3660  error=0.0018

On a curved path each step drifts outward; smaller steps drift less

In the swirl field v(x, y) = (−y, x), a particle that starts at (1, 0) travels around a circle. After a time of π/2 it has made a quarter turn and reached (0, 1). Euler's method steps along the arrow, which points along the circle's edge at the start of each step. Each step therefore leaves the circle a little toward the outside, and the next arrow out there is longer.

Euler steps (blue) against the true path (dashed) over a quarter turn

…

Try this. Find the fewest steps that bring the error below 0.1. Then go from 10 steps to 20, and from 20 to 40: what happens to the error each time? Last, choose Straight paths, where the field is the same arrow (−1, 1) everywhere, and set n to 1.
What you should find

On the curved path, 13 steps are the fewest that bring the error below 0.1 (it is 0.099). Doubling the steps roughly halves the error: 0.130 at 10 steps, 0.064 at 20, and 0.031 at 40. On the straight path, the arrow is the same all along the path, so a single step lands exactly on the end point.

Each step costs one reading of the field

Every step reads the field once. When the field is computed by a neural network, one reading is one run of the network. So n steps cost n network runs, and halving the error costs about twice as many runs.

This is why straight paths are valuable. Generative models such as flow matching follow a learned field from noise to an example with Euler steps. The straighter its paths, the fewer steps it needs for a good result.

Better methods exist

The midpoint method reads the field twice per step and combines the two arrows. The classic fourth-order Runge–Kutta method reads it four times. Their errors shrink like h2 and h4, much faster than Euler's. Both belong to the Runge–Kutta family, whose simplest member is Euler's method. Some methods also choose the step size as they go.

Check yourself

  1. Use the field v(x) = −x, start at x = 2, and take two steps with h = 0.5. Where do you end?

    Answer

    Step 1: 2 + 0.5 × (−2) = 1. Step 2: 1 + 0.5 × (−1) = 0.5. The true value at time 1 is 2e−1 ≈ 0.736, so the error is about 0.24.

  2. With 10 steps the error is 0.02. About what error do you expect with 100 steps?

    Answer

    About 0.002. Euler's method is first order, so ten times smaller steps give about ten times smaller error.

  3. A generator takes 40 Euler steps, and each run of its network takes 5 milliseconds. How long does one batch take? Why would straighter paths help?

    Answer

    40 × 5 = 200 milliseconds. Straighter paths let fewer steps reach the same accuracy, so each batch takes less time.

Go further

Optional background, for readers with more time.

Where this shows up

Units that build on this one