1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation from mpl_toolkits.mplot3d import Axes3D
class WaveSimulator: def __init__(self, length=10, nx=1000, dt=0.01, c=1): """ Initialize wave simulation parameters length: length of medium nx: number of spatial points dt: time step c: wave speed """ self.length = length self.nx = nx self.dt = dt self.c = c self.x = np.linspace(0, length, nx) self.dx = self.x[1] - self.x[0] if c * dt / self.dx > 1: raise ValueError("Stability condition not met. Decrease dt or increase dx.") def traveling_wave(self, t, wavelength, amplitude=1): """Generate traveling wave""" k = 2 * np.pi / wavelength omega = self.c * k return amplitude * np.sin(k * self.x - omega * t) def standing_wave(self, t, mode=1, amplitude=1): """Generate standing wave""" k = mode * np.pi / self.length omega = self.c * k return amplitude * np.sin(k * self.x) * np.cos(omega * t) def wave_packet(self, t, x0=0, sigma=1, k0=5): """Generate Gaussian wave packet""" x = self.x - (x0 + self.c * t) return np.exp(-(x**2)/(2*sigma**2)) * np.cos(k0 * x)
sim = WaveSimulator(length=10, nx=1000, dt=0.01, c=1)
t = np.linspace(0, 10, 100) X, T = np.meshgrid(sim.x, t)
traveling_wave = np.array([sim.traveling_wave(ti, wavelength=2) for ti in t]) standing_wave = np.array([sim.standing_wave(ti, mode=2) for ti in t]) wave_packet = np.array([sim.wave_packet(ti, x0=0, sigma=1) for ti in t])
fig = plt.figure(figsize=(15, 12))
ax1 = fig.add_subplot(311, projection='3d') ax1.plot_surface(X, T, traveling_wave, cmap='viridis') ax1.set_xlabel('Position (x)') ax1.set_ylabel('Time (t)') ax1.set_zlabel('Amplitude') ax1.set_title('Traveling Wave')
ax2 = fig.add_subplot(312, projection='3d') ax2.plot_surface(X, T, standing_wave, cmap='viridis') ax2.set_xlabel('Position (x)') ax2.set_ylabel('Time (t)') ax2.set_zlabel('Amplitude') ax2.set_title('Standing Wave (Mode 2)')
ax3 = fig.add_subplot(313, projection='3d') ax3.plot_surface(X, T, wave_packet, cmap='viridis') ax3.set_xlabel('Position (x)') ax3.set_ylabel('Time (t)') ax3.set_zlabel('Amplitude') ax3.set_title('Gaussian Wave Packet')
plt.tight_layout() plt.show()
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(10, 12))
line1, = ax1.plot([], [], 'b-', label='Traveling Wave') line2, = ax2.plot([], [], 'r-', label='Standing Wave') line3, = ax3.plot([], [], 'g-', label='Wave Packet')
for ax in [ax1, ax2, ax3]: ax.set_xlim(0, sim.length) ax.set_ylim(-1.5, 1.5) ax.grid(True) ax.legend()
ax1.set_title('Traveling Wave') ax2.set_title('Standing Wave') ax3.set_title('Wave Packet')
def animate(frame): line1.set_data(sim.x, traveling_wave[frame]) line2.set_data(sim.x, standing_wave[frame]) line3.set_data(sim.x, wave_packet[frame]) return line1, line2, line3
anim = FuncAnimation(fig, animate, frames=len(t), interval=50, blit=True) plt.tight_layout() plt.show()
|