import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
num_pendulums = 15
lengths = np.linspace(1, 2, num_pendulums)
g = 9.81
time = np.linspace(0, 20, 1000)
periods = 2 * np.pi * np.sqrt(lengths / g)
angles = [np.cos(2 * np.pi * time / T) for T in periods]
x_pos = [l * np.sin(angle) for l, angle in zip(lengths, angles)]
y_pos = [-l * np.cos(angle) for l, angle in zip(lengths, angles)]
fig, ax = plt.subplots(figsize=(8, 8))
ax.set_xlim(-2.5, 2.5)
ax.set_ylim(-2.5, 0.5)
ax.set_aspect('equal')
ax.set_title('Pendulum Wave Art Plot')
lines = [ax.plot([], [], 'o-', markersize=10)[0] for _ in range(num_pendulums)]
def animate(i):
for j, line in enumerate(lines):
line.set_data([0, x_pos[j][i]], [0, y_pos[j][i]])
return lines
ani = animation.FuncAnimation(fig, animate, frames=len(time), interval=30, blit=True)
plt.show()
#source code --> clcoding.com
Code Explanation:
0 Comments:
Post a Comment