Copyright 2023 The MathWorks, Inc.
Implementation of a simple numerical schemes for the wave equation.
Applying the second-order centered differences to approximate the derivatives,
It can be arrange to obtain
where
It is known that the scheme needs to satisfy
N = 101;
L = 4*pi;
x = linspace(0,L,N);
y = linspace(0,L,N);
% It has three data set; 1: past, 2: current, 3: future.
u = zeros(N,N,3);
s = 0.5;
% Gaussian Pulse
[X,Y] = meshgrid(x,y);
init_u = 2*exp(-2*(X-L/4).^2-2*(Y-L/3).^2);
u(:,:,1) = init_u;
u(:,:,2) = init_u;
% Plot the initial condition.
handle_surf = surf(X,Y,init_u);
axis([0,L,0,L,-2,2]);
xlabel('x');
ylabel('y');
title('2D Wave equation');
% Dirichet Boundary conditions
u(1,:,:) = 0;
u(end,:,:) = 0;
u(:,1,:) = 0;
u(:,end,:) = 0;
filename = 'wave2D.gif';
for ii=1:200
% disp(['at ii= ', num2str(ii)]);
% advance time
u(2:end-1,2:end-1,3) = s*(u(2:end-1,3:end,2)+u(2:end-1,1:end-2,2)) ...
+ s*(u(3:end,2:end-1,2)+u(1:end-2,2:end-1,2)) ...
+ 2*(1-2*s)*u(2:end-1,2:end-1,2) ...
- u(2:end-1,2:end-1,1);
u(:,:,1) = u(:,:,2);
u(:,:,2) = u(:,:,3);
% update the figure
handle_surf.ZData = u(:,:,2);
drawnow;
% capture frame to generate gif
frame = getframe(gcf);
im = frame2im(frame);
im = imresize(im,0.5);
[A,map] = rgb2ind(im,128);
if ii==1
imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',0.02);
else
imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.02);
end
end