# Code loop logic issue

1 Ansicht (letzte 30 Tage)
Takura Nyatsuro am 5 Feb. 2023
Bearbeitet: Tushar Behera am 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');
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
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

Tushar Behera am 5 Feb. 2023
Bearbeitet: Tushar Behera am 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');
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
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!