Python画洛伦兹曲线的代码
时间: 2023-11-21 12:01:47 浏览: 166
以下是Python的代码,用于绘制洛伦兹曲线:
```python
import numpy as np
import matplotlib.pyplot as plt
# 定义洛伦兹函数
def lorenz(x, y, z, s=10, r=28, b=2.667):
x_dot = s * (y - x)
y_dot = r * x - y - x * z
z_dot = x * y - b * z
return x_dot, y_dot, z_dot
dt = 0.01
stepCnt = 10000
# 初始值
xs = np.empty((stepCnt + 1,))
ys = np.empty((stepCnt + 1,))
zs = np.empty((stepCnt + 1,))
xs[0], ys[0], zs[0] = (0., 1., 1.05)
# 计算轨迹
for i in range(stepCnt):
x_dot, y_dot, z_dot = lorenz(xs[i], ys[i], zs[i])
xs[i + 1] = xs[i] + x_dot * dt
ys[i + 1] = ys[i] + y_dot * dt
zs[i + 1] = zs[i] + z_dot * dt
# 绘图
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(xs, ys, zs, lw=0.5)
ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.set_zlabel("Z Axis")
ax.set_title("Lorenz Attractor")
plt.show()
```
该代码绘制出洛伦兹曲线的三维图像。
阅读全文