2013-10-06 119 views
0

如何將此代碼轉換爲for循環,以便我可以添加更多的力量和時刻?這是一個很多的代碼,它基本上是計算單位矢量,並找到在x方向,y方向和z方向上的力的總和,並對這些時刻做同樣的操作。轉換爲for循環

這只是一個特定的情況,所以我想把它放在一個for循環,所以當新的力量應用時,他們將被解釋。

任何幫助或推動正確的方向將是一個巨大的幫助!提前致謝。

load ('inputdata.mat') 

A= zeros(6,6); 
B= zeros (6,1); 


% Calculate Force in x,y,z direction, sums them and puts them in the B 
% vector positions 1,2,3 

NF1=((ForceDir(1,2))^2+(ForceDir(1,3))^2+(ForceDir(1,4))^2)^(1/2); 

F1i=(ForceDir(1,2)/NF1)* ForceDir(1,1); 

F1j=(ForceDir(1,3)/NF1)* ForceDir(1,1); 

F1k=(ForceDir(1,4)/NF1)*ForceDir(1,1); 

NF2= ((ForceDir(2,2))^2+(ForceDir(2,3))^2+(ForceDir(2,4))^2)^(1/2); 

F2i=(ForceDir(2,2)/NF2)* ForceDir(2,1); 

F2j=(ForceDir(2,3)/NF2)* ForceDir(2,1); 

F2k=(ForceDir(2,4)/NF2)* ForceDir(2,1); 

B(1,1)= F1i+F2i; %External Force in x 

B(2,1)= F1j+F2j; %External Force in y 

B(3,1)= F1k+F2k; %External Force in z 

% Calculate Moments in x,y,z direction and puts them in B vector 
% position 3,4,5 

unitvectorForce1=[F1i F1j F1k]; 

MomentByForce1= cross(ForceCoor(1,:),unitvectorForce1); 

unitvectorForce2= [F2i F2j F2k]; 

MomentByForce2= cross(ForceCoor(2,:),unitvectorForce2); 

MomentNormal=((DirMoment(1,2))^2+(DirMoment(1,3))^2+(DirMoment(1,4))^2)^(1/2); 

M1i= (DirMoment(1,2)/MomentNormal)*DirMoment(1,1); 

M1j= (DirMoment(1,3)/MomentNormal)*DirMoment (1,1); 

M1k= (DirMoment(1,4)/MomentNormal)*DirMoment(1,1); 

unitvectorMoment= [M1i M1j M1k]; 

B(4,1)= MomentByForce1(1)+MomentByForce2(1)+ unitvectorMoment(1) 

B(5,1)= MomentByForce1(2)+MomentByForce2(2)+ unitvectorMoment(2) 

B(6,1)= MomentByForce1(3)+MomentByForce2(3)+ unitvectorMoment(3) 
+0

IMO:這個問題是不可能的幫助,因爲它主張。什麼是'ForceDir','NF1'等?你究竟想要循環什麼? –

+0

那些來自我閱讀過的文本文件,'ForceDir'是力的作用方向,它存儲在一個矩陣中,所以我用'ForceDir(1,2)'調用它們。我基本上做了我自己的變量,在我的工作空間中實際調用數字。我想循環找到NF1和NF2並將其應用到我知道的方向。 – Jacob

回答

0

如果我理解你correclty,這應該做的伎倆:

for ii = 1:N 
    NF(ii) = ((ForceDir(ii,2))^2+(ForceDir(ii,3))^2+(ForceDir(ii,4))^2)^(1/2); 
    Fi(ii) = ForceDir(ii,2)/NF(ii) * ForceDir(ii,1); 
    Fj(ii) = ForceDir(ii,3)/NF(ii) * ForceDir(ii,1); 
    Fk(ii) = ForceDir(ii,4)/NF(ii) * ForceDir(ii,1); 
    %% Etc 
end