Initial-value simulation
Simulating a dynamical system from known initial conditions.
SciPy · Function
Numerically integrates an initial-value problem for a system of ordinary differential equations. It supports multiple solver methods, requested output times, dense interpolation, and event detection.
solve_ivp numerically advances an initial state through a system defined by fun(t, y) over t_span. It can use different integration methods, sample the solution at t_eval, detect event functions, and optionally construct a continuous interpolant.
scipy.integrate.solve_ivp(fun, t_span, y0, method='RK45', t_eval=None, dense_output=False, events=None, vectorized=False, args=None, **options)| Name | Type | Description |
|---|---|---|
fun | callable | Right-hand side of the system: the time derivative of the state y at time t; called as fun(t, y). |
t_span | 2-member sequence | Two-member interval (t0, tf) over which integration is performed. |
y0 | array_like | Initial state, with shape (n,). |
method | string or OdeSolver, optional | Integration method; defaults to RK45. Supported choices include RK23, DOP853, Radau, BDF, and LSODA. |
t_eval | array_like or None, optional | Times at which to store the computed solution; values must be sorted and within t_span. |
dense_output | bool, optional | Whether to compute a continuous solution representation. |
events | callable or list of callables, optional | Event function or collection of event functions whose zero crossings are tracked. |
vectorized | bool, optional | Whether fun is implemented to accept multiple state columns simultaneously. |
args | tuple, optional | Extra positional arguments passed to fun and event functions. |
options | dict, optional | Additional solver-specific options, such as tolerances, maximum step, and initial step. |
Bunch object with fields including t, y, sol, t_events, y_events, nfev, njev, nlu, status, message, and success.
import numpy as np
from scipy.integrate import solve_ivp
def rhs(t, y):
return -y
sol = solve_ivp(rhs, (0.0, 2.0), [1.0], t_eval=np.linspace(0.0, 2.0, 5))
print(sol.t)
print(sol.y[0])Integrates the scalar equation y′ = −y from t = 0 to 2 and returns values at five requested times.
Simulating a dynamical system from known initial conditions.
Computing trajectories at a fixed set of times for analysis or plotting.
Stopping or recording integration when a state-dependent condition is reached.
Obtaining interpolated state values between solver output points.
It solves an initial-value problem for a system of ordinary differential equations, with the right-hand side supplied as fun(t, y). The initial state is y0 and integration proceeds from t0 to tf in t_span.
Follow-up: How would you obtain values between accepted solver steps? Set dense_output=True and use the returned sol callable to evaluate the continuous interpolant.
RK45 is the default method. The page also documents explicit Runge–Kutta methods, implicit methods for stiff problems, and LSODA, which switches between Adams and BDF methods.
Follow-up: When is an implicit method preferable? Use Radau or BDF when the system is stiff; LSODA can automatically switch between nonstiff and stiff modes.
The result contains the time points and state values, along with status information such as success, message, number of function evaluations, and event outputs when applicable.
Follow-up: How can terminal events stop integration? Give an event function a terminal attribute so the solver terminates when that event is detected.
scipy.integrate.OdeSolverscipy.integrate.RK45scipy.integrate.RK23scipy.integrate.DOP853scipy.integrate.Radauscipy.integrate.BDFscipy.integrate.LSODA