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

1 visualización (últimos 30 días)
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)
  2 comentarios
Image Analyst
Image Analyst el 10 de En. de 2022
Works fine for me. What are your initial inputs before the loop and then what are you entering in the loop?
NUR AIN ATIKAH ABDUL LATIF
NUR AIN ATIKAH ABDUL LATIF el 11 de En. de 2022
i want to make the process re-input the data if user key-in wrong input like negative value and no input from user

Iniciar sesión para comentar.

Respuestas (1)

Steven Lord
Steven Lord el 10 de En. de 2022
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.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by