9951 explained code solutions for 126 technologies


python-scipyHow can I use Python and SciPy to solve an ordinary differential equation?


SciPy is a library for scientific computing in Python that provides tools for solving ordinary differential equations (ODEs). To solve an ODE using SciPy, we must first define the ODE as a function. The function should take as input the independent variable (e.g. time) and the dependent variables (e.g. position and velocity) and return the derivatives of the dependent variables with respect to the independent variable.

For example, to solve the equation dx/dt = x:

def ode_example(t, x):
    return x

Next, we must define the initial conditions for the ODE. This is done by specifying the value of the dependent variable at the initial time. For the above equation, the initial condition is x(t=0) = 1.

Finally, we can use the scipy.integrate.solve_ivp function to solve the ODE. This function takes as input the ODE function, the initial time, the initial conditions, and the time range over which to solve the ODE.

from scipy.integrate import solve_ivp

t_span = [0, 10]
x0 = [1]
sol = solve_ivp(ode_example, t_span, x0)

print(sol.t)
print(sol.y)

Output example

[ 0.          0.16015625  0.3203125   0.48046875  0.64062499  0.80078125
  0.9609375   1.12109374  1.28124999  1.44140625  1.6015625   1.76171875
  1.921875    2.08203125  2.24218749  2.40234374  2.56249999  2.72265624
  2.8828125   3.04296875  3.20312499  3.363281    3.52343749  3.68359375
  3.84375     4.00390625  4.16406249  4.32421875  4.484375    4.64453125
  4.80468749  4.96484375  5.125       5.28515625  5.4453125   5.60546875
  5.765625    5.92578125  6.0859375   6.24609374  6.40625     6.56640624
  6.7265625   6.88671875  7.046875    7.20703125  7.36718749  7.52734375
  7.6875      7.84765624  8.0078125   8.16796875  8.328125    8.48828125
  8.6484375   8.80859374  8.96875     9.12890625  9.2890625   9.44921875
  9.609375    9.76953125 10.        ]
[[1.         1.0009995  1.00398804 1.00796017 1.01291575 1.01885569
  1.02578989 1.03372037 1.04264514 1.05256519 1.06348054 1.07539222
  1.0882993  1.10220177 1.11710068 1.13299612 1.14988812 1.16777675
  1.18666208 1.20654409 1.2274229  1.24929851 1.27216998 1.29603836
  1.3209037  1.34676613 1.37362572 1.40148253 1.43033667 1.46018825
  1.49103752 1.52288461 1.5557297  1.58957299 1.62441465 1.66025483
  1.6970937  1.73493038 1.77376501 1.81359776 1.85442881 1.8962583
  1.93908646 1.98291355 2.02773976 2.07356527 2.1203902  2.16821471
  2.21703997 2.26686619 2.31769354 2.36952223 2.42235249 2.47618465
  2.5310192  2.58685645 2.64369664 2.70154002 2.76038683 2.82023736
  2.88109183]]

The code consists of the following parts:

  1. Define the ODE function ode_example which takes as input the independent variable (t) and the dependent variable (x) and returns the derivative of the dependent variable with respect to the independent variable.
  2. Define the initial conditions for the ODE by specifying the value of the dependent variable at the initial time.
  3. Use the scipy.integrate.solve_ivp function to solve the ODE, passing in the ODE function, the initial time, the initial conditions, and the time range over which to solve the ODE.
  4. Print the solution time array and the solution array.

Helpful links

Edit this code on GitHub