0% found this document useful (0 votes)
12 views

Document 1

This document contains code that performs a finite element simulation of a heat equation over time on a triangular mesh. It defines the mesh and boundary nodes, assembles the mass and stiffness matrices, applies an initial condition, then iteratively solves the system of equations over discrete time steps to update the solution. At each time step it applies forcing terms, solves the system, and visualizes the updated 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)
12 views

Document 1

This document contains code that performs a finite element simulation of a heat equation over time on a triangular mesh. It defines the mesh and boundary nodes, assembles the mass and stiffness matrices, applies an initial condition, then iteratively solves the system of equations over discrete time steps to update the solution. At each time step it applies forcing terms, solves the system, and visualizes the updated 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 P2FE2

c4n = [-1 -1; 1 -1; 1 1; -1 1];


n4e = [1 2 3; 3 4 1];
Db = [];
Nb = [1 2; 2 3; 3 4; 4 1];
for i = 1 : 5
[c4n,n4e,Db,Nb] = red_refine(c4n,n4e,Db,Nb);
end
DiriNodes = unique(Db);
freeNodes = setdiff(1:size(c4n,1),DiriNodes);
T=1/5;
delta_t=1/500;
eps=1/10;
M = sparse(size(c4n,1),size(c4n,1));
A = sparse(size(c4n,1),size(c4n,1));
u = sparse(size(c4n,1),1);
F = sparse(size(c4n,1),1);
MT=1/12*[2 1 1; 1 2 1; 1 1 2];
for j = 1 : size(n4e,1)
grads_T = [1,1,1;c4n(n4e(j,:),:)'] \ [0,0;eye(2)];
area_T = det([1,1,1;c4n(n4e(j,:),:)'])/2;
mp_T = sum(c4n(n4e(j,:),:))/3;
for m = 1 : 3
for n = 1 : 3
M(n4e(j,m),n4e(j,n)) = M(n4e(j,m),n4e(j,n)) + ...
area_T*MT(m,n);
A(n4e(j,m),n4e(j,n))=A(n4e(j,m),n4e(j,n)) + ...
grads_T(m,:)*grads_T(n,:)'*area_T;
end
end
end
for j = 1: size(c4n,1)
u(j) = u_0(c4n(j,:));
end
figure (1); clf; view(30,60); hold on;
axis([-1 1 -1 1 -1 1]);
trisurf(n4e,c4n(:,1),c4n(:,2),u);
%drawnow;
%pause;
for h = 1:T/delta_t
F = sparse(size(c4n,1),1);
for j = 1 : size(n4e,1)
area_T = det([1,1,1;c4n(n4e(j,:),:)'])/2;
nmp_T = sum(u(n4e(j,:)))/3;
for m = 1 : 3
F(n4e(j,m)) = F(n4e(j,m)) + (1/3) * area_T*f(nmp_T);
end
end

U=[1/delta_t*M A; A 1/(-eps)*M]\[1/delta_t*M*u; 1/(-eps*eps)*F];


u = U(1:size(c4n,1));
figure (1);clf; view(30,60); hold on;
axis([-1 1 -1 1 -1 1]);
trisurf(n4e,c4n(:,1),c4n(:,2),u);
drawnow;
%pause;
end
function val=f(x)
val=(x*x-1)*x;
function val=u_0(x)
if(x(1)-1/3)^2+(x(2)-1/3)^2 <=1/9
val=1;
elseif (x(1)+1/3)^2+(x(2)+1/3)^2 <=1/9
val=1;
else val=-1;
end

You might also like