9951 explained code solutions for 126 technologies


python-scipyHow do I use Python Scipy's Odeint function?


The odeint function from scipy.integrate is used to solve systems of ordinary differential equations (ODEs). It takes a function as an argument that defines the system of ODEs, and returns an array of solutions for the system.

Here is an example of using odeint to solve a simple system of two ODEs:

from scipy.integrate import odeint
import numpy as np

def system_of_ode(y, t):
    dydt = [-2 * y[0] + y[1],
            y[0] - 2 * y[1]]
    return dydt

t = np.linspace(0, 3, 100)
y0 = [1, 0]
y = odeint(system_of_ode, y0, t)
print(y)

This would output an array of solutions for the system of ODEs, where each row represents a solution at a given time step:

[[ 1.          0.        ]
 [ 0.97979798 -0.0399596 ]
 [ 0.960096   -0.07979192]
 ...
 [-0.30482636  0.97979798]
 [-0.0399596   0.960096  ]
 [ 0.          1.        ]]

The code can be broken down as follows:

  • from scipy.integrate import odeint: imports the odeint function from the scipy.integrate module.
  • def system_of_ode(y, t):: defines a function that takes two arguments, y and t, and returns an array of the derivatives of y with respect to t. In this case, the system of ODEs is [-2*y[0] + y[1], y[0] - 2*y[1]].
  • t = np.linspace(0, 3, 100): creates an array of 100 evenly spaced values between 0 and 3.
  • y0 = [1, 0]: sets the initial values for the system of ODEs.
  • y = odeint(system_of_ode, y0, t): calls the odeint function with the function that defines the system of ODEs, the initial values, and the array of time values as arguments.
  • print(y): prints the array of solutions for the system of ODEs.

For more information, see the scipy.integrate documentation.

Edit this code on GitHub