Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Computer programming assignment III

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Ω,
Ω Ω

for all admissible v.


Using piecewise linear functions for approximating u on triangles, we can set up the approximation
directly. Assume that the nodes are at (x1 , y1 ), (x2 , y2 ), (x3 , y3 ). We have that

u(x, y) ≈ a + bx + cy,

We now want to write, on each element (with local node numbering),


3
X
u(x, y) ≈ uh = ϕi (x, y)ai .
i=1

Clearly, for determining ϕ1 we must (since a1 ≈ u(x1 , y1 )) use

a + bx1 + cy1 = 1, a + bx2 + cy3 = 0, a + bx3 + cy3 = 0,

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=f-S(:,pres)*val; % change the right-hand side


free=setdiff([1:neq],pres); % interior, "free", equations
f=f(free);S=S(free,free); % reduce the system
u=S\f; % solve for free variables
v=zeros(neq,1);v(pres)=val;v(free)=u; % fill global vector v

1.2 Model problem


We want to solve a heat conduction problem with known exact solution. With k = 1 and

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Ω

the root mean square norm of the error. So we assume

ku − uh k = Chp

and compute p.

The assignment must be reported individually in writing in the following way:


1. A short description of how you have made the implementation, including all of your MATLAB code.
2. Graphical output of the solved problems, with plots of the approximate solution uh ≈ u.

3. The computation of the convergence rate p.

You might also like