scipy.integrate.odeint takes a function to integrate, initial values for the dependent variables (your u
, v
, an w
) and a grid of time values. Any extra arguments that your function needs (such as a
, b
and c
) are passed as args
.
The function you define should take a vector of values, say, X
, (which you can unpack to u
, v
and w
), the time point they correspond to, and any additional arguments, and should return the first derivatives of X
with respect to time at that time point.
Visualising the Lorenz attractor is a subject of one of the Matplotlib gallery examples.
import numpy as np
from scipy.integrate import odeint
a, b, c = 5, 0.9, 8.2
u0, v0, w0 = 0, 1, 2
def lorenz(X, t, a, b, c):
u, v, w = X
up = -a*(u - v)
vp = c*u - v - u*w
wp = -b*w + u*v
return up, vp, wp
t = np.linspace(0, 100, 10000)
f = odeint(lorenz, (u0, v0, w0), t, args=(a, b, c))
x, y, z = f.T
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(x, y, z)
ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.set_zlabel("Z Axis")
ax.set_title("Lorenz Attractor")
plt.show()
