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)
  댓글 수: 2
Image Analyst
Image Analyst 2022년 1월 10일
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 2022년 1월 11일
i want to make the process re-input the data if user key-in wrong input like negative value and no input from user

댓글을 달려면 로그인하십시오.

답변 (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