Time stepping with Euler's method
Turn a rate-of-change equation into a simulation with Euler's method, measure its error against an exact answer, and extend it to systems with several state variables.
Lesson 2 of 6 · 30 min · Self-paced · Free
Rates of change
Most physical laws are statements about rates of change. A capacitor discharges at a rate proportional to its voltage. A falling object's velocity changes at a rate set by gravity and drag. A robot joint's angle changes at a rate equal to its angular velocity. An equation that gives the rate of change of a quantity is called a differential equation, written like this:
dy/dt = f(t, y)Read it as: "the rate at which y changes is some function of the time and of y itself." Sometimes you can solve such an equation exactly with calculus, but most real engineering equations, especially for robots with several moving parts, have no neat exact solution. Simulation lets you solve them numerically, one small step at a time.
Euler's method
The simplest method is named after the mathematician Leonhard Euler. If you know y now and how fast it is changing now, assume it keeps changing at that rate for a short time dt:
y_next = y_now + f(t_now, y_now) · dt
t_next = t_now + dtGeometrically, you follow the tangent line (the slope at the start of the step) for a distance dt, then recompute the slope and repeat. The true curve bends away from the tangent during the step, so every step makes a small error. Smaller steps, smaller errors.
A worked example: a discharging capacitor
When a capacitor discharges through a resistor, its voltage V obeys
dV/dt = −V / (RC)where RC is the time constant. Take RC = 2 s and a starting voltage of 5 V. The exact solution, from calculus, is V(t) = 5 · e^(−t/2), which gives V(4) = 5 · e^(−2) ≈ 0.6767 V. Having an exact answer lets us measure Euler's error.
Two steps by hand, with dt = 0.5 s:
Step 1: slope = −5 / 2 = −2.5 V/s
V = 5 + (−2.5)(0.5) = 3.75 V at t = 0.5 s
Step 2: slope = −3.75 / 2 = −1.875 V/s
V = 3.75 + (−1.875)(0.5) = 2.8125 V at t = 1.0 sThe exact voltage at t = 0.5 s is 5 · e^(−0.25) ≈ 3.894 V, so Euler is already slightly low. That makes sense: the voltage falls fastest at the start of each step, and Euler uses that steepest slope for the whole step.
In Python, it is worth writing Euler's method as a general function that accepts any rate function f:
import math
RC = 2.0 # time constant, seconds
V0 = 5.0 # starting voltage, volts
def dVdt(t, V):
return -V / RC
def euler(f, y0, t_end, dt):
"""Step dy/dt = f(t, y) from t = 0 to t_end with Euler's method."""
steps = round(t_end / dt)
t, y = 0.0, y0
for _ in range(steps):
y = y + f(t, y) * dt
t = t + dt
return y
exact = V0 * math.exp(-4.0 / RC)
print(f"exact V(4) = {exact:.4f} V")
for dt in [0.5, 0.25, 0.125, 0.0625]:
v = euler(dVdt, V0, 4.0, dt)
print(f"dt = {dt:<6} V(4) = {v:.4f} V error = {v - exact:+.4f} V")Output:
exact V(4) = 0.6767 V
dt = 0.5 V(4) = 0.5006 V error = -0.1761 V
dt = 0.25 V(4) = 0.5903 V error = -0.0863 V
dt = 0.125 V(4) = 0.6339 V error = -0.0427 V
dt = 0.0625 V(4) = 0.6554 V error = -0.0213 VTwo details in the code are worth copying. Passing the function dVdt as an argument means the same euler works for any equation. And computing the number of steps with round(t_end / dt) avoids a floating-point trap: adding 0.1 to itself repeatedly never lands exactly on a round number, so a loop like while t < t_end can take one step too many.
How fast does the error shrink?
Look at the error column: each time dt halves, the error roughly halves. Euler's method is called first order: the error at a fixed end time is roughly proportional to dt. That is useful to know, but also a limitation. To get one more correct decimal place, you need ten times as many steps. More advanced methods, such as the Runge–Kutta family, have errors proportional to dt² or dt⁴, so they gain accuracy much faster as dt shrinks. Euler remains the right place to start, because every other method builds on the same idea.
Systems with more than one state variable
A moving object needs at least two state variables: position and velocity. The rules form a system: the rate of change of position is the velocity, and the rate of change of velocity is the acceleration. Euler's method applies to each variable in the same step:
G = 9.81 # m/s²
def derivatives(state):
"""Rates of change for a dropped object: (dy/dt, dv/dt)."""
y, v = state # distance fallen (m), downward speed (m/s)
return (v, G)
def euler_step(state, dt):
rates = derivatives(state)
return tuple(s + r * dt for s, r in zip(state, rates))
state = (0.0, 0.0)
dt = 0.1
for _ in range(20): # 20 steps of 0.1 s = 2.0 s
state = euler_step(state, dt)
y, v = state
print(f"Euler: fallen {y:.3f} m, speed {v:.3f} m/s")
print(f"Exact: fallen {0.5 * G * 2.0**2:.3f} m, speed {G * 2.0:.3f} m/s")Output:
Euler: fallen 18.639 m, speed 19.620 m/s
Exact: fallen 19.620 m, speed 19.620 m/sThe speed is exactly right, because acceleration is constant, so its tangent line is the true line. The distance is too small by 0.981 m. Each step moves the object using the speed it had at the start of the step, but the object is speeding up throughout the step. For constant acceleration you can show this error is g · t · dt / 2 = 9.81 × 2.0 × 0.1 / 2 = 0.981 m, exactly what we see.
This pattern, a derivatives function that returns the rate of change of every state variable and a general step function, is how larger simulations are organised. To simulate a different system you change only derivatives.
Recap
- A differential equation dy/dt = f(t, y) describes how a quantity changes.
- Euler's method: y_next = y_now + f(t_now, y_now) · dt. It follows the tangent line for one step.
- Euler's method is first order: halving dt roughly halves the error.
- Compute the number of steps with
round(t_end / dt)rather than accumulating floats until a target. - For several state variables, compute all the rates from the current state, then update every variable.
Project
Euler by hand and by machine
Work with a pencil, paper, a calculator and Python.
- By hand, apply Euler's method to the capacitor equation dV/dt = −V / 2, starting from V = 5 V, with dt = 1 s, for 4 steps. Write a table of t, slope and V.
- Compute the exact values 5 · e^(−t/2) for t = 1, 2, 3, 4 and add them to your table.
- Check your hand table with the
eulerfunction from this lesson. - Use
euleron a new equation of your choice, for example Newton's cooling dT/dt = −0.1 (T − 20) from T = 90 °C, and show that halving dt roughly halves the error at t = 10 min. The exact solution is T = 20 + 70 · e^(−0.1t).
Deliverables
- A hand-worked table with Euler and exact values
- Python output confirming the hand table
- A table of error against dt for your second equation, with one sentence on the pattern
Knowledge check
Check your understanding
Go further
- Euler method — Wikipedia ↗
The geometric picture, error analysis and a worked example of Euler's method.
Lesson materials · free to download
Sign in to track your progress. Reading is free without an account.