Integrators#
Inspectable Euler, RK4 and adaptive integration of SI state models.
- class engineeringkit.integrators.IntegrationResult(t, history, method, evaluations)[source]#
Bases:
objectOutput of
engineeringkit.integrators.integrate_ode().- Parameters:
t (ndarray)
history (ndarray)
method (str)
evaluations (int)
- evaluations: int#
Number of calls to the right-hand side.
- Type:
int
- history: ndarray#
State at each sample, shape
(samples, states), in the model’s units.- Type:
ndarray
- method: str#
Integration method used (
"euler","rk4"or a SciPy method name).- Type:
str
- plot(labels=None, ax=None)[source]#
Plot each state against time; return
(fig, ax).- Parameters:
labels (sequence of str, optional) – One label per state, including its unit (e.g.
"x [m]").ax (matplotlib.axes.Axes, optional) – Axes to draw on; a new figure is created if omitted.
- Returns:
(fig, ax).- Return type:
tuple
- summary()[source]#
Method, sample count, right-hand-side evaluations and final state.
- Return type:
dict
- t: ndarray#
Sample times [s], strictly increasing.
- Type:
ndarray
- engineeringkit.integrators.integrate_ode(rhs, y0, t, method='RK45', *, rtol=1e-08, atol=1e-10)[source]#
Solve \(dy/dt = f(t, y)\) on a strictly increasing finite time grid.
- Parameters:
rhs (
Callable) –rhs(t, y)returning the derivative, a finite array shaped likey0, in state units per second.y0 (float or array_like) – Initial state, in the model’s units.
t (array_like) – At least two finite, strictly increasing sample times [s].
method (
str) –"euler"or"rk4"for explicit fixed steps on the gridt; any other name is passed toscipy.integrate.solve_ivp()(e.g."RK45","Radau").rtol (
float) – Positive relative and absolute tolerances of the adaptive methods.atol (
float) – Positive relative and absolute tolerances of the adaptive methods.
- Returns:
Times [s], the state history at each time and the number of right-hand-side evaluations.
- Return type:
- Raises:
ValueError – If the grid, initial state or tolerances are invalid, or
rhsreturns a nonfinite or wrongly shaped derivative.RuntimeError – If a fixed-step state becomes nonfinite or the adaptive solver fails.
Notes
Explicit methods are for non-stiff teaching problems; the step size controls their stability and accuracy. Inputs are raw SI numbers with no dimensional metadata; see Numerical methods and inspectable histories.