integrate.solve_ivp integrates ordinary differential equations dy/dt = f(t, y)—population models, circuits, and control systems.
solve_ivp signature
fun(t, y)returns dy/dtt_span=(t0, tf)integration intervaly0initial state vectormethod='RK45'default Runge-Kuttadense_outputfor smooth interpolation
Exponential decay
import numpy as np
from scipy import integrate
def dydt(t, y):
return -0.5 * y
sol = integrate.solve_ivp(dydt, [0, 10], [1.0], dense_output=True)
print('t final:', sol.t[-1], 'y final:', sol.y[0, -1])
Stability note
Stiff ODEs may need implicit methods (Radau, BDF). Always validate against known solutions or conservation laws.
Important interview questions and answers
- Q: IVP meaning?
A: Initial Value Problem—state known at t₀, integrate forward. - Q: Stiff system?
A: Fast and slow time scales—explicit RK may need tiny steps; use stiff solvers.
Self-check
- What arguments does solve_ivp require?
- What does fun(t, y) return?
Pitfall: Stiff ODEs need implicit solvers—RK45 may crawl or fail with wrong step control.
Interview prep
- solve_ivp?
Modern ODE IVP solver—fun(t,y) returns dy/dt.
- Stiff?
Use implicit methods (Radau, BDF) when time scales differ wildly.