Skip to content
Numerical Calculus

Differential equations on a computer: Euler, Runge-Kutta and stiff problems

Euler's method and its error, Heun and fourth-order Runge-Kutta, adaptive step, stability and stiff problems, implicit methods in SPICE and symplectic methods.

The differential equations you can solve with pen and paper are few: linear ones with constant coefficients, separable ones. A circuit with a diode, a pendulum with large swings, a motor with nonlinear friction, the orbit of a satellite around the oblate Earth all need numerical methods. The idea is simple: start from the initial condition and advance in small time steps, following the direction field. In this lesson we cover Euler’s and the Runge-Kutta methods, order of accuracy and the problem of stability.

The problem

    \[ y' = f(t, y),\qquad y(t_0) = y_0 \]

where y can also be a vector: every higher-order equation can be rewritten as a first-order system. The pendulum \theta'' = -\frac gl\sin\theta becomes \theta' = \omega, \omega' = -\frac gl\sin\theta. Choose the times t_n = t_0 + nh, with h the step, and compute approximations y_n \approx y(t_n).

The explicit Euler method

Follow the tangent for one step:

    \[ y_{n+1} = y_n + h\,f(t_n, y_n) \]

It is the definition of the derivative read backwards. Each step makes a local error proportional to h^2 (the neglected Taylor term); after N = (T - t_0)/h steps the global error is proportional to h. Euler is a first-order method: halving the step halves the error.

Exact solution of y' equals y compared with Euler's method at two step sizes
For y' = y, y(0) = 1, the exact solution is e^t (blue). Euler with step h_1 = 0.5 (red) and h_2 = 0.25 (orange) stays below the true curve, because the tangent of a convex function lies below it; halving the step roughly halves the error.

Example. With y' = y Euler gives y_{n+1} = (1 + h)y_n, that is y_N = (1 + h)^N. To reach t = 1: with h = 0.1, 1.1^{10} = 2.594 instead of e = 2.718 (4.6% error); with h = 0.01, 1.01^{100} = 2.705 (0.5% error). It is the definition of e as the limit of (1 + 1/n)^n.

Runge-Kutta methods

To get more accuracy without shrinking the step too much, evaluate the slope at several points within the step and take a weighted average. Heun’s method (second-order Runge-Kutta) takes a trial Euler step, computes the slope at the end point and uses the average of the two slopes:

    \[ k_1 = f(t_n, y_n),\quad k_2 = f(t_n + h, y_n + hk_1),\quad y_{n+1} = y_n + \frac h2(k_1 + k_2) \]

The most used method is fourth-order Runge-Kutta (RK4):

    \[ \begin{aligned} k_1 &= f(t_n, y_n) & k_2 &= f\!\left(t_n + \tfrac h2, y_n + \tfrac h2k_1\right)\\ k_3 &= f\!\left(t_n + \tfrac h2, y_n + \tfrac h2k_2\right) & k_4 &= f(t_n + h, y_n + hk_3) \end{aligned} \]

    \[ y_{n+1} = y_n + \frac h6\big(k_1 + 2k_2 + 2k_3 + k_4\big) \]

Four evaluations of f per step, global error proportional to h^4: halving the step cuts the error by sixteen. For y' = y with h = 0.1 up to t = 1, RK4 gives 2.7182797 against e = 2.7182818: an error of 2 millionths, against Euler’s 4.6% with the same step. The weights 1, 2, 2, 1 have the same structure as Simpson’s rule: for f depending only on t, RK4 is Simpson.

Adaptive step. Modern solvers (MATLAB’s ode45, SciPy’s solve_ivp) compute two estimates of different order at each step, use their difference as an error estimate and adjust the step: large where the solution is quiet, small where it changes quickly.

Stability and stiff problems

Apply Euler to y' = -\lambda y with \lambda \gt 0, whose solution e^{-\lambda t} decays. Euler gives y_{n+1} = (1 - h\lambda)y_n. If |1 - h\lambda| \gt 1, that is if

    \[ h \gt \frac{2}{\lambda} \]

the computed values grow and oscillate in sign, while the true solution goes to zero. The method is unstable. It is not a matter of accuracy: even when the solution is almost zero after a while and does not need to be followed in detail, the step must stay small.

A problem is stiff when it contains very different time constants. A circuit with one time constant of 1 ns and another of 1 ms, simulated for a few milliseconds, would force an explicit method to take steps below 2 ns throughout: millions of steps, even after the fast transient has died away.

The answer is implicit methods, such as implicit Euler:

    \[ y_{n+1} = y_n + h\,f(t_{n+1}, y_{n+1}) \]

where the slope is evaluated at the end point. For y' = -\lambda y it gives y_{n+1} = \frac{y_n}{1 + h\lambda}, which decays for any step. The price is that at each step an equation must be solved (or a nonlinear system, with Newton). SPICE uses exactly implicit methods, the implicit trapezoidal rule and Gear’s formulas, because electronic circuits are almost always stiff. When you change the integration method from “trap” to “gear” in a simulation’s options, you are choosing between two implicit methods with different stability properties.

Conservation of energy

For frictionless oscillating systems, such as an ideal LC circuit or an orbit, explicit Euler makes the energy grow at every turn and the trajectory spirals outwards; implicit Euler makes it shrink. There are methods, called symplectic (such as the Verlet method, used in molecular dynamics simulations and in video game physics engines), that conserve energy for very long times even with large steps.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x