Ordinary differential equations of the form
RK4 (the classical fourth-order Runge-Kutta method) solves this problem with a fixed step size, producing deterministic, constant-work-per-tick execution. It is the natural choice for hard-real-time control loops.
Dormand-Prince RK45 is an embedded pair that computes both a 4th- and a 5th-order estimate from the same seven slope evaluations, then uses their difference as a cheap local error gauge. The step-size controller shrinks the step when the estimated error is too large and grows it when the solution is smooth — adapting accuracy to computational budget without user intervention. It is suited for offline simulation or hardware-in-the-loop testing where timing determinism is less critical than accuracy.
Given
The weights
Dormand and Prince (1980) selected a 7-stage Butcher tableau whose 5th-order propagator
The 5th-order solution used to advance the state:
The 4th-order embedded solution used only for error estimation:
The mixed absolute/relative weighted RMS norm over all
A step is accepted when
clamped further to
The 7th stage
| Integrator | RHS evaluations per accepted step | State memory |
|---|---|---|
| RK4 (fixed) | 4 (always) | |
| Dormand-Prince | 6 (with FSAL), 7 on first step |
All intermediate stage vectors are stack-allocated. No heap is used. The cost of one step is
Scalar decay
| Stage | Formula | Value |
|---|---|---|
Exact:
-
Stiff systems. Explicit RK methods are unstable for stiff problems when
$h|\lambda| \gtrsim 2.8$ (RK4 stability boundary for a scalar complex eigenvalue). A stiff plant requires either an implicit integrator or a very small step size. -
Step-size floor. When DP45 shrinks the step below
$h_{\min}$ , the step is clamped and forced accepted regardless of error — useful to avoid infinite rejection loops on a discontinuity, but the solution at that point is degraded. -
Zero or negative step. Guard
$h > 0$ before callingStep; a zero step produces an unchanged state but wastes evaluations. -
FSAL invalidation. After a rejected step, the cached
$k_7$ is discarded, and the next attempt recomputes$k_1$ from scratch (7 evaluations instead of 6). - Float precision. The Dormand-Prince Butcher coefficients have denominators up to 339200; in single precision the accumulated rounding can erode one to two digits of accuracy compared with double. Use tighter tolerances or shorter integration windows.
| Variant | Key Difference |
|---|---|
| Euler (1st order) | One stage; |
| RK4 (this) | Four stages; |
| Dormand-Prince (this) | Seven stages; |
| Bogacki-Shampine RK23 | Three-stage embedded pair; lower overhead for mildly stiff or smooth problems |
| Adams-Bashforth | Multi-step; reuses past evaluations; efficient but requires startup phase |
| Implicit RK / SDIRK | Solves a nonlinear system at each stage; suitable for stiff problems at the cost of a linear solve per step |
- Dynamics propagation — advance plant models (
dynamics/) forward in time for prediction horizons in MPC or trajectory planning. - Model-based state estimation — propagate the process model in an extended Kalman filter between measurement updates.
- Hardware-in-the-loop simulation — embed a physics model on the device for closed-loop testing without external simulation hardware.
- Trajectory generation — integrate a kinematic model to produce smooth, time-parameterized reference trajectories.
graph LR
RK["RK4 / Dormand-Prince"]
DYN["dynamics/ (Euler-Lagrange, Newton-Euler)"]
EKF["Extended Kalman Filter"]
MPC["MPC Controller"]
C2D["ContinuousToDiscrete"]
DYN --> RK
RK --> EKF
RK --> MPC
C2D -.->|"exact linear alternative"| RK
| Algorithm | Relationship |
|---|---|
dynamics/ models |
Provide the right-hand side |
| Extended Kalman Filter | Uses RK to propagate the state prediction step between measurements |
| MPC Controller | Uses RK to simulate the plant over a prediction horizon |
| ContinuousToDiscrete | Exact matrix-exponential discretization — an alternative for linear, time-invariant systems |
- Dormand, J.R. and Prince, P.J., "A family of embedded Runge-Kutta formulae," Journal of Computational and Applied Mathematics, 6(1):19–26, 1980.
- Hairer, E., Nørsett, S.P., and Wanner, G., Solving Ordinary Differential Equations I: Nonstiff Problems, 2nd ed., Springer, 1993 — Chapters II.4–II.6.
- Press, W.H. et al., Numerical Recipes in C++, 3rd ed., Cambridge University Press, 2007 — Section 17.2.