ode_solve
ode_solve(
f,
initial_value,
start_time,
end_time=None,
time_steps=None,
dt=None,
dt_min=None,
method='rk4',
tolerance=None,
)Solve a user-written ordinary differential equation.
Solves dy/dt = f(t, y) forward from start_time with a ported Numerics Runge-Kutta method (P2 “math extras”): fixed-step second- or fourth-order Runge-Kutta over an equally-spaced grid (method="rk2"/"rk4" with end_time/time_steps), the fourth-order method’s single-step form (method="rk4" with dt alone), or one of the two adaptive-step-size methods, Runge-Kutta-Fehlberg or Runge-Kutta-Cash-Karp (method="rkf"/"cash_karp" with dt/dt_min).
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| f | callable | A function taking two numbers (t, y) and returning dy/dt, one number. |
required |
| initial_value | float | The value of y at start_time. |
required |
| start_time | float | The time to start integrating from. | required |
| end_time | (float, int) | The end time and the number of equally-spaced points between start_time and end_time (inclusive of both ends). Required together for method="rk2"; for method="rk4", supplying them selects this array form over the single-step form (see dt below) – their PRESENCE, not a separate flag. |
None |
| time_steps | (float, int) | The end time and the number of equally-spaced points between start_time and end_time (inclusive of both ends). Required together for method="rk2"; for method="rk4", supplying them selects this array form over the single-step form (see dt below) – their PRESENCE, not a separate flag. |
None |
| dt | float | The step size. For method="rk4" without end_time/time_steps, the single step to advance by (the ODE is solved once, at start_time + dt). For method="rkf"/"cash_karp", the maximum internal step size, required together with dt_min. |
None |
| dt_min | float | The minimum internal step size for method="rkf"/"cash_karp", required together with dt. |
None |
| method | str | One of "rk4" (the default), "rk2", "rkf", or "cash_karp". |
"rk4" |
| tolerance | float | The absolute error tolerance for method="rkf"/"cash_karp" alone. Left unset, the ported routine’s own default (1e-3) applies. |
None |
Returns
| Name | Type | Description |
|---|---|---|
| float or numpy.ndarray | For method="rk2", or "rk4" with end_time/time_steps: an array of length time_steps, the solution at each grid point (y[0] is initial_value). For every other case: a single number, the solution at start_time + dt ("rk4"’s single-step form) or under the adaptive step control ("rkf"/"cash_karp"). |
Examples
>>> import corehydropy as ch
>>> y = ch.ode_solve(lambda t, y: y, initial_value=1, start_time=0, end_time=1,
... time_steps=100)
>>> round(y[-1], 5)
2.71828