Accelerating the pace of engineering and science

# Documentation

## Wave Equation on a Square Domain

This example shows how to solve the wave equation using the hyperbolic function in the Partial Differential Equation Toolbox™.

We solve the standard second-order wave equation

on a square domain with zero Dirichlet boundary conditions on left and right and zero Neumann boundary conditions on the top and bottom.

Problem Definition

The following variables will define our problem:

• g: A specification function that is used by initmesh. For more information, please see the documentation page for squarereg and pdegeom.

• c, a, f, d: The coefficients of the PDE.

g = @squareg;
c = 1;
a = 0;
f = 0;
d = 1;


Boundary Conditions

Plot the geometry and display the edge labels for use in the boundary condition definition.

figure
pdegplot(g, 'edgeLabels', 'on');
axis([-1.1 1.1 -1.1 1.1]);
title 'Geometry With Edge Labels Displayed';
% Create a pde entity for a PDE with a single dependent variable
numberOfPDE = 1;
pb = pde(numberOfPDE);
% Create a geometry entity
pg = pdeGeometryFromEdges(g);
% Edges 1 and 3 are free
bTopBot = pdeBoundaryConditions(pg.Edges([1 3]), 'g', 0);
% Solution is zero on edges 2 and 4
bLeftRight = pdeBoundaryConditions(pg.Edges([2 4]), 'u', 0);
pb.BoundaryConditions = [bTopBot bLeftRight];


Generate Mesh

[p,e,t] = initmesh(g);
figure
pdemesh(p,e,t);
axis equal


Generate Initial Conditions

The initial conditions:

• .

• .

This choice avoids putting energy into the higher vibration modes and permits a reasonable time step size.

x = p(1,:)';
y = p(2,:)';
u0 = atan(cos(pi/2*x));
ut0 = 3*sin(pi*x).*exp(sin(pi/2*y));


Define Time-Discretization

We want the solution at 31 points in time between 0 and 5.

n = 31;
tlist = linspace(0,5,n);


Find FEM Solution

uu = hyperbolic(u0,ut0,tlist,pb,p,e,t,c,a,f,d);

428 successful steps
62 failed attempts
982 function evaluations
1 partial derivatives
142 LU decompositions
981 solutions of linear systems


Animate FEM Solution

To speed up the plotting, we interpolate to a rectangular grid.

figure
delta = -1:0.1:1;
[uxy,tn,a2,a3] = tri2grid(p,t,uu(:,1),delta,delta);
gp = [tn;a2;a3];
umax = max(max(uu));
umin = min(min(uu));
for i = 1:n
pdeplot(p,e,t,'xydata',uu(:,i),'zdata',uu(:,i),'zstyle','continuous',...
'mesh','off','xygrid','on','gridparam',gp,'colorbar','off');
axis([-1 1 -1 1 umin umax]);
caxis([umin umax]);
M(i) = getframe;
end
movie(M,1);