braincell.quad.rk4_step

Contents

braincell.quad.rk4_step#

braincell.quad.rk4_step(target, *args)[source]#

Advance one step with the classical fourth-order Runge-Kutta method.

The classical RK4 scheme is the four-stage, fourth-order explicit Runge-Kutta method:

\[\begin{split}k_1 &= f(t_n, y_n), \\ k_2 &= f\!\left(t_n + \tfrac{\Delta t}{2},\ y_n + \tfrac{\Delta t}{2} k_1\right), \\ k_3 &= f\!\left(t_n + \tfrac{\Delta t}{2},\ y_n + \tfrac{\Delta t}{2} k_2\right), \\ k_4 &= f\!\left(t_n + \Delta t,\, y_n + \Delta t \, k_3\right), \\ y_{n+1} &= y_n + \tfrac{\Delta t}{6}\left(k_1 + 2 k_2 + 2 k_3 + k_4\right).\end{split}\]

Local truncation error is \(O(\Delta t^5)\); global error is \(O(\Delta t^4)\). RK4 is the canonical fixed-step ODE solver and is a sensible default whenever the right-hand side is smooth and the problem is not stiff.

Parameters:
  • target (DiffEqModule) – Differential-equation module to advance.

  • *args – Extra positional arguments forwarded to target’s integration hooks.

Returns:

Updates target’s state in place.

Return type:

None

See also

ralston4_step

An error-optimal four-stage fourth-order variant.

rk3_step

Lower-order three-stage scheme with cheaper steps.

Notes

Butcher tableau (rk4_tableau):

\[\begin{split}\begin{array}{c|cccc} 0 & 0 & 0 & 0 & 0 \\ \tfrac{1}{2} & \tfrac{1}{2} & 0 & 0 & 0 \\ \tfrac{1}{2} & 0 & \tfrac{1}{2} & 0 & 0 \\ 1 & 0 & 0 & 1 & 0 \\ \hline & \tfrac{1}{6} & \tfrac{1}{3} & \tfrac{1}{3} & \tfrac{1}{6} \end{array}\end{split}\]

Examples

>>> import brainstate
>>> import brainunit as u
>>> from braincell.quad import rk4_step
>>> with brainstate.environ.context(t=0. * u.ms, dt=0.01 * u.ms):
...     rk4_step(my_neuron)