2016-12-02 298 views
0

我試圖從兩個'for'循環中的兩個單獨值保存8個矩陣,以便我可以乘以T & kloc以找到值'k'。他們是兩個獨立的循環,這裏&都給出8個矩陣,但是當我嘗試將它們相乘時,它們的值作爲迭代中的最後一個值出現。另外,當我試圖在其中一個插入其中一個時,有一個大的'for'循環,我最終得到的T或kloc(無論哪個嵌套)的輸出矩陣多於8個。我會很感激任何幫助。謝謝。MATLAB保存'for'循環中的所有值

%-------------------------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 

回答

0

合併循環

兩個環路的長度是相同的,所以你可以使用一個單一的一個

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 

高維

您可以保存所有TCkloc到更高的維度,但您必須小心維度,您可能還需要預先初始化多維數組(張量)。事情是這樣的,注僞代碼

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; 

您也可以嘗試存儲在細胞中,其中每個單元包含獨立Tkloc等數據,但我不是細胞的大風扇,所以我不會詳述。