Code loop logic issue

조회 수: 3 (최근 30일)
Takura Nyatsuro
Takura Nyatsuro 2023년 2월 5일
편집: Tushar Behera 2023년 2월 5일
Hi,
Im trying to make a takeoff weight estimation code formy course and im having some issues trying to figure out the logic for then code. the code is meant to calculate a value of weight using a set of predetermined values and compare it to the guessed value which is input. I have it set up so that if the calculated value does not match the guessed one its meant to loop the code using a new value. However i can only get it to don one loop before stopping, Any help would be appreciated.
Thank you
close;
clear;
Wguess=input('input weight guess');
WTO=input('input original weight');
WE=input('input empty weight');
WF=input('input fuel weight');
WPAY=input('input payload weight');
AW0=WE/WTO;
WFRAC=WF/WTO;
Wcalc=(WPAY)/(1-WFRAC-AW0);
if Wcalc==Wguess
WTO=Wcalc;
else
Wguess=Wcalc;
Wcalc=(WPAY)/(1-(WF/WTO)-(WE/WTO));
end

답변 (1개)

Tushar Behera
Tushar Behera 2023년 2월 5일
편집: Tushar Behera 2023년 2월 5일
Hi Takura
I believe you want to loop through the code until the guessed value is equal to the calculated value.
In order to acheive that you can use a while loop to iterate over your code. For example,
close;
clear;
Wguess=input('input weight guess');
WTO=input('input original weight');
WE=input('input empty weight');
WF=input('input fuel weight');
WPAY=input('input payload weight');
AW0=WE/WTO;
WFRAC=WF/WTO;
% Start a while loop to repeat the process until the guessed value is correct
while Wcalc ~= Wguess
Wcalc=(WPAY)/(1-WFRAC-AW0);
% Check if the calculated value matches the guessed value
if Wcalc==Wguess
WTO=Wcalc;
break;
end
% Update the guessed value for the next iteration
Wguess=Wcalc;
Wcalc=(WPAY)/(1-(WF/WTO)-(WE/WTO));
end
% Display the final takeoff weight
disp(['The takeoff weight is: ' num2str(WTO)])
I hope this resolves your query.
Regards,
Tushar

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by