mathematics//numerical methods//numerical integration
Numerical integration is the computation of a differential equation's solution by advancing the state in finite steps of size \(\Delta t\), each step estimating where the dynamics \(\dot x=f(x,u)\) will carry the state next, and it is what every simulator, every state estimator predicting between measurements and every autopilot propagating its attitude from the IMU actually runs. Two choices define it, the step and the method, and the right pair depends on how fast the system is and on where the code will run.
Numerical integration is the computation of a differential equation's solution by advancing the state in finite steps of size Δt\Delta tΔt, each step estimating where the dynamics x˙=f(x,u)\dot x=f(x,u)x˙=f(x,u) will carry the state next, and it is what every simulator, every state estimator predicting between measurements and every autopilot propagating its attitude from the IMU actually runs. Two choices define it, the step and the method, and the right pair depends on how fast the system is and on where the code will run.
Methods differ in how they sample the slope inside a step. The Euler method uses the slope at the start and follows it in a straight line; the Runge-Kutta method samples it four times inside the step and averages. What a method buys is its order of accuracy, how the accumulated error shrinks with the step: Euler is order 1 (half the step, half the error), RK4 is order 4 (half the step, one sixteenth of the error), at four times the work per step. Adaptive step size solvers (the RK45 behind SciPy's default) estimate their own error and shrink or stretch the step to meet a tolerance, which is efficient offline and unusable in a hardware-in-the-loop rig, because a method that may need more substeps cannot promise to finish on time (hardware-in-the-loop).
Explicit Euler+632 % Semi-implicit Euler+5.26 % RK4−2.8·10⁻⁴ % ω·Δt0.10 A spring-mass oscillator integrated for 20 s with Δt = 0.100 s (ω·Δt = 0.100). Largest energy drift: explicit Euler +632 %, semi-implicit Euler +5.26 %, RK4 −2.8·10⁻⁴ %. Unstable: explicit Euler.
At 0.1 s explicit Euler spirals out of the circle while the semi-implicit version stays bounded and RK4 hardly leaves it; switch to the stiff system and push the step past 0.04 s, where explicit Euler blows up, then past 0.056 s, where RK4 follows, while implicit Euler stays calm.
Integrating is choosing a step and a method, and numerical stability matters as much as accuracy.
A method can be accurate on paper and still explode on a system that should decay, if the step is large against the fastest time constant (numerical stability); a system that mixes fast and slow modes forces explicit methods into tiny steps and calls for implicit ones (stiffness).
The step is chosen against the fastest time constant that matters. If the step is ten times smaller or more, Euler is enough and the cheapest choice, semi-implicit if the system oscillates; long or precise simulations call for RK4 or an adaptive solver; stiff systems for an implicit method. On a microcontroller a simple fixed step usually wins (flyswatter rule).
The cost is small where it matters most. An autopilot's estimator integrates the IMU hundreds or thousands of times per second, with 1 ms steps against dynamics of tens of milliseconds, so first or second order methods are plenty; one RK4 step of a 12-state model is on the order of a thousand floating-point operations, a few microseconds on a Cortex-M7.
A simulator should integrate finer than the controller it tests runs. When a 400 Hz controller is tested against a plant integrated with the same 2.5 ms step, the method's error mixes with the controller's and cannot be told apart.
What changes in the system when time becomes steps (sampled modes, aliasing, lost phase) is the modelling side, discretization.