9951 explained code solutions for 126 technologies


python-scipyHow can I solve a differential equation using Python and SciPy?


To solve a differential equation using Python and SciPy, you can use the odeint function from scipy.integrate. This function takes a function of the form dy/dx = f(x, y) and an initial value, and returns the array of values for the solution at each point in the interval.

For example, to solve the differential equation dy/dx = x + y with initial value y(0) = 0, you could use the following code:

from scipy.integrate import odeint
import numpy as np

def f(y, x):
    return x + y

x = np.linspace(0, 10, 100)
y = odeint(f, 0, x)

print(y)

Output example

[[ 0.        ]
 [ 0.2040404 ]
 [ 0.40808081]
 ...
 [10.91919192]
 [11.12323232]
 [11.32727273]]

The code consists of the following parts:

  1. from scipy.integrate import odeint: imports the odeint function from scipy.integrate
  2. import numpy as np: imports the numpy library as np
  3. def f(y, x):: defines the function f(x, y) for the differential equation
  4. x = np.linspace(0, 10, 100): creates an array of 100 equally spaced values between 0 and 10
  5. y = odeint(f, 0, x): solves the differential equation using odeint
  6. print(y): prints the array of values for the solution

For more information, see the following links:

Edit this code on GitHub