Documentation / @interstellar-tools/equations / rk45Integrate
Function: rk45Integrate()
ts
function rk45Integrate(
f: OdeRightHandSideType,
y0: readonly number[],
t0: number,
t1: number,
options?: Rk45IntegrateOptionsInterface
): OdeSolutionInterface;Defined in: helpers/rk45.ts:99
Integrate a system of ODEs from t0 to t1 with adaptive steps: the Dormand–Prince method, RK45, which takes long steps where the solution is smooth and short ones where it changes fast, to keep each step's error within a tolerance.
Each step evaluates
A step with
Notes
- Use it for orbits that aren't circular: on an eccentric orbit, it steps quickly through apoapsis and slowly through periapsis, where rk4Integrate needs the short step everywhere.
- The tolerance bounds each step's error, not the total: errors add up over many steps. Tighten it until the answer stops changing.
- The solution holds the accepted steps, with
t1exactly last. Their times are wherever the steps fell; for given times, integrate to each in turn. t1may be beforet0, to integrate backwards.
Parameters
| Parameter | Type | Description |
|---|---|---|
f | OdeRightHandSideType | The right-hand side y0. |
y0 | readonly number[] | State at t0 (finite components). |
t0 | number | Start time (finite). |
t1 | number | End time (finite, not equal to t0). |
options? | Rk45IntegrateOptionsInterface | tolerance and absoluteTolerance (finite, > 0), initialStep and maxStep (finite, > 0), maxSteps (an integer >= 1), and stop. |
Returns
The times and states of the accepted steps, t[k] with y[k], from t0 to t1.
Throws
- If a number is not finite,
maxStepsis not an integer, orfreturns an array of the wrong length. - If
t1 = t0, an option is out of range, or the integration needs more thanmaxStepssteps, or a step too small to tell from rounding.
Example
ts
// A harmonic oscillator over one period: back where it started
const { t, y } = rk45Integrate((_t, [x, v]) => [v, -x], [1, 0], 0, 2 * Math.PI);
y[y.length - 1]; // ≈ [1, 0], to 3e-9
t.length; // 94: the start, and the 93 steps it tookSee
- rk4Integrate
- Dormand & Prince (1980), "A family of embedded Runge-Kutta formulae", Journal of Computational and Applied Mathematics 6(1), 19-26
- Hairer, Nørsett & Wanner, Solving Ordinary Differential Equations I, 2nd ed. (1993), section II.4