Numerical Methods

Euler, Runge-Kutta, and adaptive step methods

From Continuous to Discrete

Most differential equations cannot be solved exactly. Numerical methods approximate solutions by discretizing the continuous problem: instead of finding y(t) for all t, we compute y at a finite set of points t_0, t_1, t_2, ... separated by a step size h. At each step, we use the ODE itself to estimate where the solution goes next.

The simplest approach is Euler's method: follow the tangent line for a small step. More sophisticated methods like Runge-Kutta evaluate the slope at multiple points within each step to achieve dramatically better accuracy. Understanding these methods is essential for scientific computing, engineering simulation, and computational physics.

Euler's Method: Step by Step

Euler's method is the foundation of all numerical ODE solvers. Given dy/dx = f(x,y) with y(x_0) = y_0, each step computes:

y_{n+1} = y_n + h * f(x_n, y_n)

The cyan curve shows the Euler approximation, while the dashed white curve shows the exact solution. Red bars indicate the error at each step. Try different step sizes to see how accuracy improves as h decreases.

Euler's method approximates the solution by following the tangent line at each point. Smaller step sizes produce more accurate results. Red bars show the cumulative error.

Key insight: Euler's method has a local truncation error of O(h^2) per step, but errors accumulate over many steps, giving a global error of O(h). Halving the step size roughly halves the total error -- but doubles the number of steps required.

Method Comparison: Euler vs. Heun vs. RK4

Higher-order methods evaluate the slope at multiple points within each step to achieve far better accuracy. The improved Euler (Heun's) method averages the slopes at the start and end of each step (2nd order). The celebrated Runge-Kutta 4 (RK4) method takes a weighted average of four slope evaluations (4th order).

Methody(3)ErrorOrder
Exact0.00012341------
Euler0.000038358.506e-5O(h)
Heun (Improved Euler)0.008746788.623e-3O(h^2)
RK40.000279791.564e-4O(h^4)

Higher-order methods achieve much better accuracy for the same step size. RK4 (4th order) is dramatically more accurate than Euler (1st order), especially with larger steps.

Key insight: The order of accuracy determines how fast the error shrinks with step size. For an order-p method, halving h reduces the error by a factor of 2^p. RK4 (order 4) gains 16x accuracy per halving, making it the workhorse of scientific computing.

Stiffness: When Explicit Methods Fail

A stiff equation contains dynamics at vastly different time scales. The ODE below has a fast transient that decays at rate 1000, while the solution tracks sin(t) which varies on a scale of order 1:

dy/dt = -1000(y - sin(t)) + cos(t)

Explicit methods like forward Euler must use extremely tiny steps to remain stable (h < 2/1000 = 0.002). Implicit methods like backward Euler solve an equation at each step and remain stable with much larger steps.

Panel 1: Forward Euler (large h)
When h > 2/lambda, the explicit method oscillates and explodes. The solution is completely wrong.
Panel 2: Forward Euler (tiny h)
With h small enough, forward Euler is stable but needs thousands of steps. This is computationally expensive.
Panel 3: Backward Euler (large h)
The implicit method remains stable even with large steps. It tracks the exact solution without exploding.

Stiff equations have components that decay at vastly different rates. Explicit methods like forward Euler require tiny steps (h < 2/lambda) to avoid instability, while implicit methods like backward Euler remain stable at any step size.

Key insight: Stiffness is not about the complexity of the solution -- it is about the stability constraint on step size. Stiff problems force explicit methods into impractically small steps. Implicit methods sacrifice some per-step simplicity for unconditional stability, making them essential for stiff systems in chemistry, circuit simulation, and computational biology.

Key Takeaways

  • Discretization -- numerical methods replace the continuous ODE with a sequence of algebraic steps, trading exactness for computability
  • Local vs. global error -- the error introduced per step (local) accumulates over many steps into the total error (global); for Euler's method, local error is O(h^2) while global error is O(h)
  • Order of accuracy -- higher-order methods like RK4 achieve dramatically better accuracy for the same step size, with error scaling as O(h^p) for a p-th order method
  • Stiffness and stability -- stiff equations require implicit methods (like backward Euler) that remain stable at large step sizes; explicit methods become unstable unless the step size is impractically small