Code loop logic issue

3 views (last 30 days)
Takura Nyatsuro
Takura Nyatsuro on 5 Feb 2023
Edited: Tushar Behera on 5 Feb 2023
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

Answers (1)

Tushar Behera
Tushar Behera on 5 Feb 2023
Edited: Tushar Behera on 5 Feb 2023
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!