Python: Butterfly effect graph 3D
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# Constants
sigma = 10.0
rho = 28.0
beta = 8.0 / 3.0
# Time parameters
dt = 0.01
num_steps = 10000
# Initial conditions
x0, y0, z0 = 0.0, 1.0, 1.05
# Arrays to store trajectories
x = np.empty(num_steps + 1)
y = np.empty(num_steps + 1)
z = np.empty(num_steps + 1)
x[0], y[0], z[0] = x0, y0, z0
# Lorenz system equations
def lorenz(x, y, z, sigma, rho, beta):
dx = sigma * (y - x)
dy = x * (rho - z) - y
dz = x * y - beta * z
return dx, dy, dz
# Integrate the Lorenz equations
for i in range(num_steps):
dx, dy, dz = lorenz(x[i], y[i], z[i], sigma, rho, beta)
x[i + 1] = x[i] + dx * dt
y[i + 1] = y[i] + dy * dt
z[i + 1] = z[i] + dz * dt
# Animation setup
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim(-20, 20)
ax.set_ylim(-30, 30)
ax.set_zlim(0, 50)
line, = ax.plot([], [], [], lw=2)
plt.title('Lorenz Attractor')
def init():
line.set_data(np.array([]), np.array([]))
line.set_3d_properties(np.array([]))
return line,
def update(frame):
line.set_data(x[:frame], y[:frame])
line.set_3d_properties(z[:frame])
return line,
ani = FuncAnimation(fig, update, frames=num_steps, init_func=init, blit=True, interval=1)
plt.show()
No comments:
Post a Comment