hi i want to ask,why matlab cannot process the condition for the while loop ? Thank you!

1 次查看(过去 30 天)
clc
clear
close all
%Input data
w=input('Enter the load,w: ');
L=input('Enter the beam length: ');
x=input('Enter specific point: ');
%While loop if false
while (w<1||L<1||x<0||isempty(w)||isempty(L)||isempty(x))
fprintf("\nPlease recheck input data\n")
w=input('Enter the load,w: ');
L=input('Enter the beam length: ');
x=input('Enter specific point: ');
end
%true input data
Ra=w*L;
Ma=-w*L.^2/2;
Mx=-w*(L-x).^2/2;
Sx=w*(L-x);
fprintf("\nThe reaction at A is %.2f\n",Ra)
fprintf("The moment at A is %.2f\n",Ma)
fprintf("The bending moment at point %.f is %.2f\n",x,Mx)
fprintf("The shear force at point %.f is %.2f\n",x,Sx)

回答(1 个)

Steven Lord
Steven Lord 2022-1-10
Since you're using the short-circuiting or operator you should check the size of the inputs first. Rather than asking if they're empty, ask if they're not a scalar.
%{
while ~isscalar(w) || ~isscalar(L) || ~isscalar(x) || w < 1 || L < 1 || x < 0
%}
That way if (for example) w was not a scalar the ~isscalar(w) check would make the while condition return false before it gets to the w < 1 part (and throws an error.)
w = 0:5;
~isscalar(w) || w < 1 % false, MATLAB doesn't get to the w < 1 check
ans = logical
1
w < 1 || ~isscalar(w) % error, w < 1 is logical but not scalar
Operands to the logical and (&&) and or (||) operators must be convertible to logical scalar values.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by