Waves superpose: two waves in the same place add up. This gives interference (constructive and destructive), standing waves (nodes that don't move), Fourier decomposition (any wave is a sum of sines), and resonance (drive at the natural frequency and the amplitude explodes).
Superposition
When two waves overlap, the displacement at each point is the sum of the individual displacements. This is the superposition principle, and it holds for all linear wave equations. Sound waves in air, waves on a string, electromagnetic waves: they all superpose.
Scheme
; Superposition: y_total(x,t) = y1(x,t) + y2(x,t)
(define pi 3.14159265)
(define (wave1 x t)
(sin (* 2 pi (- (/ x 1.0) (* 2 t)))))
(define (wave2 x t)
(* 0.7 (sin (* 2 pi (- (/ x 0.8) (* 2.5 t))))))
(define (superposition x t)
(+ (wave1 x t) (wave2 x t)))
; Sample at t=0, different positions
(display "x=0.0: ") (display (round (* 100 (superposition 0.00)))) (newline)
(display "x=0.2: ") (display (round (* 100 (superposition 0.20)))) (newline)
(display "x=0.4: ") (display (round (* 100 (superposition 0.40)))) (newline)
(display "x=0.6: ") (display (round (* 100 (superposition 0.60)))) (newline)
(display "x=0.8: ") (display (round (* 100 (superposition 0.80)))) (newline)
(display "x=1.0: ") (display (round (* 100 (superposition 1.00)))) (newline)
(display "(values are 100x displacement)")
Interference
Constructive interference: waves in phase add to a bigger wave. Destructive interference: waves half a wavelength apart cancel. Noise-cancelling headphones emit a wave exactly out of phase with ambient noise.
Scheme
; Constructive and destructive interference
(define pi 3.14159265)
(define (wave amp freq phase x)
(* amp (sin (+ (* 2 pi freq x) phase))))
; Two waves, same frequency
(define A 1) (define f 1)
; Constructive: same phase
(define (constructive x)
(+ (wave A f 0 x) (wave A f 0 x)))
; Destructive: phase difference = pi
(define (destructive x)
(+ (wave A f 0 x) (wave A f pi x)))
(display "Constructive (in phase):") (newline)
(display " x=0.25: ") (display (constructive 0.25)) (newline)
(display "Destructive (out of phase):") (newline)
(display " x=0.25: ") (display (round (* 1000 (destructive 0.25)))) (display "/1000") (newline)
; Should be zero (or very close)
(display "Peak constructive amplitude: 2A = ")
(display (constructive 0.25))
Standing waves and resonance
A standing wave is two traveling waves going in opposite directions. The nodes never move; the antinodes oscillate maximally. A guitar string fixed at both ends can only sustain wavelengths that fit: lambda = 2L/n. These are the harmonics. Driving a system at its resonant frequency pumps energy in efficiently.
Scheme
; Standing waves on a string of length L; Allowed wavelengths: lambda_n = 2L/n; Frequencies: f_n = n * v / (2L)
(define L 0.65) ; guitar string, meters
(define v 250) ; wave speed, m/s
(define (harmonic-freq n) (/ (* n v) (* 2 L)))
(define (harmonic-wavelength n) (/ (* 2 L) n))
(display "Guitar string harmonics (L=0.65m, v=250m/s):") (newline)
(let loop ((n 1))
(if (> n 6) (newline)
(begin
(display " n=") (display n)
(display " lambda=") (display (round (* 1000 (harmonic-wavelength n)))) (display "mm")
(display " f=") (display (round (harmonic-freq n))) (display "Hz")
(newline)
(loop (+ n 1)))))
(display "Fundamental (n=1) is the pitch you hear.")
Python
# Standing wave harmonics
L, v = 0.65, 250# m, m/sprint("Guitar string harmonics:")
for n inrange(1, 7):
lam = 2*L/n
f = n*v/(2*L)
print(f" n={n} lambda={lam*1000:.0f}mm f={f:.0f}Hz")
Fourier decomposition
Any periodic wave can be written as a sum of sines and cosines. A square wave is an infinite series of odd harmonics. Fourier analysis is the reason we can separate instruments in a chord: each instrument has a different harmonic spectrum. The math is the same whether the wave is sound, light, or a quantum wavefunction.
Scheme
; Fourier series: square wave from odd harmonics; f(x) = (4/pi) * sum_{n=1,3,5,...} sin(n*x)/n
(define pi 3.14159265)
(define (square-wave-approx x n-terms)
(let loop ((n 1) (sum 0) (count 0))
(if (>= count n-terms) (* (/ 4 pi) sum)
(loop (+ n 2)
(+ sum (/ (sin (* n x)) n))
(+ count 1)))))
(define x (/ pi 4)) ; sample point
(display "Square wave at x=pi/4:") (newline)
(display " 1 term: ") (display (square-wave-approx x 1)) (newline)
(display " 3 terms: ") (display (square-wave-approx x 3)) (newline)
(display " 10 terms: ") (display (square-wave-approx x 10)) (newline)
(display " 50 terms: ") (display (square-wave-approx x 50)) (newline)
(display "Converges to 1.0 (the square wave value).")