Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 39 ยท Numerical Methods

Adaptive Steps and solve_ivp

Understand adaptive step-size control with embedded Runge-Kutta pairs, write your own controller, and use SciPy's solve_ivp with tolerances, dense output and events.

A fixed step size is a compromise. If the solution changes slowly for most of the time and sharply for a short stretch, a step small enough for the sharp part wastes thousands of evaluations on the easy part. Adaptive step-size control adjusts h as it goes: small steps where the solution is difficult, large ones where it is smooth, with an error you choose in advance.

All professional ODE solvers work this way. This lesson shows the idea behind them by building a controller yourself and then uses the real thing, scipy.integrate.solve_ivp.

Estimating the error of a step

You cannot know the true error, but you can estimate it by computing the step two ways with different orders of accuracy. The difference between the two answers approximates the error of the lower-order one.

There are two standard techniques:

  • Step doubling. Take one step of size h, and two steps of size h/2. Compare.
  • Embedded pairs. Use a method that produces an order-p and an order-(p-1) answer from the same function evaluations, so the error estimate is almost free. The most famous pair is Dormand-Prince 5(4), the heart of SciPy's RK45 and MATLAB's ode45.

The simplest embedded pair is Heun-Euler: the same two slopes k1, k2 give both an Euler result (order 1) and a Heun result (order 2).

The controller

With a tolerance tol and an error estimate err for a step of size h:

  1. If err <= tol, accept the step; otherwise reject it and retry.
  2. Propose the next step size h_new = h * 0.9 * (tol / err)^(1/(p+1)), where p is the order of the lower method and 0.9 is a safety factor.
  3. Limit how quickly h may grow or shrink, e.g. between 0.2h and 5h.

That is the entire idea. The exponent comes from the fact that the local error scales like h^(p+1).

Python A tiny adaptive solver (Heun-Euler pair)

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 solver packs its small steps around t = 3 where the bump is, and takes steps more than twenty times larger elsewhere. A fixed step equal to the smallest one would need roughly 3,000 steps instead of the few hundred used here.

solve_ivp in practice

Python
solve_ivp(fun, t_span, y0, method="RK45", t_eval=None, dense_output=False,
          events=None, rtol=1e-3, atol=1e-6, max_step=inf)
  • fun(t, y) returns the derivative vector.
  • rtol and atol set the allowed error per step: roughly |error| <= atol + rtol * |y|. Defaults are loose; use rtol=1e-8, atol=1e-10 for accurate work.
  • method picks the algorithm: RK45 (default), RK23, DOP853 (high accuracy), and the implicit stiff solvers Radau, BDF and LSODA.
  • t_eval asks for output at given times. The solver still chooses its own internal steps and interpolates.
  • dense_output=True returns sol.sol(t), a continuous interpolant.
  • max_step stops it from jumping over narrow features.

The returned object holds sol.t, sol.y (one row per component), sol.nfev (function evaluations), sol.status and sol.message.

Python Compare solvers and tolerances

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 higher-order DOP853 needs far fewer steps at tight tolerances, even though each step is more expensive. For loose tolerances RK23 is competitive. Note that the answers for different tolerances agree up to the tolerance level, which is the quickest check that you have enough accuracy.

Dense output

An adaptive solver takes irregular steps. If you want a smooth curve, do not shrink the step; ask for dense output and evaluate the interpolant anywhere.

Python Dense output and t_eval

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: stopping at something interesting

Often you want to know when something happens: a ball hits the ground, a population reaches 1000, a pendulum crosses zero. An event is a function g(t, y) whose sign change the solver detects with a root finder. Set event.terminal = True to stop there and event.direction to react only to up- or down-crossings.

Python When does the ball hit the ground?

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.

What solve_ivp gives you

Tolerances rtol and atol control accuracy.
Methods Explicit for smooth, implicit for stiff.
Events Stop or record at sign changes.
Dense output Evaluate the solution anywhere.
1 / 2

Common mistakes

  • Treating rtol as the final error of the whole run. It controls each step; the global error can be larger.
  • Setting atol too large for a tiny solution, so the solver ignores it. Scale atol to your variables.
  • Passing fun(y, t) in odeint order. solve_ivp uses fun(t, y).
  • Plotting sol.y against sol.t when the solver took very few steps, giving a jagged curve. Use dense_output or t_eval.
  • Forgetting events need terminal to stop the run.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. An embedded Runge-Kutta pair gives an error estimate by:

    The two results share function evaluations, and their difference approximates the error of the lower-order one.
  2. 2. If a step has error larger than the tolerance, an adaptive solver:

    The step is rejected, the step size is reduced by the controller, and the step is retried.
  3. 3. In solve_ivp, the function signature is:

    solve_ivp calls fun(t, y); the older odeint used fun(y, t).
  4. 4. How do you stop integration when a quantity crosses zero?

    Events detect sign changes; marking them terminal ends the run at that time.

Summary

  • Adaptive solvers estimate the error from two methods of different order and adjust h to meet a tolerance.
  • The controller rescales h by (tol/err)^(1/(p+1)) with a safety factor.
  • solve_ivp offers RK45, DOP853, and stiff solvers, with rtol, atol, t_eval, dense_output.
  • Events locate and stop at interesting moments.
  • Always confirm accuracy by tightening the tolerance and comparing.

Next: Stiff Equations and Stability