Written and maintained by CASRAI Editorial Board
Published
On this page
To solve an ordinary differential equation (ODE) in Python, use scipy.integrate.solve_ivp(fun, t_span, y0): you write the right-hand side as a function fun(t, y), give the time interval and the starting values, and SciPy returns the solution at adaptively chosen time points. The default method is RK45, an explicit Runge-Kutta scheme for non-stiff problems; the SciPy documentation recommends it when you are unsure, and Radau, BDF or LSODA for stiff ones. SciPy’s own odeint page says that for new code you should use solve_ivp.
What scipy.integrate.solve_ivp is used for in research
solve_ivp solves initial value problems for systems of first-order ODEs: given dy/dt = f(t, y) and y(t0) = y0, it integrates forward from t0 to the end of t_span. In research this covers the standard dynamical models: predator-prey and epidemic compartment models, chemical kinetics, pharmacokinetic compartments, orbital and projectile motion, and neuron or circuit models. The SciPy tutorial adds that higher-order equations are handled by rewriting them as a first-order vector system, adding the derivatives to y.
The full signature in the current reference is solve_ivp(fun, t_span, y0, method='RK45', t_eval=None, dense_output=False, events=None, vectorized=False, args=None, **options). The arguments that matter most in everyday work are these.
fun(t, y, *args): returns dy/dt. Time comes first, the state vector second.t_span: a pair(t0, tf).y0: the initial state, one value per equation.method:'RK45'(default),'RK23','DOP853','Radau','BDF'or'LSODA'.t_eval: times at which to store the solution; must be sorted and lie withint_span.dense_output: if true, the result carries a continuous solution object,sol.sol.events: functions whose zero crossings the solver should locate, optionally stopping the integration.args: extra parameters passed tofun(and to events and the Jacobian), so you do not need global variables.rtol,atol: error tolerances, defaults 1e-3 and 1e-6.
If you work in another language, the same task is covered in our guides to ode45 in MATLAB and the deSolve package in R; for epidemic and population models in general see epidemiology models.
How to install or access SciPy
The official SciPy installation page gives two routes. With pip, run python -m pip install scipy; with conda, run conda install scipy (mamba works the same way). The page recommends using a virtual environment with pip and Miniforge for conda, and advises against installing through a system package manager because those packages are often older versions. For new projects it also points to uv and pixi project workflows. After installation the function is imported with from scipy.integrate import solve_ivp. NumPy is installed as a dependency, and the examples below use it for the time grid.
How to solve an ODE system with solve_ivp, step by step
The worked example is the Lotka-Volterra predator-prey system used in the SciPy reference: dx/dt = a·x − b·x·y and dy/dt = −c·y + d·x·y, with a = 1.5, b = 1, c = 3, d = 1, starting from x = 10 and y = 5 and integrated over t = 0 to 15. This is an illustrative model, not data from a particular study. All numbers quoted below were produced by running this code with SciPy 1.18.1.
-
Write the right-hand side. Time is the first argument, the state the second, and parameters follow:
def lotkavolterra(t, z, a, b, c, d): x, y = z return [a*x - b*x*y, -c*y + d*x*y] -
Call the solver. Pass the parameters through
argsand ask for a dense solution:from scipy.integrate import solve_ivp sol = solve_ivp(lotkavolterra, [0, 15], [10, 5], args=(1.5, 1, 3, 1), dense_output=True) -
Check that it worked.
sol.successisTrueandsol.statusis 0 when the end oft_spanwas reached. In this run the solver took 57 time points and 470 right-hand-side evaluations (sol.nfev). -
Evaluate the continuous solution. Because
dense_output=True,sol.solcan be called at any time in the interval:import numpy as np t = np.linspace(0, 15, 300) z = sol.sol(t) # shape (2, 300) -
Or request fixed output times.
t_evalstores the solution only at the times you list, which suits comparison with measurements taken at known times:sol = solve_ivp(lotkavolterra, [0, 15], [10, 5], args=(1.5, 1, 3, 1), t_eval=[0, 5, 10, 15]) -
Tighten the tolerances and compare. Re-run with
rtol=1e-8, atol=1e-10and confirm that your result does not change. The next section shows why this step matters.
Choosing a method and tolerances
The documentation describes RK45, RK23 and DOP853 as explicit Runge-Kutta methods for non-stiff problems, DOP853 being the choice when high precision is needed, and Radau and BDF as implicit methods for stiff problems; LSODA switches automatically between a non-stiff and a stiff method. The solver keeps its local error estimate below atol + rtol * abs(y), and atol may be an array with one entry per component when the components differ greatly in scale. The tolerances are local error controls, not a guarantee on the final result, which is why a convergence check against tighter tolerances is good practice.
The table below shows the effect on the Lotka-Volterra example. The data table lists the maximum absolute error of x and y at t = 5 and t = 15 against a high-accuracy reference solution (DOP853 with rtol = atol = 1e-13), together with the number of right-hand-side evaluations.
- With the defaults, RK45 reports x = 0.2575 at t = 5 and x = 0.5820 at t = 15; with rtol = 1e-8 and atol = 1e-10 it reports 0.2642 and 0.7138. The default-tolerance answer at t = 15 is wrong in the second digit, even though the call reported success.
- Tightening the tolerances raised RK45’s evaluation count from 470 to 3,056.
- DOP853 with default tolerances was within about 0.001 of the reference at a cost of 644 evaluations, a better trade than RK45 at the same settings for this smooth problem.
The SciPy tutorial makes the same point with its Airy-function example: with default tolerances the computed solution deviates noticeably from the known answer, and setting rtol=1e-8, atol=1e-8 reduces the deviation. The lesson is general: success status means the solver reached the end within its own error control, not that the answer is accurate to the digits you plan to report.
How to read the output
solve_ivp returns an OdeResult object. Its fields, as documented in the reference, are:
t: array of shape(n_points,)with the time points.y: array of shape(n, n_points), one row per state variable and one column per time point. In the examplesol.y.shapeis(2, 57), sosol.y[0]is x andsol.y[1]is y. This is the transpose of whatodeintreturns.sol: the continuous solution, present only whendense_output=True.t_events,y_events: lists of arrays with the times and states at each event, one entry per event function.nfev,njev,nlu: counts of right-hand-side evaluations, Jacobian evaluations and LU decompositions. For RK45 the last two stay at 0, as seen in the run above.status: -1 if the step failed, 0 if the end oft_spanwas reached, 1 if a terminal event stopped the integration.messageandsuccess: a text description and a boolean that is true whenstatusis 0 or higher.
Stopping at an event
An event is a function event(t, y) whose zero crossing the solver locates. Attributes on the function control it: event.terminal = True stops the integration at the first crossing, and event.direction restricts the crossing direction (-1 for decreasing, +1 for increasing, 0 for both). The SciPy reference uses a projectile that stops when height reaches zero. A version of that idea, run with SciPy 1.18.1:
def hit_ground(t, y):
return y[0]
hit_ground.terminal = True
hit_ground.direction = -1
def cannon(t, y):
return [y[1], -9.81]
s = solve_ivp(cannon, [0, 100], [0, 10], events=hit_ground)
s.status # 1: a terminal event occurred
s.t_events # [array([2.0387...])]
The state starts at height 0 moving upward at 10 m/s, so the downward crossing is detected at t ≈ 2.04 s, matching 2·10/9.81. Because the integration stopped there, status is 1 and message reads “A termination event occurred.” For a longer physical treatment in MATLAB, see our projectile motion guide.
Troubleshooting solve_ivp
The arguments of the function are the wrong way round
solve_ivp calls fun(t, y). The older odeint calls func(y, t, ...) by default, and only calls func(t, y, ...) when you pass tfirst=True, an option added in SciPy 1.1.0. Code copied from an odeint example into solve_ivp without swapping the arguments produces wrong results or shape errors rather than a clear message. Likewise, odeint returns an array with one row per time point, whereas solve_ivp returns one row per variable.
TypeError about a missing positional argument
If args has fewer entries than the function expects, Python raises a TypeError naming the missing parameter. In the example, passing args=(1.5, 1, 3) gives “lotkavolterra() missing 1 required positional argument: ‘d'”. Count the parameters after t and y, and pass a tuple, not a list.
The result looks plausible but changes when you tighten tolerances
This is the default-tolerance effect shown above. Run the model at two tolerance settings, or with two methods, and report the result only to the digits on which they agree. Set atol per component if one variable is many orders of magnitude smaller than another.
An event is not detected
The reference warns that an event can be missed when the solution crosses zero more than once within a single step. Limit the step with max_step or supply tighter tolerances when events must not be missed. Also check the sign convention of direction.
A Jacobian is ignored or rejected with RK45
The SciPy tutorial states that the default RK45 method does not support Jacobian matrices. Supply jac only with the methods that use it, which are Radau, BDF and LSODA, for example solve_ivp(func, t_span, y0, method='Radau', jac=gradient). For large systems, jac_sparsity can describe the sparsity pattern.
The solver is slow or takes a huge number of steps
A very large nfev for an explicit method is a typical sign of a stiff problem. Switch to method='Radau', 'BDF' or 'LSODA'. On the Lotka-Volterra example, which is not stiff, those methods are not cheaper: Radau used 1,076 evaluations plus 30 Jacobian evaluations and 146 LU decompositions, against 470 evaluations for RK45 at default tolerances. Choose by problem type, not by habit.
t_eval raises an error
t_eval must be sorted and must lie within t_span. Passing times outside the interval, or an unsorted list, is rejected by the solver.
Free alternatives to solve_ivp
Within Python, solve_ivp is the interface SciPy recommends. The alternatives below are other free routes to the same task; the data table above covers the methods inside solve_ivp itself.
| Option | What it is | When to consider it |
|---|---|---|
scipy.integrate.odeint |
The legacy SciPy interface to the LSODA solver from ODEPACK, which switches between Adams (non-stiff) and BDF (stiff) methods. | Maintaining existing code. The SciPy page says new code should use solve_ivp. |
| scikits.odes | A Python scikit described by its project page as an extension to SciPy. It provides the SUNDIALS CVODE and IDA solvers (BDF for stiff problems, Adams-Moulton for non-stiff) and exposes SciPy’s dopri5 and dop853. | Differential-algebraic equations or the SUNDIALS solvers, which solve_ivp does not offer. |
| deSolve (R) | The R package for ordinary differential equations, covered in our deSolve guide. | Analysis pipelines that already run in R. |
For the related task of solving nonlinear algebraic equations with SciPy rather than integrating over time, see scipy.optimize.fsolve.
Sources
- SciPy documentation: scipy.integrate.solve_ivp, checked 2026-11-18
- SciPy documentation: Integration (scipy.integrate) tutorial, checked 2026-11-18
- SciPy documentation: scipy.integrate.odeint, checked 2026-11-18
- SciPy: Installing SciPy, checked 2026-11-18
- GitHub (bmcage/odes): scikits.odes README, checked 2026-11-18
Data: solve_ivp methods and tolerances on the Lotka-Volterra example: cost and error
Eight solve_ivp configurations run on the Lotka-Volterra system from the SciPy reference (a=1.5, b=1, c=3, d=1; y0=[10,5]; t from 0 to 15; SciPy 1.18.1). Each row gives right-hand-side evaluations, Jacobian evaluations and the maximum absolute error of x and y at t=5 and t=15 against a DOP853 reference run at rtol=atol=1e-13. Compiled by running the code; the model and solver options come from the SciPy documentation.
| Configuration | Right-hand-side evaluations (nfev) | Jacobian evaluations (njev) | Maximum absolute error (population units) |
|---|---|---|---|
| RK45 default (rtol 1e-3, atol 1e-6) | 470 | 0 | 0.1367 |
| RK45 rtol 1e-6, atol 1e-9 | 1394 | 0 | 0.00002 |
| RK45 rtol 1e-8, atol 1e-10 | 3056 | 0 | 0 |
| DOP853 default | 644 | 0 | 0.00094 |
| DOP853 rtol 1e-10, atol 1e-12 | 3248 | 0 | 0 |
| LSODA default | 459 | 0 | 0.0052 |
| Radau default | 1076 | 30 | 0.00185 |
| BDF default | 639 | 10 | 0.06074 |
Sources
- SciPy documentation: scipy.integrate.solve_ivp (accessed 2026-11-18)
- SciPy documentation: Integration (scipy.integrate) tutorial (accessed 2026-11-18)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







