Simulating the Wave Equation


Brandon Pelfrey

What's this all about?

  • Some wave equation theory
  • Some basic simulation theory
  • How to actually code all this stuff

Let's Talk Waves

Waves are everywhere


A little hard to define precisely, but waves generally...


  • Transmit energy (light, sound, radio)

  • Can reflect off of certain boundaries

  • Can be absorbed by certain boundaries

  • Can interfere with each other

Wave Equation


$$\frac {\partial^2 u} {\partial t^2}$$ $$=$$ $$c \; \nabla^2 u$$
Acceleration "Curvature"
Great. How can I code upside-down triangles?

Simulating Things

How can we solve these equations in a discrete way?

Finite Difference Method


  1. Sample function at some regular interval
  2. Figure out what the original equations look like in this discretized form

Sampling the function

(Function)

Sampling the function

(Function, sampled)

Computing Derivatives of a Sampled Function

$$f(x_i+h)$$ $$=$$ $$f(x_i) + h \frac{df}{dx}\Big|_{x_i} + \mathcal{O}(h^2)$$
$$f(x_{i+1})$$ $$=$$ $$f(x_i) + h f'({x_i})$$
$$f'(x_i)$$ $$=$$ $$\frac{f(x_{i+1}) - f(x_{i})} {h}$$

Lazy Notation

$$f(x_i+k h)$$ $$= f(x_{i+k})$$ $$= f_{i+k}$$

How about a second derivative?

$$f_{i+1}$$ $$=$$ $$f_i + h f'_i + \frac{h^2}{2} f''_i + \mathcal{O}(h^3)$$
$$f_{i-1}$$ $$=$$ $$f_i - h f'_i + \frac{h^2}{2} f''_i + \mathcal{O}(h^3)$$
$$f_{i+1} + f_{i-1}$$ $$=$$ $$2 f_i + h^2 f''_i + \mathcal{O}(h^3)$$
$$\frac{f_{i-1} - 2f_i + f_{i+1}}{h^2}$$ $$\approx$$ $$f''_i$$

The Discrete Laplacian

$$\nabla^2 f(x,y) = \frac{\partial^2 f}{\partial x^2} + \frac{\partial^2 f}{\partial y^2}$$

$$\nabla^2 f_{i,j} \approx \frac{f_{i-1,j}-2f_{i,j}+f_{i+1,j}}{h^2} + \frac{f_{i,j-1}-2f_{i,j}+f_{i,j+1}}{h^2}$$

$$\nabla^2 f_{i,j} \approx \frac{-4f_{i,j}+f_{i-1,j}+f_{i+1,j} + f_{i,j-1} + f_{i,j+1}}{h^2}$$

Bringing in Time

Acceleration means we need \(\frac{\partial^2 f}{\partial t^2}\)!


$$\frac {\partial f_{i,j}^n}{\partial t^2} = \frac{f_{i,j}^{n-1}-2f_{i,j}^{n}+f_{i,j}^{n+1}}{\delta t^2}$$

$$f^{new} = 2 f^{current} - f^{previous} + \delta t^2 \frac{\partial^2 f}{\partial t^2}$$

All Together Now

$$\frac {\partial^2 u} {\partial t^2}$$ $$=$$ $$c \; \nabla^2 u$$
$$u_{i,j}^{n+1}$$ $$=$$ $$2 u_{i,j}^{n} - u_{i,j}^{n-1} + \frac{c \delta t^2}{h^2}(-4 u_{i,j}^{n} + u_{i\pm1,j\pm1}^{n})$$

Thanks for watching!