0
votes

I am trying to save the 8 matrices from two separate values in two 'for' loops so that I can multiply T & kloc to find a value 'k'. They are two separate loops here & both give 8 matrices, but when I try to multiply them their values come out as the last value from the iterations. Also, when I attempt to insert one of them within the other & have one big 'for' loop I end up with a lot more than 8 outputted matrices for either T or kloc (whichever is nested). I'd appreciate any help. Thanks.

%-------------------------Givens/Constants--------------------------%

E = 10200; %ksi
b = 1; %in
h = 0.25; %in
I = (b*h^3)/12; %in^4
A = b*h;


%%Lengths &Angles
addangle = atand(8/16.25);
theta1 = 0;
theta2 = atand(16.25/8);
theta3 = 2*addangle + 63.7886;
theta4 = 90;
L1=8;
L3 = 16.25/sind(63.7886);
L2 = 16;
L4 = 16.25;
%--------------------------------------------------------------------% 



%%Local stiffness matrices

%The angle between 8 beams of structure from the positive x-axis
%(In order of member ID 1:8)

for theta=[theta1,theta1,theta1,theta1,theta3,theta2,theta3,theta4]

ct = cosd(theta);
st = sind(theta);

T =[ct,st,0,0,0,0; -st,ct,0,0,0,0;...
    0,0,1,0,0,0;0,0,0,ct,st,0; 0,0,0,-st,ct,0;0,0,0,0,0,1];
end


%Length of 8 seperate beams in truss structure
%(In order of member ID 1:8)

for L=[L3,L1,L1,L3,L2,L2,L2,L4]    
C = (E*I)/(L^3);
line1 = [(A*L^2)/I,0,0,-(A*L^2)/I,0,0];
line2 = [0,12,6*L,0,-12,6*L];
line3 = [0,6*L,4*L^2,0,-6*L,2*L^2];
line4 = [-(A*L^2)/I,0,0,(A*L^2)/I,0,0];
line5 = [0,-12,-6*L,0,12,-6*L];
line6 = [0,6*L,2*L^2,0,-6*L,4*L^2];

kloc = C*[line1;line2;line3;line4;line5;line6];
end

%Need to calculate 8 matrices of 'k' where...
% k = TT*kloc*T
1

1 Answers

0
votes

Merging loops

Both loops are of the same length so you can use a single one

theta=[theta1,theta1,theta1,theta1,theta3,theta2,theta3,theta4];
L=[L3,L1,L1,L3,L2,L2,L2,L4];
k = zeros(1,length(L));
for id=1:length(theta) % or length(L)
    theta_loop = theta(id);
    L_loop = L(id);
    % calculate T
    % calculate C and kloc
    k(id) = TT*kloc*T;
end

Higher dimensional

You can save all T, C and kloc into higher dimensions, but you have to be careful about the dimensions, you might also want to pre-initialize the multi-dimensional arrays (tensors) in advance. Something like this, note pseudo code

for id=1:length(theta)
    theta_loop = theta(id);
    % calculate T
    T(:,:,id) = T;
end
for id=1:length(L)
    L_loop = L(id);
    % calculate C and kloc
    kloc(:,:,id) = kloc;
end
k = kloc*T;

You can also try to store the data in cells, where each cell contains separate T, kloc etc. But I am not a big fan of cells so I will not go into details.