In order to create the global stiffness matrix of a truss, I need to overlay the stiffness matrix to the global one. I started by creating a zero matrix
K=zeros(6,6); %Empty global stiffness matrix
And then I want to overlay a 4x4 matrix (Ke) to the correct position. For example:
Ke(1,1)->K(1,1)
Ke(1,2)->K(1,2)
Ke(1,3)->K(1,5)
Ke(1,4)->K(1,6)
Ke(2,1)->K(2,1)
Ke(2,2)->K(2,2)
Ke(2,3)->K(2,5)
Ke(2,4)->K(2,6)
etc.
I found a piece of code that would work but I'm afraid I can't understand why. Here it is:
sctr = [2*n1-1 2*n1 2*n2-1 2*n2];
K(sctr,sctr) = K(sctr,sctr) + Ke;
Following my above example n1, n2 should be n1=1 and n2=3. The n1 and n2 correspond to the start and finish node of the element. Of course this would be inside a loop that will calculate the stiffness matrix of each element and overlay it to the global matrix.