2017-04-19 34 views
1

該代碼旨在計算具有疊加原理的光束的偏轉,其中對於光束的每個給定位置,計算所有單獨的力的影響,然後相加在一起。循環播放不同尺寸的矢量

function deflection = beamSuperposition(positions, beamLength,loadPositions, loadForces, beamSupport) 
%If several loads are applied, calculate each one individually and end with sum 

x = positions; 
l = beamLength; 
a = loadPositions; 
W = loadForces; 
E = 200*10^9; 
I = 0.001; 

%For every position(x) element calculate the expression for all 
%loadPositions(a) and their respective loadForce(W) 

%Make sure a and W are of same size 
%Make sure neither x or a ever surpass l 

%Go trhough each element of x 
for i = 1:length(x) 
    %For beamSupport = 1 only 
    while beamSupport == 1 
     %Go through each element of 'a' and calculate the deflection 
     for n = 1:length(a) 
      %There are two different equations to calculating the deflection depending on the position of the loadForce compared to the position of interest 
     while true 
      if x(i) < a(n) 
       y = (W(n)*(l-a(n))*x(i))*(l^2-x(i)^2-(l-a(n))^2)/(6*E*I*l); 
      end 
      if x(i) >= a(i) 
       y = (W(n)*a(n)*(l-x(i)))*(l^2-(l-x(i))^2-a(n)^2)/(6*E*I*l); 
      end 
      break 
     end 
     end 
     break 
    end 
%Sum the values for each y element?!? 
    deflection = y 
    end 

我該如何按預期做這項工作?輸出應該是一個矢量y,其大小與x相同,每個值的總和偏差爲x

使用中;

beamSuperposition([1,2,5],10,[6,5,3],[10,15,20],1) 

就回去了,如果deflection = y不supressed,

deflection = 

    5.8333e-07 


deflection = 

    1.0967e-06 


deflection = 

    1.6500e-06 


ans = 

    1.6500e-06 

但首先應該只

+0

可能的錯誤線路28上,應讀'如果x(I)> = A(N)'?不知道爲什麼這是在一段時間「循環」,看起來只會評估一次。 – Steve

+0

你能用短行顯示它的用法來編輯你的問題嗎? – Steve

+0

也許你想保存輸出到'y(i)' – Steve

回答

1

我做了一些小的變化。首先,我已將中央兩個if聲明更改爲單個if else聲明。此刪除你有

if x(i) >= a(i) 

裏面應該有可能讀

if x(i) >= a(n) 

雖然有我還刪除了一些我沒有很明白的目的,你while break控制流的一個錯誤。你可能想要比較。

其次,我將你的輸出保存到yi-條目。我也預分配它,所以它不會改變循環中的大小。

最後,如@StackPlayer所示,我用循環變量ii代替i。關於Using i and j as variables in Matlab的問題有一些討論。

function deflection = beamSuperposition(positions, beamLength,loadPositions, loadForces, beamSupport) 
%If several loads are applied, calculate each one individually and end with sum 

x = positions; 
l = beamLength; 
a = loadPositions; 
W = loadForces; 
E = 200*10^9; 
I = 0.001; 

%For every position(x) element calculate the expression for all 
%loadPositions(a) and their respective loadForce(W) 

%Make sure a and W are of same size 
%Make sure neither x or a ever surpass l 

y = zeros(size(x)); 
%Go through each element of x 
for ii = 1:length(x) 
    %For beamSupport = 1 only 
    if (beamSupport == 1) 
     %Go through each element of 'a' and calculate the deflection 
     for n = 1:length(a) 
      %There are two different equations to calculating the deflection depending on the position of the loadForce compared to the position of interest 
      if x(ii) < a(n) 
       y(ii) = (W(n)*(l-a(n))*x(ii))*(l^2-x(ii)^2-(l-a(n))^2)/(6*E*I*l); 
      else 
       y(ii) = (W(n)*a(n)*(l-x(ii)))*(l^2-(l-x(ii))^2-a(n)^2)/(6*E*I*l); 
      end 
     end 
    end 
end 
%Sum the values for each y element?!? 
deflection = y; 
end 

實施例使用:

>> beamSuperposition([1,2,5],10,[6,5,3],[10,15,20],1) 

ans = 

    1.0e-05 * 

    0.0583 0.1097 0.1650 
0

第一件事返回值作爲載體的,而不是最後的值,避免使用I和J MATLAB(它們用於虛數)。

在你的第二個while循環,你如果不是 「X(I)」 的 「X」 條件

+0

這似乎有幫助,但我怎樣才能更改向量而不是三個單獨的結果的答案? – ElCapitain

+0

其中3個單獨結果? (通常你可以這樣組合:x_vec = [x1 x2 x3];) –

+0

啊我現在可以看到你的編輯了,只需在y(i)中迭代保存,但是請將我改爲idx(索引)或類似的東西 –