1% The leapfrog scheme on a square membrane.
2%
3% The two-dimensional version of Chapter 2's scheme, with the five-point
4% Laplacian in space:
5%
6% u(i,j,n+1) = 2 u(i,j,n) - u(i,j,n-1)
7% + lam^2 (u(i+1,j) + u(i-1,j) + u(i,j+1) + u(i,j-1) - 4 u(i,j)),
8%
9% on the unit square with u = 0 on the rim. It is stable exactly when
10% lam <= 1/sqrt(2), and there is no Courant number at which it is exact.
11%
12% u and uold are (N+1)-by-(N+1), rim included, at steps 0 and -1. The scheme
13% advances M steps and returns the displacement at node (ir, jr) after each
14% one, and the whole field, as columns u(:), after each of the first K. The
15% `scheme` argument is ignored here; Chapter 5's version of this function
16% offers three.
18function [y, U] = drum2d(u, uold, lam, M, scheme, ir, jr, K)
19n1 = size(u, 1);
20l2 = lam ^ 2;
21y = zeros(M, 1);
22U = zeros(n1 * n1, K);
23unew = zeros(n1, n1);
24for n = 1:M
25 for j = 2:n1 - 1
26 for i = 2:n1 - 1
27 unew(i, j) = 2 * u(i, j) - uold(i, j) ...
28 + l2 * (u(i + 1, j) + u(i - 1, j) + u(i, j + 1) + u(i, j - 1) - 4 * u(i, j));
29 end
30 end
31 uold = u;
32 u = unew;
33 y(n) = u(ir, jr);
34 if n <= K
35 U(:, n) = u(:);
36 end
37end
38end