循环在不同大小的矢量

时间:2017-04-19 14:26:47

标签: matlab loops vector

此代码用于计算具有叠加原理的梁的挠度,其中对于每个给定位置和梁,计算力的所有单独影响,然后将它们相加。

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没有被压制,将返回

deflection =

   5.8333e-07


deflection =

   1.0967e-06


deflection =

   1.6500e-06


ans =

   1.6500e-06

但是应该将值作为向量而不是仅返回最后一个值

2 个答案:

答案 0 :(得分:1)

我做了一些小改动。首先,我将中心的两个if语句更改为单个if else语句。这删除了您的错误

if x(i) >= a(i)

本应该阅读

if x(i) >= a(n)

虽然在那里我也删除了你的一些while break控制流程,但我并没有完全看到它的目的。你可能想比较一下。

其次,我将您的输出保存到i的{​​{1}}条目中。我还预先分配了它,因此它不会改变循环中的大小。

最后,正如@StackPlayer所建议的那样,我使用y作为循环变量而不是ii。对问题Using i and j as variables in Matlab进行了一些讨论。

i

使用示例:

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

答案 1 :(得分:0)

首先,避免在MATLAB中使用i和j(它们用于虚数)。

在你的第二个while循环中,你有&#34; x&#34;而不是&#34; x(i)&#34;