% The leapfrog scheme on a square membrane. % % The two-dimensional version of Chapter 2's scheme, with the five-point % Laplacian in space: % % u(i,j,n+1) = 2 u(i,j,n) - u(i,j,n-1) % + lam^2 (u(i+1,j) + u(i-1,j) + u(i,j+1) + u(i,j-1) - 4 u(i,j)), % % on the unit square with u = 0 on the rim. It is stable exactly when % lam <= 1/sqrt(2), and there is no Courant number at which it is exact. % % u and uold are (N+1)-by-(N+1), rim included, at steps 0 and -1. The scheme % advances M steps and returns the displacement at node (ir, jr) after each % one, and the whole field, as columns u(:), after each of the first K. The % `scheme` argument is ignored here; Chapter 5's version of this function % offers three. function [y, U] = drum2d(u, uold, lam, M, scheme, ir, jr, K) n1 = size(u, 1); l2 = lam ^ 2; y = zeros(M, 1); U = zeros(n1 * n1, K); unew = zeros(n1, n1); for n = 1:M for j = 2:n1 - 1 for i = 2:n1 - 1 unew(i, j) = 2 * u(i, j) - uold(i, j) ... + l2 * (u(i + 1, j) + u(i - 1, j) + u(i, j + 1) + u(i, j - 1) - 4 * u(i, j)); end end uold = u; u = unew; y(n) = u(ir, jr); if n <= K U(:, n) = u(:); end end end