Two-Dimensional Wave Equation (With Animations)

This morning, Facebook showed me news posted by the Facebook profile of an indie videogame that I follow. In the news, the developers talked about improving the simulation of fluid flow in their videogame. They briefly talked about the mathematics involved, which was enough to make me want to simulate a simple wave with my computer.

I resorted to the wave equation $\ddot w=k^2\Delta w$. It is ridiculously abstract in this form, so I specialized it by requiring $w$ to be a function of (two-dimensional) space and time defined on $[0,1]^2\times[0,\infty)$ with initial conditions$$\begin{align}w(x,0,0)&=w(x,1,0)=w(0,y,0)=w(1,y,0)=0,\\w(x,y,0)&=\varphi(x,y),\text{ and}\\\dot w(x,y,0)&=0.\end{align}$$Simply put, $w$ lives in a square and is configured nicely at $t=0$.

The solution of this specialized wave equation is complicated and infeasible to realize with a computer, so I resorted to the theory of finite-difference methods. In particular, if $\{(x_i,y_j)\}_{i,j=0}^S$ and $\{t_n\}_{n=0}^\infty$ are uniform discretizations of $[0,1]^2$ and $[0,\infty)$ with meshes $\Delta s$ and $\Delta t$, then$$\begin{align}\ddot w(x_i,y_j,t_n)&\approx\frac{W_{i,j}^{n+1}-2W_{i,j}^n+W_{i,j}^{n-1}}{\Delta t^2},\\w_{xx}(x_i,y_j,t_n)&\approx\frac{W_{i+1,j}^n-2W_{i,j}^n+W_{i-1,j}^n}{\Delta s^2},\text{ and}\\w_{yy}(x_i,y_j,t_n)&\approx\frac{W_{i,j+1}^n-2W_{i,j}^n+W_{i,j-1}^n}{\Delta s^2},\end{align}$$where $W_{i,j}^{n}:=w(x_i,y_j,t_n)$. Note the further specializations that I made.

The wave equation then implies that$$W_{i,j}^{n+1}=2W_{i,j}^n-W_{i,j}^{n-1}+C^2(W_{i+1,j}^n+W_{i,j+1}^n-4W_{i,j}^n+W_{i-1,j}^n+W_{i,j-1}^n),$$where $C=k\Delta t/\Delta s$, and its initial conditions then imply that $W_{i,j}^{1}=W_{i,j}^{-1}$ and that $W_{i,j}^{0}=\varphi(x_i,y_j)$.

Since this idea requires nothing too fancy to implement, I implemented it in MATLAB due to its simple graphics. The following are a few results for some choices of $k$ and $\varphi$.

Centered Point Ripple
Off-Center Square Ripple
Off-Center Square Ripple (As a Surface)