Skip to main content
An ordinary differential equation (ODE) specifies the rate of change of a quantity. The derivative and an initial condition determine the quantity itself. The simplest form is dydt=f(y,t),y(0)=y0.\frac{d y}{d t} = f(y, t), \qquad y(0) = y_0. At each time tt, the function ff gives the slope of yy at the current state (y,t)(y, t). Solving the ODE means finding the function y(t)y(t) that satisfies this relation.

Exponential growth

Consider the ODE dydt=y,y(0)=1.\frac{d y}{d t} = y, \qquad y(0) = 1. The rate of change of yy equals yy itself. This law describes populations, compound interest, and unstable physical systems in some regimes. The exponential has this property and satisfies the initial condition, so y(t)=et.y(t) = e^t. This is the analytical solution. Most ODEs encountered in practice have no analytical solution, so you must compute y(t)y(t) numerically. The simplest numerical method is the Euler method.

The Euler method

The Euler method approximates the solution by taking repeated short steps in the direction of the derivative. Choose a small step size Δt\Delta t. Starting from (t0,y0)=(0,1)(t_0, y_0) = (0, 1), replace the continuous ODE with the discrete update yn+1=yn+Δt⋅f(yn,tn),tn+1=tn+Δt.y_{n+1} = y_n + \Delta t \cdot f(y_n, t_n), \qquad t_{n+1} = t_n + \Delta t. At each step, evaluate the slope at the current point and move a distance Δt\Delta t in that direction. After NN steps, the result approximates yy at time t=NΔtt = N \Delta t. The following Python function implements this update.
Output from cell 2 Euler undershoots the exact curve. With Δt=0.2\Delta t = 0.2, each step uses the slope at the start of the interval. The true slope grows over the interval, so the discrete update consistently lags behind. The next experiment shows how shrinking Δt\Delta t changes the error and how quickly the error approaches zero as Δt→0\Delta t \to 0.

Step size and error

The Euler method is first-order accurate. The global error at a fixed final time scales linearly with Δt\Delta t. Halving Δt\Delta t roughly halves the error. You can verify this by running Euler at several step sizes and plotting the error at t=2t = 2 on a log-log axis. A first-order method produces a straight line with slope 1.
Output from cell 4 The data points lie on the reference line with slope 11, as expected for a first-order accurate method. Higher-order methods such as Heun (slope 22) and RK4 (slope 44) give more accuracy per step. Like Euler, they advance the solution using the derivative. The Euler step is sufficient for the examples that follow. Better integrators solve the same ODE with greater accuracy.

Rotation in two dimensions

An ODE can also describe a vector state x∈Rd\mathbf{x} \in \mathbb{R}^d. Its right-hand side is then a vector-valued function v(x,t)∈Rd\mathbf{v}(\mathbf{x}, t) \in \mathbb{R}^d: dxdt=v(x,t),x(0)=x0.\frac{d \mathbf{x}}{d t} = \mathbf{v}(\mathbf{x}, t), \qquad \mathbf{x}(0) = \mathbf{x}_0. The vector field v\mathbf{v} assigns a velocity to every point in space. A solution follows this velocity. The Euler update has the same form: xn+1=xn+Δt⋅v(xn,tn).\mathbf{x}_{n+1} = \mathbf{x}_n + \Delta t \cdot \mathbf{v}(\mathbf{x}_n, t_n). Consider the two-dimensional vector field for pure rotation: v(x,y)=[−yx].\mathbf{v}(x, y) = \begin{bmatrix} -y \\ x \end{bmatrix}. Each particle at position (x,y)(x, y) has a velocity perpendicular to its position vector. Its exact trajectory is a circle around the origin, traced counterclockwise.
Output from cell 6 The black ✕ marks show the position of each particle after t=2πt = 2\pi. The Euler endpoints drift slightly outward because of the same first-order error seen in one dimension. Shrinking Δt\Delta t moves the endpoints toward their starting circles. A higher-order solver reduces the drift faster than decreasing Δt\Delta t with Euler.

Generative models

The same construction appears in modern generative models. Replace the hand-written rotation field with a learned vector field vθ(x,t)\mathbf{v}_\theta(\mathbf{x}, t) parameterized by a neural network. Initialize the particles with Gaussian noise. Then apply the same Euler loop.
  • If vθ\mathbf{v}_\theta is trained to match a target velocity field that connects Gaussian noise to the data distribution, the result is flow matching.
  • If vθ\mathbf{v}_\theta encodes the score of a noise-perturbed data distribution, the result is a diffusion model in its probability-flow ODE form.
Both models use an ODE with a learned vector field. The vector field vθ\mathbf{v}_\theta, the probability path, and the regression target used to train the network still have to be chosen.

References

  • L. C. Evans, Partial Differential Equations (Chapter 1), for ODE basics and existence/uniqueness.
  • E. Hairer, S. P. Nørsett, G. Wanner, Solving Ordinary Differential Equations I, the standard reference on Euler and its successors.
  • Khan Academy, Differential equations, for an introductory video course.