Having trouble with for loop function. Displays only last value.

My loop function "for el = 1:ne" only displays the last value of ke when I would like it to display a number equal to the number of elements I input into my LM.mat file. If I put 3 elements I should have 3 ke matricies.
Basically, I want to be able to view all ke values and the assembled value of Kg as output. So if I have 3 elements I want to view the output as seen below.
ke = [ x x x x
x x x x;
x x x x;
x x x x]
ke = [ x x x x
x x x x;
x x x x;
x x x x]
ke = [ x x x x
x x x x;
x x x x;
x x x x]
Kg = [ x x x x x x
x x x x x x;
x x x x x x;
x x x x x x;
x x x x x x;
x x x x x x]
The function is:
function [ke Kg] = AnalyzeTruss(Coord,LM,N,ndof,NNE)
% N = No. of elements (say 3)
% ndof = No. of degrees of freedom per node (say 2)
% NNE = No. of nodes per element (say 2)
% Coord.mat = (x-cord, y-coord)
% LM.mat = (node 1, node 2, A, E)
[ne,~] = size(LM);
for el = 1:ne
E = LM(el,3);
A = LM(el,4);
iNode = LM(el,1);
jNode = LM(el,2);
end
iNodeX = Coord(iNode,1);
iNodeY = Coord(iNode,2);
jNodeX = Coord(jNode,1);
jNodeY = Coord(jNode,2);
L = sqrt((jNodeX - iNodeX)^ 2 + (jNodeY - iNodeY)^ 2);
c = (jNodeX - iNodeX)/L;
s = (jNodeY - iNodeY)/L;
c2 = c^2;
s2 = s^2;
ke = (E*A/L)*[c2 c*s -c2 -c*s;
c*s s2 -c*s -s2;
-c2 -c*s c2 c*s;
-c*s -s2 c*s s2];
Kg=zeros(N*ndof);
for i=1:NNE
index_i=ndof*(i-1);
index_I=ndof*(LM(el,i)-1);
for j=1:NNE
index_j=ndof*(j-1);
index_J=ndof*(LM(el,j)-1);
for d1=1:ndof
for d2=1:ndof
Kg(index_I+d1,index_J+d2)=ke(index_i+d1,index_j+d2);
end
end
end
end
Thanks, if anyone can help me!
Cody Davis

Réponses (1)

Star Strider
Star Strider le 21 Fév 2014
I can’t run your code because I don’t have all the data, but it would seem that removing the semicolons from the ends of the statements for ke and Kg might do what you want. (Semicolons suppress the output of a specific line in MATLAB.)

Catégories

Question posée :

le 21 Fév 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by