Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 38 ยท Integration and Differential Equations

Solving ODEs with solve_ivp

Solve initial value problems in Python with scipy.integrate.solve_ivp: choose RK45 or DOP853, set tolerances, sample with t_eval, use events and dense output.

An ordinary differential equation (ODE) describes how something changes: a population grows at a rate that depends on its size, a hot cup cools at a rate that depends on the temperature difference, a pendulum accelerates according to its angle. You rarely need the formula for the answer. You usually want the trajectory: the value at many times, starting from a known state.

That is an initial value problem: you know the rate dy/dt = f(t, y) and the starting value y(t0), and you want y later. scipy.integrate.solve_ivp is the modern, all-purpose solver for this. You give it the function f, a time span and the starting state, and it steps forward in time, choosing its own step sizes to stay within an error tolerance.

flowchart LR
    A["f(t, y)<br/>the rule for change"] --> S["solve_ivp"]
    B["t_span = (t0, t1)"] --> S
    C["y0<br/>starting state"] --> S
    S --> R["result object<br/>sol.t, sol.y, events"]

The three inputs of every initial value problem, and the result object that comes back.

The first solve

The function you write takes (t, y) and returns the derivative. Note the order: time first, state second (the opposite of the older odeint). y is always a one-dimensional array, even for a single equation.

Python Exponential decay

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The result is an object with several attributes. The important ones:

  • sol.t: the times at which the solver stored a value (shape (n,)).
  • sol.y: the states, with shape (n_equations, n). Each column is the state at one time, each row is one variable over time.
  • sol.success and sol.message: always check these in real code.
  • sol.nfev: how many times your function was called, a useful cost measure.

By default the solver picks the times itself, so you get a handful of points that are not evenly spaced. The answer is still accurate at those points, but at the default tolerances the final value above is off by roughly a quarter of a percent. We will fix both issues next.

Choosing output times with t_eval

To get values at times you choose, pass t_eval. The solver still takes its own internal steps; it interpolates to your times afterwards, so asking for more output points does not make the solve slower or more accurate.

Python Logistic growth sampled at chosen times

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The curve is the familiar S shape: slow at first, fastest around half the capacity, then levelling off at K.

Accuracy: rtol and atol

The solver adapts its step size to keep the estimated error small. You control "small" with two tolerances. Roughly, the error at each step stays below atol + rtol * |y|: rtol is the relative part and atol the absolute part, which matters when y is close to zero. The defaults (rtol=1e-3, atol=1e-6) are loose, fine for a rough picture but too sloppy for results you rely on. For most scientific work, set rtol=1e-8, atol=1e-10.

Python Tighter tolerances cost more steps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each tightening costs more steps, but the error falls much faster than the step count grows. Remember, though, that rtol limits the local error per step. Errors can accumulate over a long integration, especially for unstable or chaotic systems, so the final error can be larger than rtol suggests.

Choosing a method

The method argument selects the algorithm:

Method Use it for
RK45 (default) General smooth problems at moderate accuracy
RK23 Low accuracy, cheap steps
DOP853 High accuracy (rtol around 1e-10 and below), smooth problems
Radau, BDF, LSODA Stiff problems (see Stiff Systems and Higher-Order ODEs)

For non-stiff problems, RK45 is the sensible starting point and DOP853 the upgrade when you need many correct digits.

Systems of equations: the pendulum

A system is just a vector y. A pendulum of length L has angle theta and angular velocity omega. The state is [theta, omega], and the rules are dtheta/dt = omega and domega/dt = -(g/L) sin(theta). Extra parameters can be passed with args=(g, L), which are forwarded to your function after t, y.

Python A swinging pendulum

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A large swing is far from a clean sine wave, because sin(theta) is not close to theta; the pendulum spends a long time near the top. The phase portrait on the right is a closed loop: the motion repeats forever, because this model has no friction.

Events: stopping or recording at a condition

Often you want to know when something happens: when the ball hits the floor, when the population passes 50. An event is a function event(t, y) whose sign change is detected precisely. Give it attributes:

  • event.terminal = True stops the integration at the event.
  • event.direction = -1 fires only when the value goes from positive to negative (+1 for the opposite, 0 for either).

The times are in sol.t_events and the states in sol.y_events, one array per event function.

Python A falling ball and the pendulum period

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Events are far better than scanning the output for the first sign change, because the solver locates the crossing between steps to high accuracy. The tiny gap between the measured and small-angle periods is real physics: the formula assumes a tiny swing.

Dense output

With dense_output=True the solution carries a continuous interpolant sol.sol. Call it with any time (or an array of times) inside the span and it returns the state there. That is handy when you decide after solving where you need values.

Python Evaluate the solution anywhere

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Tip: Use t_eval when you know the output grid in advance, and dense_output when you do not. Both interpolate; neither changes the solver's internal steps.

The older interface: odeint

Before solve_ivp, SciPy's ODE solver was scipy.integrate.odeint, a wrapper around the Fortran library LSODA. It is still available, and a huge amount of existing code, many papers' supplementary scripts and many tutorials use it, so you should be able to read and convert it. It differs from solve_ivp in four ways:

odeint solve_ivp
derivative function f(y, t): state first, time second f(t, y): time first
time points you pass the whole array of output times, t = np.linspace(...) you pass t_span and optionally t_eval
result an array of shape (len(t), n): one row per time sol.y of shape (n, len(t)), with sol.t
methods LSODA only (switches automatically between stiff and non-stiff) RK45, DOP853, Radau, BDF, LSODA... and events

odeint(f, y0, t, args=(...), tfirst=False) accepts the keyword tfirst=True to use the f(t, y) order, which makes a function reusable with solve_ivp. Set full_output=True to receive a dictionary with statistics (nfe, the number of function evaluations, and a message). There is no event detection and no dense output; if you need them, switch to solve_ivp.

Python odeint next to solve_ivp for a damped oscillator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two solvers agree to about the accuracy of the default tolerances, and the only things to remember are the two transposed conventions: argument order for the function and row versus column for the result. When converting old code, swap the arguments of the function (or add tfirst=True), transpose the result, and enjoy the extra methods and events of solve_ivp.

Common mistakes

  • Swapping t and y in the function signature. It is always f(t, y).
  • Returning a scalar for a system, or a list of the wrong length. The output must have the same size as y.
  • Ignoring sol.success. If the solver fails (for example, the solution blows up), you still get partial results, and only success and message tell you.
  • Forgetting the shape. sol.y[0] is the first variable over time; sol.y[:, 0] is the initial state.
  • Using loose default tolerances for results you plan to report.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the correct signature of the function passed to solve_ivp?

    solve_ivp calls f(t, y, *args): time first, then the state vector. The older odeint used the opposite order.
  2. 2. For a system of 3 equations solved at 40 output times, what is the shape of sol.y?

    sol.y has one row per variable and one column per time, so its shape is (n_equations, n_times) = (3, 40).
  3. 3. What does t_eval do?

    t_eval only chooses where the solution is reported; the solver's internal steps and accuracy are controlled by rtol, atol and the method.
  4. 4. You want the integration to stop the moment y[0] falls through zero. What do you do?

    An event function with terminal=True stops the solve at the sign change, and direction=-1 restricts it to downward crossings. The root is located precisely between steps.
  5. 5. Which pair of tolerances are the right ones to tighten for more accurate results?

    rtol (relative) and atol (absolute) bound the local error per step. The defaults are loose, so scientific work usually sets them much smaller.

Summary

  • solve_ivp(f, (t0, t1), y0) solves dy/dt = f(t, y); f takes time first and returns an array as long as y.
  • The result holds sol.t, sol.y of shape (n_equations, n_times), plus success, message and nfev. Check success.
  • t_eval picks output times; dense_output=True gives a continuous sol.sol(t).
  • Tighten rtol and atol for reliable numbers; DOP853 helps when you need very high accuracy on smooth problems.
  • Events with terminal and direction find precisely when a condition happens, and args= passes constants to f.

Next up: problems where the default solvers crawl, and equations of higher order, in Stiff Systems and Higher-Order ODEs.