rk2#
Functions
-
template<typename State, typename DerivFunc>
void rk2_step(DerivFunc &&f, const State &x, double t, double dt, State &x_out)# Perform a single step of second-order Runge-Kutta (RK2) integration.
This function performs one time step of the explicit second-order Runge-Kutta method (midpoint method) for solving ordinary differential equations of the form:
\[ \frac{d\mathbf{x}}{dt} = \mathbf{f}(t, \mathbf{x}) \]The RK2 method updates the state using two function evaluations:
\[\begin{split} \mathbf{k}_1 = \mathbf{f}(t, \mathbf{x}) \\ \mathbf{k}_2 = \mathbf{f}\left(t + \frac{\Delta t}{2}, \mathbf{x} + \frac{\Delta t}{2}\mathbf{k}_1\right) \\ \mathbf{x}_{\text{out}} = \mathbf{x} + \Delta t \, \mathbf{k}_2 \end{split}\]RK2 has a local truncation error of \(O(\Delta t^3)\) and is second-order accurate. It is a good compromise between accuracy and computational cost for many applications.
- Template Parameters:
State – Type of the state vector (must support scalar multiplication and addition).
DerivFunc – Type of the derivative function/functor.
- Parameters:
f – Derivative function with signature:
void f(double t, const State& x, State& dxdt). Computes the derivative at timetand statex, storing the result indxdt.x – Current state vector.
t – Current time.
dt – Time step size.
x_out – Output state vector after one RK2 step.