fork download
  1. # Standing wave animation with labeled nodes and antinodes
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import matplotlib.animation as animation
  5.  
  6. # Parameters
  7. A = 1.0
  8. lam = 1.0
  9. k = 2 * np.pi / lam
  10. omega = 2 * np.pi * 2.0
  11. x = np.linspace(0, 2 * lam, 500)
  12.  
  13. # Standing wave function
  14. def y_standing(x, t):
  15. return 2 * A * np.sin(k * x) * np.cos(omega * t)
  16.  
  17. # Node and antinode positions
  18. n_nodes = np.arange(0, 5)
  19. x_nodes = n_nodes * lam / 2
  20. n_anti = np.arange(0, 4)
  21. x_anti = (2 * n_anti + 1) * lam / 4
  22.  
  23. # Set up figure
  24. fig, ax = plt.subplots(figsize=(10, 4))
  25. line, = ax.plot([], [], lw=2, color="blue")
  26. ax.set_xlim(0, 2 * lam)
  27. ax.set_ylim(-2.2 * A, 2.2 * A)
  28. ax.set_xlabel("x (m)")
  29. ax.set_ylabel("y (m)")
  30. ax.set_title("Standing Wave with Nodes and Antinodes")
  31. ax.grid(True)
  32.  
  33. # Mark nodes and antinodes
  34. ax.scatter(x_nodes, np.zeros_like(x_nodes), color="red", label="Nodes")
  35. ax.scatter(x_anti, np.zeros_like(x_anti), color="green", label="Antinodes")
  36. ax.legend(loc="upper right")
  37.  
  38. # Initialization
  39. def init():
  40. line.set_data([], [])
  41. return (line,)
  42.  
  43. # Update function
  44. def animate(i):
  45. t = i / 40
  46. y = y_standing(x, t)
  47. line.set_data(x, y)
  48. return (line,)
  49.  
  50. # Create animation
  51. ani = animation.FuncAnimation(fig, animate, init_func=init, frames=80, interval=60, blit=True)
  52. plt.close(fig)
  53. ani
  54.  
Success #stdin #stdout 1.13s 59184KB
stdin
4
stdout
Standard output is empty