Simulasi Gelombang n = f(x±ct)#
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
# Parameter simulasi
c = 1.0 # Kecepatan gelombang
L = 20.0 # Panjang domain (diperbesar)
T = 10.0 # Waktu simulasi
dx = 0.02 # Resolusi spasial
dt = 0.02 # Interval waktu
x0 = 5.0 # Posisi awal pusat gelombang
sigma = 0.5 # Lebar gelombang Gaussian
# Fungsi bentuk gelombang
def wave_form(x, t):
return np.exp(-((x - c*t - x0)**2)/(2*sigma**2))
# Membuat grid spasial
x = np.arange(0, L, dx)
total_frames = int(T/dt)
# Setup plot
fig, ax = plt.subplots(figsize=(10,5))
line, = ax.plot(x, wave_form(x, 0), lw=2, color='blue')
ax.set_xlim(0, L)
ax.set_ylim(-0.1, 1.1)
ax.set_xlabel('Posisi (x)')
ax.set_ylabel('Amplitudo')
ax.set_title('Gelombang Berjalan y = f(x - ct)')
ax.grid(True)
# Fungsi animasi
def animate(frame):
t = frame * dt # Waktu aktual
line.set_ydata(wave_form(x, t))
# line.set_color(plt.cm.viridis(frame%256/255)) # Animasi warna opsional
# Untuk animasi monokrom:
line.set_color('blue') # Hapus efek warna dinamis
return line,
# Mengurangi jumlah frame dengan frame_step
frame_step = 3 # Ubah nilai ini untuk kontrol ukuran file
ani = FuncAnimation(
fig,
animate,
frames=range(0, total_frames, frame_step),
interval=30,
blit=True
)
plt.close()
# Tampilkan animasi
HTML(ani.to_jshtml())
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
# Parameter simulasi
c = 1.0 # Kecepatan gelombang
L = 20.0 # Panjang domain
T = 10.0 # Waktu simulasi
dx = 0.02 # Resolusi spasial
dt = 0.02 # Interval waktu
x0 = 5.0 # Posisi awal pusat gelombang
sigma = 0.8 # Lebar gelombang Gaussian
# Fungsi turunan Gaussian orde pertama
def wave_form(x, t):
arg = (x - c*t - x0)
return (-arg/(sigma**2)) * np.exp(-(arg**2)/(2*sigma**2)) # Turunan pertama Gaussian
# Membuat grid spasial
x = np.arange(0, L, dx)
total_frames = int(T/dt)
# Setup plot
fig, ax = plt.subplots(figsize=(10,5))
line, = ax.plot(x, wave_form(x, 0), lw=2, color='royalblue')
ax.set_xlim(0, L)
ax.set_ylim(-1.5, 1.5) # Diadjust untuk amplitudo turunan Gaussian
ax.set_xlabel('Posisi (x)')
ax.set_ylabel('Amplitudo')
ax.set_title('Gelombang Berjalan Turunan Gaussian $\partial/\partial x(e^{-(x-ct)^2/2\sigma^2})$')
ax.grid(True)
# Fungsi animasi
def animate(frame):
t = frame * dt # Waktu aktual
y = wave_form(x, t)
# Update data dan efek visual
line.set_ydata(y)
line.set_color(plt.cm.coolwarm(0.5 + np.max(y)/3)) # Warna dinamis berdasarkan amplitudo
return line,
# Konfigurasi frame
frame_step = 2 # Kontrol kehalusan animasi
ani = FuncAnimation(
fig,
animate,
frames=range(0, total_frames, frame_step),
interval=30,
blit=True
)
plt.close()
# Tampilkan animasi
HTML(ani.to_jshtml())