0% found this document useful (0 votes)
30 views2 pages

Document 3

This document contains code for solving the Poisson equation on a triangulated domain using the finite element method. It defines functions for: 1) Refining the mesh and node/element data 2) Assembling the system matrix and right hand side vector 3) Solving the linear system and visualizing the solution The code loops over all elements to compute the element stiffness matrices and load vectors, assembles the global system, applies boundary conditions, and solves for the unknown nodal values. It then compares the solution to a reference solution.

Uploaded by

Thu Nguyen
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

Document 3

This document contains code for solving the Poisson equation on a triangulated domain using the finite element method. It defines functions for: 1) Refining the mesh and node/element data 2) Assembling the system matrix and right hand side vector 3) Solving the linear system and visualizing the solution The code loops over all elements to compute the element stiffness matrices and load vectors, assembles the global system, applies boundary conditions, and solves for the unknown nodal values. It then compares the solution to a reference solution.

Uploaded by

Thu Nguyen
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

function poisson_cr_edit

Omega_2
for j = 1:4
[c4n,n4e,Db,Nb] = red_refine(c4n,n4e,Db,Nb);
end
[element2edges, Db2edges, Nb2edges, nEdges] = edgeData(n4e,Db,Nb);
DiriNodes = unique(Db2edges);
freeNodes = setdiff([1:nEdges],DiriNodes);
I1 = zeros(size(n4e,1)*3,1);
I2 = zeros(size(n4e,1)*3,1);
EE = zeros(size(n4e,1)*3,1);
b = zeros(nEdges,1);
x = zeros(nEdges,1);
ctr = 1;
for j = 1 : size(n4e,1)
grads_T = [1,1,1;c4n(n4e(j,:),:)'] \ [0,0;eye(2)];
nc_grads_T = -2.*grads_T;
area_T = det([1,1,1;c4n(n4e(j,:),:)'])/2;
mp_T = sum(c4n(n4e(j,:),:))/3;
for m = 1 : 3
I = element2edges(j,m);
b(I) = b(I) + area_T * f(mp_T) / 3;
for n = 1 : 3
I1(ctr) = I; I2(ctr) = element2edges(j,n);
EE(ctr) = area_T * nc_grads_T(m,:) * nc_grads_T(n,:)';
ctr = ctr + 1;
end
end
end
A = sparse(I1,I2,EE);
for j = 1 : size(Nb,1)
length_E = norm(c4n(Nb(j,1),:) - c4n(Nb(j,2),:));
mp_E = (c4n(Nb(j,1),:) + c4n(Nb(j,2),:))/2;
I = Nb2edges(j);
b(I) = b(I) + length_E * g(mp_E);
end
x(DiriNodes) = u_D((c4n(Db(:,1),:) + c4n(Db(:,2),:))/2);
b = b - A * x;
x(freeNodes) = A(freeNodes,freeNodes) \ b(freeNodes);
show(c4n,n4e,element2edges,x);
pause
%%%%%%% for the comparison %%%%%%%
p_rt=poisson_rt_edit;
for j = 1 : size(n4e,1)
grads_T = [1,1,1;c4n(n4e(j,:),:)'] \ [0,0;eye(2)];
nc_grads_T = - 2.*grads_T;

area_T = det([1,1,1;c4n(n4e(j,:),:)'])/2;
mp_T = sum(c4n(n4e(j,:),:))/3;
a = p_rt( (1:3) + 3*(j-1));
b = x(element2edges(j,:));
difference1 = [a(1) a(2)] - b(1)*nc_grads_T(1,:) - b(2)*nc_grads_T(2,:) - b(3)*nc_grads_T(3,:)
difference2 = a(3) + f(mp_T)/2
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%
function val = g(x)
val = 1;
function val = u_D(x)
val = 0;
function val = f(x)
val = 20*pi^2* sin(2*pi*x(1)) * sin(4 * pi * x(2));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%
function show(c4n,n4e,element2edges,x)
clf; hold on; view(30,30);
for j = 1 : size(n4e,1)
u = [-1,1,1;1,-1,1;1,1,-1] * x(element2edges(j,:));
trisurf([1,2,3],c4n(n4e(j,:),1),c4n(n4e(j,:),2),u,1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%
function [element2edges, Db2edges, Nb2edges, nEdges] = edgeData(n4e,Db,Nb)
nE = size(n4e,1); nDb = size(Db,1); nNb = size(Nb,1);
Ind = [2 3, 3 1, 1 2];
edges = reshape( n4e(:,Ind)', 2, [])';
edges = [edges; Db; Nb];
[edge2nodes,foo,edgeIndex] = unique(sort(edges,2),'rows','first');
element2edges = reshape(edgeIndex(1:3*nE), 3, [])';
Db2edges = edgeIndex((3*nE + 1):(3*nE + nDb));
Nb2edges = edgeIndex((3*nE + nDb + 1):(3*nE + nDb + nNb));
nEdges = size(edge2nodes,1);

You might also like