odeint() takes a function func(y, t) describing the derivative dy/dt at any given state y and time t, an initial condition y0, and an array of time points t at which you want the solution evaluated, then numerically integrates the ODE forward from the initial condition, returning the solution's value at every requested time point. It works for systems of multiple coupled equations too, where y0 is an array of initial values and func returns an array of derivatives, one per equation, which is how coupled physical systems, like predator-prey population models, or projectile motion with multiple state variables, are typically solved numerically.
1Understanding integrate.odeint()
odeint() takes a function func(y, t) describing the derivative dy/dt at any given state y and time t, an initial condition y0, and an array of time points t at which you want the solution evaluated, then numerically integrates the ODE forward from the initial condition, returning the solution's value at every requested time point. It works for systems of multiple coupled equations too, where y0 is an array of initial values and func returns an array of derivatives, one per equation, which is how coupled physical systems, like predator-prey population models, or projectile motion with multiple state variables, are typically solved numerically.
odeint() only returns the solution's values at the specific time points you request in t — it doesn't compute or return anything about the solution's behavior between those requested points, so pass a sufficiently fine-grained t array if you need a smooth-looking curve or accurate values at intermediate times.
from scipy.integrate import odeint
import numpy as np
def decay(y, t):
return -0.25 * y
t = np.linspace(0, 5, 6)
solution = odeint(decay, 100, t)
print(np.round(solution.flatten(), 2))2Practical Example
Here is a real-world application of integrate.odeint() showing how it is used in production SciPy code.
from scipy.integrate import odeint
import numpy as np
def harmonic(y, t):
return [y[1], -y[0]]
t = np.linspace(0, np.pi, 3)
solution = odeint(harmonic, [1, 0], t)
print(np.round(solution, 2))3Best Practices
Follow these guidelines when working with integrate.odeint():
1. Pass a sufficiently fine-grained array of time points when you need a smooth solution curve, since odeint() only returns values exactly at the times you request
2. Structure func to return an array of derivatives for a system of coupled equations, with y0 as a matching array of initial conditions, rather than trying to solve coupled equations one at a time
3. Verify the solution's behavior makes physical/mathematical sense, like checking conservation laws or known limiting behavior, as a sanity check, rather than trusting a numerical solver blindly
Tip: odeint() only returns the solution's values at the specific time points you request in t — it doesn't compute or return anything about the solution's behavior between those requested points, so pass a sufficiently fine-grained t array if you need a smooth-looking curve or accurate values at intermediate times.
from scipy.integrate import odeint
import numpy as np
def decay(y, t):
return -0.25 * y
t = np.linspace(0, 5, 6)
solution = odeint(decay, 100, t)
print(np.round(solution.flatten(), 2))