I have solved the 2D Poisson equation using finite element method with simplex triangular element in MATLAB.
First, I generated the triangular mesh using pdetool
in Matlab and it gives me three matrices: p
(position of each node), t
(element with three vertices) and e
. Then, I derived the local stiffness matrix like this,
for I=1:i_elems
conec(i_elem,1)=t(1,i_elem);
conec(i_elem,2)=t(2,i_elem);
conec(i_elem,3)=t(3,i_elem);
node_1=conec(i_elem,1);
node_2=conec(i_elem,2);
node_3=conec(i_elem,3);
NN_e(1,1)=(1/6) * elem_area(i_elem);
NN_e(1,2)=(1/12)* elem_area(i_elem);
NN_e(1,3)=(1/12)* elem_area(i_elem);
NN_e(2,1)=(1/12)* elem_area(i_elem);
NN_e(2,2)=(1/6) * elem_area(i_elem);
NN_e(2,3)=(1/12)* elem_area(i_elem);
NN_e(3,1)=(1/12)* elem_area(i_elem);
NN_e(3,2)=(1/12)* elem_area(i_elem);
NN_e(3,3)=(1/6) * elem_area(i_elem);
% and then I assembled that like this,
NN(node_1,node_1)=NN(node_1,node_1)+NN_e(1,1);
NN(node_1,node_2)=NN(node_1,node_2)+NN_e(1,2);
NN(node_1,node_3)=NN(node_1,node_3)+NN_e(1,3);
NN(node_2,node_1)=NN(node_2,node_1)+NN_e(2,1);
NN(node_2,node_2)=NN(node_2,node_2)+NN_e(2,2);
NN(node_2,node_3)=NN(node_2,node_3)+NN_e(2,3);
NN(node_3,node_1)=NN(node_3,node_1)+NN_e(3,1);
NN(node_3,node_2)=NN(node_3,node_2)+NN_e(3,2);
NN(node_3,node_3)=NN(node_3,node_3)+NN_e(3,3);
end
NN
matrix will be a sparse matrix. I just want to use the c.s.r technique and make the assembling process more efficient but I don't know how to assemble the global stiffness matrix in this way. I would be grateful if someone could help me.