% Math 3150-1
%
% Sample code for problem 3.7.2:
%
% Square membrane with side 1, wave velocity c=1/pi and
% initial shape
u(x ,y ,0) = f(x , y) = sin( pi*x)*sin( pi*y)
% initial velocity
ut(x ,y ,0) = g(x , y) = sin( pi*x)
% number of terms in the expansion
N=30; M=30;
% Time steps and final time
nt=40; Tmax=pi ;
ts = linspace (0 ,Tmax, nt) ;
% whether to take snapshots for several times
take_snapshots=1;
nsnaps= 8;
snapbasename = 'p3_7_2';
snapcount = 0;
if(mod(nt , nsnaps )˜=0)
error( 'number of snapshots must divide number of time steps' ) ;
end;
% setup a grid to plot the function
Nx=20; Ny=20; % number of points in x and y directions
x=linspace (0 ,1 ,Nx) ; y=linspace (0 ,1 ,Ny) ;
[ xx , yy]=meshgrid(x , y) ;
for
it =1:length(ts),
t = ts(it) ;
ss = sin(pi*xx).*sin(pi*yy)*cos(sqrt(2)*t);
for n=1:N,
lambda1n = sqrt(1+n^2) ;
B1nstar = (1-(-1)^n) /(lambda1n*2*n*pi) ;
ss = ss + sin(pi*xx) .* sin(n*pi*yy) * B1nstar * sin(lambda1n*t ) ;
end;
% the rest of the loop handles plotting
mesh(xx , yy , ss , 'edgecolor' , 'k' , 'facecolor' , 'none  ) ;
axis ([0 1 0 1 -1 1]) ;
title ( sprintf ( 't = %d' , t ) ) ; xlabel( 'x' ) ; ylabel( 'y' ) ;
pause(0.2) ; % 0.2 is the time we pause
% take a snapshot if necessary
if(mod( it -1,nt/nsnaps )==0 & take_snapshots )
filename = sprintf ( '%s_%03d.png' ,snapbasename , snapcount ) ;
fprintf (['saving to file' filename '\n' ]) ;
print( '-dpng' , '-r50' , filename ) ;
snapcount=snapcount+1;
end;
end;
