%======== Simulation of a stretched membrane ===========
%       We solve the standard wave equation
%        d2u/dt2-div(grad(u))=0
%       on a square.
%       Problem definition
g='squareg'; % The unit square
b='squareb3'; % 0 on the left and right boundaries and
%               0 normal derivative on the top and bottom boundaries.
c=1;
a=0;
f=0;
d=1;

%       Mesh
[p,e,t]=initmesh('squareg');
%       The initial conditions:
%       u(0)=atan(cos(pi/2*x)) and
%       dudt(0)=3*sin(pi*x).*exp(sin(pi/2*y))
%       are chosen to avoid putting a lot of energy into
%       the higher vibration modes, thus permitting a reasonable
%       time step size.
x=p(1,:)';
y=p(2,:)';

%u0=atan(cos(pi/2*x*4));
%ut0=3*sin(pi*x*4).*exp(sin(pi/2*y*2));
u0=exp(-(x.^2+y.^2)/0.1);
ut0=zeros(size(u0));
%       We want the solution at 31 points in time between 0 and 5.
n=51;
tlist=linspace(0,6,n);

%       Solve hyperbolic problem
uu=hyperbolic(u0,ut0,tlist,b,p,e,t,c,a,f,d);


%       To speed up the plotting, we interpolate to a rectangular grid.
delta=-1:0.05:1;
[uxy,tn,a2,a3]=tri2grid(p,t,uu(:,1),delta,delta);
gp=[tn;a2;a3];

%       Make the animation
newplot;
M=moviein(n);
umax=max(max(uu));
umin=min(min(uu));

for i=1:n,
  if rem(i,10)==0,
    fprintf('%d ',i);
  end;
  pdeplot(p,e,t,'xydata',uu(:,i),'zdata',uu(:,i),'zstyle','continuous',...
          'mesh','off','xygrid','on','gridparam',gp,'colorbar','off',...
          'colormap','jet');
  axis([-1 1 -1 1 umin umax]); caxis([umin umax]);
  M(:,i)=getframe;
  if i==n,
   fprintf('done\n');
  end;
end


% Show movie
nfps=5;
movie(M,2,nfps);

%movie2avi(M,'membrane_pde.avi','quality',90);
