Bake, Shake or Break - and Other Applications For The FEM: A) 1D Quadrature
Bake, Shake or Break - and Other Applications For The FEM: A) 1D Quadrature
The programming project will be split into two parts. This is the first part and is mandatory for
all students. It is an introduction into the finite element method and is designed to build up a solid
code base for part 2, where you will actually solve larger, real life problems. For the second part
you will have to choose one of several tasks to implement.
1 Gaussian quadrature
At the heart of every finite element code, lies the evaluation of an integral. This integral may be of
varying complexity depending on the problem at hand, and many of these integrals does not even
have a known analytical solution. Some integrals are possible to solve analytically, but of such
computational complexity that it is impractical to do so. As such, one often refers to numerical
integration schemes to do the core integration. One popular integration scheme is the Gaussian
quadrature.
In one dimension the gauss quadrature takes the form
Z 1 Nq
X
g(z)dz ≈ ρq g(zq ),
−1 q=1
where Nq is the number of integration points, zq are the Gaussian quadrature points and ρq are the
associated Gaussian weights.
This extends to higher dimensions by
Z Nq
X
g(z)dz ≈ ρq g(z q ),
Ω̂ q=1
and specifying the vector quadrature points z q as well as integrating over a suitable reference
domain Ω̂ (i.e. squares or triangles in 2D, tetrahedra or cubes in 3D).
a) 1D quadrature
b) 2D quadrature
Using all numerical quadratures, it is important to first map the function to the referance domain.
In one dimension, this is the interval ζ ∈ [0, 1]. In higher dimensions, we often map to barycentric
coordinates (or area coordinates as they are known in 2D). The gauss points are then given as
triplets in this coordinate system. The area coordinates are defined by
A1
ζ1 =
A
A2
ζ2 =
A
A3
ζ3 =
A
where A1 , A2 and A3 are the area of the triangles depicted in figure 1 and A is the total area of the
triangle. Note that these do not form a linear independent basis as ζ1 + ζ2 + ζ3 = 1.
Nq (ζ1 , ζ2 , ζ3 ) ρ
1-point rule (1/3, 1/3, 1/3) 1
(1/2, 1/2, 0) 1/3
3-point rule (1/2, 0, 1/2) 1/3
(0, 1/2, 1/2) 1/3
(1/3, 1/3, 1/3) -9/16
4-point rule (3/5, 1/5, 1/5) 25/48
(1/5, 3/5, 1/5) 25/48
(1/5, 1/5, 3/5) 25/48
where Ω is the triangle defined by the corner points (1, 0), (3, 1) and (3, 2).
c) 3D quadrature
The extension of the barycentric coordinates to 3 dimensions and tetrahedral elements, should be
straightforward. The integration schemes can be found in the following table
Write a matlab function I = quadrature3D(p1,p2,p3,p4,Nq,g). With the following
arguments:
Nq (ζ1 , ζ2 , ζ3 , ζ4 ) ρ
1-point rule (1/4, 1/4, 1/4, 1/4) 1
(α, β, β, β) 1/4
4-point rule (β, α, β, β) 1/4
(β, β, α, β) 1/4
(β, β, β, α) 1/4
(1/4, 1/4, 1/4, 1/4) -4/5
(1/2, 1/6, 1/6, 1/6) 9/20
5-point rule (1/6, 1/2, 1/6, 1/6) 9/20
(1/6, 1/6, 1/2, 1/6) 9/20
(1/6, 1/6, 1/6, 1/2) 9/20
√ √
1 3 5 1 5
α= 4 + 20 , β= 4 − 20
(*) A function pointer in matlab is a variable which represents a function instead of the usual
numerical values. In its simplest form it is declared as
f = @(x) xˆ2 + 1
which would cause the variable f to contain a pointer to the function f (x) = x2 + 1. The function
can then be evaluated using one of two methods
y = f(4);
y = feval(f,4);
both of which should yield the same result y = 17. A function may take in several arguments,
i.e. f (x, y) = x2 + y 2 may be declared as
Provided that the actual function body is capable of vector or matrix operations, then the input
arguments may be of vector or matrix form. The syntax remains unchanged by this. You may also
use variables in the function declaration, i.e.
a = 2;
f = @(x) x*a
will result in a function f which is doubling its input argument (even if a is changed at a later
point).
2 Poisson in 2 dimensions
a) Analytical solution
Verify that the following expression is in fact a solution to the problem (1)
b) Weak formulation
a(u, v) = l(v), ∀v ∈ X.
c) Galerkin projection
Instead of searching for a solution u in the entire space X we are going to be looking for a
solution in a much smaller space Xh ⊂ X. Let Ω be discretized into M triangles such that our
computational domain is the union of all of these Ω = ∪M k=1 Kk . Each triangle Kk is then defined
by its three corner nodes xi . For each of these nodes there corresponds one basis function. The
space Xh is then defined by
Xh = {v ∈ X : v|Kk ∈ P1 (Kk ), 1 ≤ k ≤ M }
for which the basis functions {ϕi }ni=1 satisfy
A = [Aij ] = [a(ϕi , ϕj )]
u = [uih ]
f = [fi ] = [l(ϕi )].
d) Implementation
We are now going to actually solve the system (3). First we are going to take a look at the triangula-
tion {Kk }. From the webpage http://wiki.math.ntnu.no/tma4220/2013h/start
you may download the mesh generators. For organization purposes you might want to keep them
in a separate directory and see the matlab addpath command.
The function getDisk is generating the unit disk Ω. Plot at least three meshes of different
sizes using the mesh generated from this function. You may want to check the matlab function
trimesh or triplot.
e) Stiffness matrix
Build the stiffness matrix A. You may choose if you perform the integration analytically or by
Gaussian quadrature.
The matrix A should now be singular. Verify this in your code and explain why this is the case.
Build the right hand side vector f in the same manner as A. Here you might need to resort to
Gaussian quadrature.
g) Boundary conditions
Implement the homogeneous dirichlet boundary conditions. Describe what method you used for
this and how you did it.
h) Verification
Solve the system (3) and verify that you are getting (approximately) the same result as the analyt-
ical solution (2).
3 Neumann boundary conditions
with the source term f and exact solution u given as above, and g as
a) Boundary condition
b) Variational formulation
How does a(·, ·) and l(·) change with the introduction of Neumann boundary conditions?
c) Gauss quadrature
The neumann boundary condition is given as an integral and should be evaluated using Gaussian
quadrature. Modify your quadrature methods from task 1 to solve line integrals in two dimensions,
i.e. the method signature in I = quadrature1D(a,b,Nq,g) should change to a ∈ R2 and
b ∈ R2 .
d) Implementation
Change your code from task 2 to solve this new boundary value problem. How does your solution
in the interior compare to the one you got in task 2? How does your solution at the boundary
compare?
4 Moving into 3 dimensions
a) The Poisson in 3d
b) Volume visualization
Plot the domain Ω (i.e. the ball). Note that you will not be required to plot every element, as most
will be hidden on the inside of the domain. See the matlab function TriRep or tetramesh for
functionality relating to this.
Plot your solution using isosurfaces. Note that the matlab function isosurface requires your
data to be structured, which it currently is not. You will have to post process the data to get it on
the desired form. Read up on TriScatteredInterp for this.
c) GLview (optional)
GLview is a dedicated commercial visualising program to display the results from finite element
analysis. It is available to all NTNU students through progdist. While it is possible to display 2D-
results in GLview, the advantage of using a dedicated visualiser really is apparent when trying to
draw 3D volumetric results.
Download and install GLview inova from progdist and write a .vtf output file to view your
results. See the attached writeVTF function in the grids.zip file.
Change the problem definition to use Neumann boundary conditions for the top half of the sphere
(i.e. z > 0) and Dirichlet for the bottom part. The neumann condition is given in (5).