🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

integrate.odeint()

AI & DATA SCIENCE // integrate-odeint

scipy.integrate.odeint() numerically solves a system of ordinary differential equations (ODEs) given an initial condition, computing the solution's value at a series of specified time points.

Syntax

scipy.integrate.odeint(func, y0, t)

Deep Dive Course

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.

editor.html
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))
localhost:3000

2Practical Example

Here is a real-world application of integrate.odeint() showing how it is used in production SciPy code.

editor.html
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))
localhost:3000

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.

editor.html
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))
localhost:3000

Examples

Example 01Basic Usage
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))
Example 02Advanced Example
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))

Best Practices

  • 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
  • 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
  • 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

Interview Question

Why does odeint() only give you the solution's value at the specific time points you request, rather than a continuous function you can evaluate anywhere?

Hint: Think about what a numerical ODE solver actually computes internally as it steps forward through time.

odeint() works by taking discrete numerical steps forward through time, computing an approximation of the solution's value at each step based on the derivative function and the solution's value at the previous step — it never derives an actual closed-form, continuous mathematical expression for the solution, since that's frequently impossible for anything but the simplest ODEs. It internally takes whatever step sizes its adaptive algorithm determines are necessary for accuracy, which don't necessarily correspond to the exact points you requested, so it reports back the solution's approximated value specifically at the time points you asked for, with nothing about the true underlying continuous behavior between them.

Exercises

MediumPractice using integrate.odeint() in a real scenario.
View Solution
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))

Frequently Asked Questions

Why does odeint() only give you the solution's value at the specific time points you request, rather than a continuous function you can evaluate anywhere?

odeint() works by taking discrete numerical steps forward through time, computing an approximation of the solution's value at each step based on the derivative function and the solution's value at the previous step — it never derives an actual closed-form, continuous mathematical expression for the solution, since that's frequently impossible for anything but the simplest ODEs. It internally takes whatever step sizes its adaptive algorithm determines are necessary for accuracy, which don't necessarily correspond to the exact points you requested, so it reports back the solution's approximated value specifically at the time points you asked for, with nothing about the true underlying continuous behavior between them.

Related Functions

integrate-quadintegrate-dblquadnp-linspace