1.1 Derivation of the Wave Equation
We derive the one-dimensional wave equation from string vibrations.
📐 Theory
One-Dimensional Wave Equation:
\[\frac{\partial^2 u}{\partial t^2} = c^2 \frac{\partial^2 u}{\partial x^2}\]where \(u(x,t)\) is the displacement and \(c\) is the wave propagation velocity
Derivation: For a string with tension \(T\) and linear density \(\rho\), we have \(c = \sqrt{T/\rho}\)
💻 Code Example 1: Numerical simulation of wave equation (finite difference method)
# Requirements:
# - Python 3.9+
# - matplotlib>=3.7.0
# - numpy>=1.24.0, <2.0.0
"""
Example: Derivation:For a string with tension \(T\) and linear densit
Purpose: Demonstrate data visualization techniques
Target: Intermediate
Execution time: 2-5 seconds
Dependencies: None
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
# Parameters
L = 10.0 # string length
c = 1.0 # wave propagation velocity
T = 20.0 # time range
Nx = 200 # number of spatial grid points
Nt = 1000 # number of time steps
# Grid
x = np.linspace(0, L, Nx)
t = np.linspace(0, T, Nt)
dx = x[1] - x[0]
dt = t[1] - t[0]
# CFL condition verification
r = c * dt / dx # Courant number
print(f"Courant number r = {r:.3f} (stability condition: r ≤ 1)")
# Initial condition: triangular wave
def initial_displacement(x):
u = np.zeros_like(x)
peak = L / 2
width = L / 5
mask = np.abs(x - peak) < width
u[mask] = 1 - np.abs(x[mask] - peak) / width
return u
# Initialization
u = np.zeros((Nt, Nx))
u[0] = initial_displacement(x)
u[1] = u[0].copy() # initial velocity = 0
# finite difference method(explicit scheme)
for n in range(1, Nt-1):
for i in range(1, Nx-1):
u[n+1, i] = (2*(1-r**2)*u[n, i] - u[n-1, i] +
r**2*(u[n, i+1] + u[n, i-1]))
# Boundary conditions: u(0,t) = u(L,t) = 0
u[n+1, 0] = 0
u[n+1, -1] = 0
# Visualization
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
# Snapshots
times = [0, Nt//4, Nt//2, 3*Nt//4]
for idx, n in enumerate(times):
ax = axes[idx//2, idx%2]
ax.plot(x, u[n], 'b-', linewidth=2)
ax.axhline(0, color='gray', linewidth=0.5)
ax.grid(True, alpha=0.3)
ax.set_xlabel('Position x', fontsize=12)
ax.set_ylabel('Displacement u(x,t)', fontsize=12)
ax.set_title(f't = {t[n]:.2f}', fontsize=12)
ax.set_ylim(-1.2, 1.2)
plt.suptitle('Numerical solution of wave equation (finite difference method)', fontsize=14)
plt.tight_layout()
plt.show()
# Spacetime plot
plt.figure(figsize=(12, 6))
plt.contourf(x, t, u, levels=50, cmap='RdBu_r')
plt.colorbar(label='Displacement u(x,t)')
plt.xlabel('Position x', fontsize=12)
plt.ylabel('Time t', fontsize=12)
plt.title('Wave propagation (Spacetime diagram)', fontsize=14)
plt.tight_layout()
plt.show()
print("\nProperties of wave equation:")
print("- Waves propagate left and right, and reflect at boundaries")
print("- Energy is conserved")
print(f"- Propagation velocity: c = {c} m/s")