The purpose of this project is to implement the finite element method for solving the Poisson equation in a general polygonal domain using the piecewise linear finite element.
% Generate mesh for the unit square
[node,elem] = squaremesh([0,1,0,1],0.25);
showmesh(node,elem);
% Generate mesh for the unit disk
[node,elem] = circlemesh(0,0,1,0.2);
showmesh(node,elem);
% Uniformly refine it to get a finer mesh
[node,elem] = uniformrefine(node,elem);
showmesh(node,elem);
Compare three ways of assembling the stiffness matrix discussed in Progamming of Finite Element Methods.
Record the time by
tic; assemblingstandard; toc;
tic; assemblingsparse; toc;
tic; assembling; toc;
Compare the computational time for different N
by applying the uniform refinement of the initial mesh several times. Try to test the assembling until N > 5*1e^4
.
Using three points quadrature (i.e. 3 middle points of a triangle) to compute the right hand side vector.
findboundary.m
to get all boundary nodes and edgesChoose a smooth solution, say $u = \sin(2\pi x)\cos(2\pi y)$, calculate the right hand side $f$ and boundary conditions for the unit square.
Use your subroutine to get an approximation and use showresult
to plot the mesh and the solution.
Use uniformrefine
to refine the mesh and compute a sequence of solutions.
Compute the error in H1 norm and L2 norm using getH1error
and
getL2error
.
Using the stiffness matrix to compute the error $\|\nabla(u_I - u_h)\|$, where $u_I$ is the nodal interpolation.
Use showrateh
to plot the rate of convergence of these error.
Test both Dirchlet and Neumann problems.
Code your subroutine in a general way such that you can solve the Poisson equation on a different mesh by changing the input arguments.
After you get the desireable results for the unit square, try to solve $-\Delta u = 1$ with constant Neumann boundary conditions on the unit disk. The exact solution can be found using the polar coordinate.