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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by