Vector imbalance error in while loop
2 次查看(过去 30 天)
显示 更早的评论
I am trying to plot based on a certain situation. In the first to situation (Beam. Load == 1 and Beam.Support == 1) and (Beam. Load == 1 and Beam.Support == 2), I need to create two sets of y values and two sets of x values and then join them together such that x1 is the x values and y is y values. When I run this script in MATLAB the other cases work, but not the one stated above. Instead MATLAB returns the error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in Beam_Deflection_GUI>process (line 395)
y1(i) = (F.*x.^2/6*E*I).*(3*a-x); % 0<x<a
Error while evaluating uicontrol Callback
Here is the Code:
I = Beam.Inertia;
a = Beam.Location;
F = Beam.Magnitude;
L = Beam.Length;
b = Beam.Length-Beam.Location;
x = linspace(0,Beam.Length);
o = Beam.Magnitude/Beam.Length; % Magnitude is them same as the force
y1 = zeros(1,100);
y2 = zeros(1,100);
x1 = zeros(1,100);
x2 = zeros(1,100);
i = 1;
Beam.x1 = x;
if Beam.Load == 1 % Referring to the point in the list during the selection
if Beam.Support == 1
while x(i) < a
y1(i) = (F.*x.^2/6*E*I).*(3*a-x); % 0<x<a
x1(i) = x(i);
i = i + 1;
end
while x(i) <= L && x(i) < a
y2(i) = (F.*a^2/6*E*I).*(3*x-a); % a<x<L
x2(i) = x(i);
i = i+1;
end
Beam.y1 = [y1, y2];
Beam.x1 = [x1, x2];
else % x.Support == 2
while x(i) < a
y1 = (F*b.*x/6*L*E*I).*(L^2-x.^2-b^2); % 0<x<a
i = i+1;
x1 = x(i);
end
while x(i) <= L
y2 = (F*b/6*E*L*I).*((L/b).*(x-a).^3+(L^2-b^2).*x-x.^3);% a<x<L
i = i+1;
x2 = x(i);
end
Beam.y1 = [y1, y2];
Beam.x1 = [x1, x2];
end
else % x.Load == 2
if Beam.Support == 1
Beam.y1 = (o.*x.^2/24*E*I).*(x.^2+6*L^2-4*L.*x);
else % x.Support == 2
Beam.y1 = (o.*x.^2/24*E*I).*((L^3-2*L.*x.^2)+x.^3);
end
end
0 个评论
采纳的回答
Matt Fig
2012-12-4
编辑:Matt Fig
2012-12-4
You may have meant:
y1(i) = (F.*x(i).^2/6*E*I).*(3*a-x(i)); % 0<x<a
Otherwise you are trying to put numel(x) elements into a single element of y1.
3 个评论
Walter Roberson
2012-12-4
If all of the x(i) < L then you "run off the end" of x. You increment i and blindly use x(i) without checking to see if the new "i" is after the end of "x".
Matt Fig
2012-12-4
It would be better to check that your increment variable (i) is not larger than the length of the vectors involved.
X = [1 5 2 4 3];
L = length(X);
ii = 1;
while ii<=L
X(ii) = X(ii) + 10;
ii = ii + 1;
end
X
But, if you know you are just going to increment through the vector elements, why not use a FOR instead?
X = [1 5 2 4 3];
for ii = 1:length(X)
X(ii) = X(ii) + 10;
end
X
更多回答(2 个)
Walter Roberson
2012-12-4
Your loop is while x(i) < a implying that x is a vector. But then you have
y1(i) = (F.*x.^2/6*E*I).*(3*a-x);
and if x is a vector then the result of the calculation is going to be a vector, and you then try to store that vector into the single result y1(i) .
Perhaps you want
y1(i) = (F.*x(i).^2/6*E*I).*(3*a-x(i));
and similar fixes in other lines.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!