Skip to content

ode

tangent/ode validated against scipy.integrate

Initial value problems y' = f(t, y) with plain-array (or scalar) state. Adaptive Dormand-Prince RK45 with dense output and event detection for non-stiff systems, an adaptive Rosenbrock method for stiff ones, and the classic fixed-step integrators. PDEs are handled by the method of lines: discretize space yourself and hand the resulting ODE system to a solver.

Terminal window
npm install @tangent.to/ode # npm
deno add jsr:@tangent/ode # Deno / JSR
Run the example notebook

Non-stiff integration

The result is {t, y, success, nfev, nsteps} with y component-major: y[i] is the trajectory of state component i over the reported times t.

SignatureDescription
rk45(f, tSpan, y0, options?)Adaptive Dormand-Prince RK45: a 5th-order step with an embedded 4th-order error estimate, PI step-size control, and a dense-output interpolant.
solve(f, tSpan, y0, options?)Dispatcher over every method (scipy solve_ivp style). Defaults to rk45; select another with options.method.

Here f is (t, y) => dydt, tSpan is [t0, tEnd] (backward integration allowed when tEnd < t0), and y0 is a number[] or a scalar.

Stiff systems

SignatureDescription
rosenbrock(f, tSpan, y0, options?)Adaptive Rosenbrock method for stiff systems, where an explicit method would be forced into vanishingly small steps.

Fixed step

Non-adaptive integrators for when a fixed grid is wanted. Same call shape; use options.step (or tEval) to set the grid.

SignatureDescription
euler(f, tSpan, y0, options?)Explicit (forward) Euler, 1st order.
rk2(f, tSpan, y0, options?)2nd-order Runge-Kutta (midpoint).
rk4(f, tSpan, y0, options?)Classic 4th-order Runge-Kutta.

Options

Passed as the options object to any adaptive solver.

OptionDescription
rtolRelative tolerance (default 1e-6).
atolAbsolute tolerance (default 1e-9).
tEvalTimes at which to report the solution, via the dense-output interpolant.
eventsg(t, y) => number (or an array of them); each sign change is bracketed and its root reported in events.
maxStepLargest allowed step size.
firstStepInitial step size (chosen automatically if omitted).
maxStepsSafety cap on accepted plus rejected steps.

Verified against scipy

The comparison suite integrates shared problems against scipy.integrate: rk45 tracks solve_ivp with RK45 to tolerance on non-stiff systems, rosenbrock holds accuracy on stiff ones where fixed-step methods diverge, and event roots and dense-output samples land on the reference trajectory. The Lotka-Volterra orbit closes on itself over a period, the standard check that the adaptive controller is conserving the invariant.