洛伦兹吸引力

这是使用mplot3d在3维空间中绘制Edward Lorenz 1963年的“确定性非周期流”的示例。

注意:因为这是一个简单的非线性ODE,使用SciPy的ode求解器会更容易完成,但这种方法仅取决于NumPy。

洛伦兹吸引力示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # This import registers the 3D projection, but is otherwise unused.
  4. from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
  5. def lorenz(x, y, z, s=10, r=28, b=2.667):
  6. '''
  7. Given:
  8. x, y, z: a point of interest in three dimensional space
  9. s, r, b: parameters defining the lorenz attractor
  10. Returns:
  11. x_dot, y_dot, z_dot: values of the lorenz attractor's partial
  12. derivatives at the point x, y, z
  13. '''
  14. x_dot = s*(y - x)
  15. y_dot = r*x - y - x*z
  16. z_dot = x*y - b*z
  17. return x_dot, y_dot, z_dot
  18. dt = 0.01
  19. num_steps = 10000
  20. # Need one more for the initial values
  21. xs = np.empty((num_steps + 1,))
  22. ys = np.empty((num_steps + 1,))
  23. zs = np.empty((num_steps + 1,))
  24. # Set initial values
  25. xs[0], ys[0], zs[0] = (0., 1., 1.05)
  26. # Step through "time", calculating the partial derivatives at the current point
  27. # and using them to estimate the next point
  28. for i in range(num_steps):
  29. x_dot, y_dot, z_dot = lorenz(xs[i], ys[i], zs[i])
  30. xs[i + 1] = xs[i] + (x_dot * dt)
  31. ys[i + 1] = ys[i] + (y_dot * dt)
  32. zs[i + 1] = zs[i] + (z_dot * dt)
  33. # Plot
  34. fig = plt.figure()
  35. ax = fig.gca(projection='3d')
  36. ax.plot(xs, ys, zs, lw=0.5)
  37. ax.set_xlabel("X Axis")
  38. ax.set_ylabel("Y Axis")
  39. ax.set_zlabel("Z Axis")
  40. ax.set_title("Lorenz Attractor")
  41. plt.show()

下载这个示例