5.4.9 · D5Scientific Computing (Python)

Question bank — scipy.integrate — odeint, solve_ivp (RK45, DOP853), quad

1,225 words6 min readBack to topic

True or false — justify

quad returns just the numerical value of the integral.
False. It returns a tuple (value, estimated_absolute_error); ignoring the second element hides whether the answer is trustworthy.
Making t_eval denser makes solve_ivp more accurate.
False. t_eval only picks where the answer is reported; the solver's internal adaptive steps (and thus accuracy) are controlled by rtol/atol/method. See Numerical stability.
solve_ivp and odeint take the right-hand-side function with the same argument order.
False. solve_ivp wants f(t, y) (time first); odeint wants f(y, t) (state first). Swapping them silently computes nonsense.
Simpson's rule is only exact for parabolas (degree-2 polynomials).
False. It's derived by fitting a parabola but is exact up to degree 3 — the cubic error term integrates to zero by symmetry over .
A higher-order method like DOP853 always finishes faster than RK45.
False. Each DOP853 step is more expensive; it wins only on smooth problems where it can take huge steps. On a wiggly or short problem RK45 can be cheaper.
Gauss quadrature with points can only integrate polynomials up to degree .
False. By choosing both the sample locations and weights cleverly, -point Gaussian quadrature is exact up to degree — double what fixed-node rules manage.
Forward Euler and RK4 are fundamentally different ideas.
False (mostly). Both approximate the same exact integral from the Fundamental Theorem of Calculus; Euler uses one left-rectangle sample, RK4 blends several interior slope samples like Simpson does. See Runge-Kutta methods.
odeint returns its result in the same shape as solve_ivp.
False. odeint returns shape (len(t), n_states); solve_ivp returns sol.y of shape (n_states, len(t)) — the transpose.

Spot the error

sol = solve_ivp(lambda y, t: -2*y, [0,5], [3]) — what's wrong?
The lambda has arguments reversed. solve_ivp calls it as f(t, y), so y here is actually time and t is the state — the decay rate gets applied to the wrong variable.
y0 = 3.0 passed straight to solve_ivp as the initial condition.
The state must be a vector. Even a scalar problem needs [3.0]; a bare float can raise an error or be silently mishandled.
Reading sol.y[0] to get the first time point's full state.
Wrong axis. Rows are state components, columns are time points. sol.y[0] is the first component across all times; the state at time index k is sol.y[:, k].
Solving by passing the second-order equation directly to solve_ivp.
Solvers accept only first-order systems. You must introduce and return [y1, -w**2*y0]. See Harmonic oscillator.
Using method='RK45' on a chemical-kinetics system with rates spanning many orders of magnitude, then reporting a crash as a bug.
Not a bug — it's stiffness. Explicit RK needs an absurdly tiny for stability; switch to BDF/Radau/LSODA. See Stiff differential equations.
quad(f, 0, np.inf) for a function that oscillates forever without decaying.
The integral may not converge, and adaptive Riemann sums-style sampling can't detect infinitely many equal-height wiggles — quad may return a wrong value with a large reported error. Flag structure or split the domain.
Trusting the default rtol=1e-3 for a long-time oscillator simulation.
The phase drifts and amplitude decays visibly over many periods. Set rtol/atol explicitly (e.g. 1e-8) when accuracy over long times matters.

Why questions

Why do quad (area) and solve_ivp (trajectories) live in the same module?
Both are "integration" in the calculus sense — one accumulates area under a known curve, the other accumulates change from a known slope. Each replaces a continuous limit with clever finite sampling.
Why does Simpson's error depend on the 4th derivative, not the 3rd?
We matched a degree-2 (and freely degree-3) polynomial, so all error up to cubic vanishes; the first term the parabola cannot capture is the next even derivative, .
Why does RK45 evaluate the slope at extra trial points inside a step?
To estimate the little integral more accurately — sampling the slope mid-step (like Simpson samples the midpoint) captures curvature that a single endpoint slope misses.
Why does the name "45" appear in RK45?
It computes a 5th-order answer and an embedded 4th-order answer from the same slope evaluations; their difference estimates the local error, letting the solver grow or shrink automatically.
Why must a 2nd-order ODE be rewritten as a system before solving?
Every general-purpose solver's step formula is built for (first order); introducing the derivative as a new state variable converts any higher-order ODE into a first-order vector system.
Why do implicit methods (BDF, Radau) beat explicit RK on stiff systems?
Implicit methods stay stable with large regardless of the fast time-scale; explicit methods have a tiny stability region, forcing minuscule steps. See Numerical stability.
Why can n-point Gaussian quadrature reach degree ?
It has free choices ( node positions + weights); matching moments pins down polynomials of degree up to exactly.

Edge cases

What does quad do when has a sharp kink or discontinuity at a known point?
Adaptive sampling may waste effort or mis-estimate near the kink; pass points=[...] to force the algorithm to split the interval there and integrate each smooth piece.
What happens to forward Euler's per-step error as you shrink toward zero?
The per-step error falls like , but you take more steps, so the global error falls only like — halving merely halves total error, which is slow.
For , what is the trajectory if the initial value is exactly zero?
It stays identically zero forever — is an equilibrium; the slope vanishes there, a degenerate but valid trajectory the solver returns as a flat line.
If you request t_eval points outside the integration span [t0, tf], what happens?
Those points can't be reported (the solver never integrated there); requests outside the span are dropped or raise, so keep t_eval inside [t0, tf].
For an energy-conserving oscillator run for a very long time, why might even RK45 slowly lose energy?
Non-symplectic explicit methods introduce small numerical damping/growth each step that accumulates; tightening tolerances or using a high-order method delays but doesn't perfectly remove the drift.
What does quad report when it fails to converge to the requested tolerance?
It still returns a (value, err) tuple but with a large err (and often a warning); always inspect the error estimate rather than assuming success.