Functions and Stuff

FixTriangle[i_,{j_,k_,m_}] := Module[
{TempPositions,n},
TempPositions=Flatten[Position[{j,k,m},i]];
If[ Length[TempPositions]==1,n=TempPositions[[1]],
Return[Print["FixTriangle: Innapropriate Triangle Input"]]];
RotateLeft[{j,k,m},n-1]
]

TrianglePlot[{TriangleList_,NodeCoordinateList_}]:= Module[
{n1,n2,n3,p1,p2,p3,i},
Show[
Graphics[
{
Table[
{n1,n2,n3}=TriangleList[[i]];
p1=NodeCoordinateList[[n1]];
p2=NodeCoordinateList[[n2]];
p3=NodeCoordinateList[[n3]];
{
{
EdgeForm[{Thick,Black}],
Hue[i/Length[TriangleList]],
Polygon[ {p1,p2,p3}]
},
Text[(Subscript["Δ", i]),(p1+p2+p3)/3]
},
{i, Length[TriangleList]}
],
Table[
p1=NodeCoordinateList[[i]];
Text[(Subscript["p", i]),p1,
Background->White],
{i, Length[NodeCoordinateList]}
]
}
],
Frame->True
]
]

AdjacentNodes[TriangleList_,i_]:=Module[
{FilteredTriangleList},
 FilteredTriangleList=Select[TriangleList, MemberQ[#,i]&];
Union[ Flatten[FilteredTriangleList]]
]

FixTriangle[i_,{j_,k_,m_}] := Module[
{TempPositions,n},
TempPositions=Flatten[Position[{j,k,m},i]];
If[ Length[TempPositions]==1,n=TempPositions[[1]],
Return[Print["FixTriangle: Innapropriate Triangle Input"]]];
RotateLeft[{j,k,m},n-1]
]

Data and Stuff

TriangleList= {
{9,4,1},
{1,4,2},
{1,2,3},
{1,3,8},
{9,1,8},
{4,5,2},
{2,5,6},
{3,2,6},
{3,6,7},
{8,3,7}
};

NodeCoordinateList={
{2,2},
{4,2},
{2,4},
{3,0},
{7,0},
{4,3},
{0,7},
{0,3},
{0,0}
};

TrianglePlot[{TriangleList, NodeCoordinateList}]

aRow[i_,{TriangleList_, NodeCoordinateList_}]:=Module[
{js,ps,k,j,TempTriangleList},
js=AdjacentNodes[TriangleList,i];
ps=NodeCoordinateList[[js]];
TempTriangleList=Select[TriangleList, MemberQ[#,i]&];
TempTriangleList=Table[
TempTriangle=TempTriangleList[[k]];
TempTriangle=FixTriangle[i, TempTriangle],
{k, Length[TempTriangleList]}];
TempMatrixList = Table[
TempTriangle=TempTriangleList[[k]];
{p1,p2,p3}=NodeCoordinateList[[TempTriangle]];
Transpose[{p2-p1,p3-p1}],
{k, Length[TempTriangleList]}
];
TempGradDetList=Table[
A= TempMatrixList[[k]];
InvATranspose=Transpose[Inverse[A]];
{-InvATranspose.{1,1},-InvATranspose.{1,0},-InvATranspose.{0,1},Det[A]},
{k, Length[TempMatrixList]}
];
TempMatEntries=Table[
{grad1, grad2,grad3,det}=TempGradDetList[[k]];
0.5*det*{grad1.grad1,grad1.grad2,grad1.grad3},
{k, Length[TempGradDetList]}
];
Table[
TempPos=Position[TempTriangleList,j];
Sum[
{k1,k2}=TempPos[[n]];
TempMatEntries[[k1,k2]],
{n, Length[TempPos]}
],
{j, 1, 9}
]
]

A=Table[
aRow[i,{TriangleList, NodeCoordinateList}],
{i, 9}];
MatrixForm[A]