Professional Documents
Culture Documents
Labb 3
Labb 3
1.1 Information
The programming assignment consists of implementing the finite element method for a heat conduction
problem in two dimensions on triangular meshes.
We want to solve
−∇ · (k∇u) = f in Ω, (1.1)
with u known on ∂Ω. In weak form we then seek u such that
Z Z
k ∇u · ∇v dΩ = f v dΩ,
Ω Ω
u(x, y) ≈ a + bx + cy,
or
1 x1 y1 a 1
1 x2 y2 b = 0
1 x3 y3 c 0
leading to
a x2 y3 − y2 x3
1
b = y2 − y3 ,
2∆
c x3 − x2
where ∆ is the area of the triangle, and
1
ϕ1 = (x2 y3 − x3 y2 + (y2 − y3 ) x + (x3 − x2 ) y) .
2∆
In the same way
1
ϕ2 = (x3 y1 − x1 y3 + (y3 − y1 ) x + (x1 − x3 ) y) ,
2∆
1
ϕ3 = (x1 y2 − x2 y1 + (y1 − y2 ) x + (x2 − x1 ) y) .
2∆
Derivatives can now be taken directly with respect to x and y.
• Download and use meshtri, which creates a mesh of triangular elements on a rectangle. Called as
[nodes,xnod,ynod]=meshtri(hv,xv,yv); where hv(2) is the meshsize in the x− and
y−directions, xv(2) and yv(2) are the start and endpoint in the x− and y− directions, respec-
tively
• Create a matrix S of size (number of nodes)2 —use spalloc—and a right hand side f of size
number of nodes.
1
• Loop over all of the elements.
• Use one point integration to integrate element loads and stiffness matrices.
• Assemble to S and f .
• Handling prescribed boundary data: assuming that the prescribed equation numbers are stored in a
vector pres, and the corresponding prescribed values are stored in a vector val, we can use index-
ing to reduce the system of equations S*v = f of size neq×neq as follows:
f = 2x(1 − x) + 2y(1 − y)
on the domain Ω = (0, 1) × (0, 1) with u = 0 on the boundary, we have the exact solution
u = x(1 − x)y(1 − y)
The assignment is to compute a piecewise linear approximation uh of u and check convergence for the
quantity
Z 1/2
ku − uh k := (u − uh )2 dΩ
Ω
ku − uh k = Chp
and compute p.