Skip to content

ode

The ode module provides numerical integrators for ordinary differential equations. It covers fixed-step (Euler, RK4) and adaptive-step (RK45 Dormand-Prince) methods, in both scalar and vector forms.

use std::core::ode

All integrators return an array of { t, y } records sampled along the trajectory.

Explicit forward-Euler. f(t, y) -> dy/dt is the derivative.

use std::core::ode
// Exponential decay: dy/dt = -y
let trajectory = ode::euler(|t, y| 0.0 - y, 1.0, 0.0, 5.0, 0.01)
print(trajectory[trajectory.length - 1].y) // ~0.0067

Classical 4th-order Runge-Kutta. Same signature as euler with much better accuracy at the same dt.

ode::euler_system(f, y0_vec, t_start, t_end, dt)

Section titled “ode::euler_system(f, y0_vec, t_start, t_end, dt)”

Forward-Euler over a vector state. f(t, y_vec) -> dy_vec/dt returns the component-wise derivative.

ode::rk4_system(f, y0_vec, t_start, t_end, dt)

Section titled “ode::rk4_system(f, y0_vec, t_start, t_end, dt)”

RK4 over a vector state.

use std::core::ode
// Harmonic oscillator: y'' = -k y, written as y0' = y1, y1' = -k y0
let trajectory = ode::rk4_system(
|t, y| [y[1], 0.0 - 1.0 * y[0]],
[1.0, 0.0], // x = 1, v = 0
0.0, 10.0, 0.01
)
print(trajectory.length)

The RK45 integrators automatically adjust the step size to maintain a target error tolerance. Optional parameters default to sensible values; pass 0.0 to fall back to the default.

ode::rk45(f, y0, t_start, t_end, tol?, dt_init?, dt_min?, dt_max?)

Section titled “ode::rk45(f, y0, t_start, t_end, tol?, dt_init?, dt_min?, dt_max?)”

Scalar adaptive RK45. Returns { t, y } records at each accepted step.

let trajectory = ode::rk45(|t, y| 0.0 - y, 1.0, 0.0, 5.0)

ode::rk45_system(f, y0_vec, t_start, t_end, tol?, dt_init?, dt_min?, dt_max?)

Section titled “ode::rk45_system(f, y0_vec, t_start, t_end, tol?, dt_init?, dt_min?, dt_max?)”

Vector adaptive RK45.

  • monte_carlo — stochastic trajectories
  • physics — physics-specific step functions built on ODE primitives