Vector imbalance error in while loop

2 views (last 30 days)
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

Accepted Answer

Matt Fig
Matt Fig on 4 Dec 2012
Edited: Matt Fig on 4 Dec 2012
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 Comments
Matt Fig
Matt Fig on 4 Dec 2012
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

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 4 Dec 2012
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.

Lawson Hoover
Lawson Hoover on 4 Dec 2012
Ok I have fixed the first issue but now I am receiving the error:
Attempted to access x(101); index out of bounds because numel(x)=100.
Error in Beam_Deflection_GUI>process (line 415)
x2 = x(i);
Error while evaluating uicontrol Callback
I have turned the all the x into x(i) but I still get that error. Any ideas?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by