10.2 ODE solvers: Euler, midpoint, RK4
An ODE with initial condition has a unique solution (for reasonable — see Foundations 5.4). Numerically integrating it means walking forward from in small steps of size , producing a sequence of approximate values at times . The recipe for going from to is what distinguishes the methods.
This lesson covers the three solvers that span the practical landscape of explicit ODE integrators: forward Euler (first-order, simplest possible), midpoint (second-order, modestly more work), and Runge–Kutta 4 (fourth-order, the workhorse of practical computation). RK4 is the method behind every ODE-integration interactive on this site.
Forward Euler
The simplest possible recipe: approximate the derivative by its current value, treat it as constant over the step:
This is exactly the forward-difference approximation from 10.1, rearranged. By the Taylor analysis there, the local truncation error per step is ; after steps the accumulated global error is . Euler is a first-order method.
Euler is cheap (one evaluation of per step), trivially easy to code, and almost never the right choice. The reason is stability rather than accuracy. For the test equation with (exponential decay), the Euler update is . The numerical solution decays only if , i.e. . Beyond that threshold the numerical solution blows up, even though the true solution decays gracefully. For stiff problems (large ) the stability bound forces tiny step sizes regardless of how accurate you actually need.
Midpoint (RK2)
Midpoint trades one extra -evaluation per step for an order of accuracy. The recipe:
The idea: is the slope at the start of the step; using it, you take a half-step to find a better-informed slope at the midpoint; that slope is the one you use for the full step. The local truncation error is and the global error is . Second-order method, costing twice what Euler costs per step.
Runge–Kutta 4
The workhorse of explicit ODE integration. Four function evaluations per step, fourth-order accuracy globally:
The slopes are evaluated at the start, twice at the midpoint (each using the previous slope), and once at the end. The four slopes are combined in a weighted average with weights . The motivation is to make the Taylor expansion of the update match the true expansion of the solution to fourth order. The resulting method has local error and global error .
Going from second-order to fourth-order at a cost of 4× the function evaluations rather than 2× sounds expensive, but the gain is enormous: for the same accuracy, RK4 can use a much larger step size. On reasonable problems RK4 with outperforms Euler with .
Worked example
▶ Worked example: one Euler and one RK4 step, by hand
The problem. Take one step of size for the ODE from initial condition . The true solution is , so .
Euler step.
Error: .
RK4 step. With (autonomous; doesn’t depend on ):
Error: .
Comparison. One RK4 step (4 function evaluations) gives error . The same accuracy with Euler would need step size , so Euler steps. RK4 is more than more efficient on this problem.
Side-by-side comparison
Three solvers integrate the harmonic-oscillator system (ẋ, v̇) = (v, −x) from the initial condition (x, v) = (1, 0). The true trajectory is the unit circle (dashed). Each method takes the same number of steps with the same step size h: Euler spirals visibly outward at every step (it is consistently *adding* energy), midpoint drifts much less because its O(h²) error is smaller, and RK4 has O(h⁴) error and tracks the true circle even at h = 0.6. Crank h up and the disparity becomes dramatic: at h ≈ 0.4 Euler is already noticeably wrong, while RK4 is essentially exact.
Three solvers integrate the harmonic-oscillator system from . The true trajectory is the unit circle (energy conserved). Euler spirals outward at every step — it consistently adds energy to the system. Midpoint drifts much more slowly. RK4 tracks the circle essentially perfectly, even at large step sizes. Crank up to see the disparity become dramatic.
Stability
Stability is the second axis of solver quality, distinct from accuracy. A solver is stable for a given ODE and step size if numerical solutions stay bounded when the true solution does. For the test equation (linear, scalar, possibly complex ), each method has a stability region in the complex plane of :
- Forward Euler is stable for — a disc of radius 1 centred at . Practically, requires .
- RK4 is stable in a larger region that includes more of the negative real axis: stable for on the real axis.
- Implicit methods (backward Euler, trapezoidal rule) have unbounded stability regions — they are stable for any on . The cost is solving a nonlinear equation at each step rather than just evaluating .
The implication: for stiff systems — those with widely-separated time scales — explicit methods like RK4 are forced into tiny step sizes by the fastest time scale, even when you only care about the slow dynamics. Stiff problems demand implicit solvers, which is where the heavy machinery of numerical-ODE-software libraries lives (LSODA, SUNDIALS, etc.).
What we use this for
RK4 (sometimes its adaptive-step cousin Cash–Karp or Dormand–Prince) is the integrator behind every ODE simulation on this site:
- DirectionField (MATH-30) in Foundations 5.1 — RK4 for the trajectories that thread through the slope field.
- FixedPointGallery (MATH-29) in Foundations 5.4 — RK4 for the phase-portrait trajectories around each equilibrium type.
- DampedDrivenOscillator (MATH-9) — RK4 for the time-domain response curves.
The next lesson, 10.3, takes the same finite-difference machinery from 10.1 into spatial dimensions and discretises the wave and heat equations.