How do I return to the 2nd For loop until the if statement is met before starting the 2nd iteration of the first For loop?

2 vues (au cours des 30 derniers jours)
This may not be a very clean way to code this but, I am trying to get the 2nd For loop to repeat until the 2nd if loop condition is met then return to the 1st For loop and begin the next iteration until all 19 iterations are complete.
%% AEM 508 Term Project
% Define Known Variables
clc
clear all
close all
Pa = 18750; %Patm Pa
Ta = 216.7; %Tatm K
UFlight = 252; % m/s
T = 189000; % Thrust Needed N
To4 = 1875; % K Temp after CC
Qr = 43400000;
EtaDiffuse = .97;
EtaCompress = .85;
EtaBurn = 1;
EtaTurbine = .9;
EtaNozzle = .98;
EtaFan = .85;
EtaFN = .97;
R = 287; % Gas Constant (j/(kg.K))
B = 5; % Bypass Ratio
GDiffuse = 1.4; % Gamma Diffuser
GCompress = 1.37; % Gamma Compressor
GBurn = 1.35; % Gamma Burner
GTurbine = 1.33; % Gamma Turbine
GNozzle = 1.36; % Gamma Nozzle
GFan = 1.4; % Gamma Fan
GFN = 1.4; % Gamma Fan-Nozzle
%% Calculations
% Diffuser
M = UFlight/(sqrt(1.4*R*Ta));
To2 = Ta*(1+((GDiffuse-1)/2)*(M^2));
Po2 = Pa*(1+EtaDiffuse*((To2/Ta)-1)^(GDiffuse/(GDiffuse-1)));
Toa = To2;
% Compressor/CC/Turbine/Nozzle
CpB = R*(GBurn/(GBurn-1)); % Cp Combustion Chamber
PrcIDX = 0;
Prf = 1;
for Prc = 7:1:25;
PrcIDX = PrcIDX + 1;
PrcSave(PrcIDX) = Prc;
Po3 = Po2*Prc;
To3 = To2*(1+(1/EtaCompress)*((Prc.^((GCompress-1)/GCompress))-1));
f = (To4-To3)/(((EtaBurn*Qr)/CpB)-To4); % air-fuel ratio
Po4 = Po3*((To4/To3)^(GBurn/(GBurn-1)));
To8 = To2*(1+(1/EtaFan)*((Prf^(GFan-1)/GFan)-1));
Po8 = Po2*Prf;
To5 = To4 - (To3-To2) - B*(To8-Toa);
Po5 = Po4*((1-(1/EtaTurbine)*(1-(To5/To4)))^(GTurbine/(GTurbine-1)));
if abs(Po5 - Po8)<100
PrfSave(PrcIDX) = Prf;
Prf = 1;
continue
else
for Prf = Prf + .0001
To8 = To2*(1+(1/EtaFan)*((Prf^(GFan-1)/GFan)-1));
Po8 = Po2*Prf;
To5 = To4 - (To3-To2) - B*(To8-Toa);
Po5 = Po4*((1-(1/EtaTurbine)*(1-(To5/To4)))^(GTurbine/(GTurbine-1)));
if abs(Po5 - Po8)>100
continue
else
PrfSave(PrcIDX) = Prf;
Prf = 1;
break
end
end
end
end

Réponse acceptée

Cris LaPierre
Cris LaPierre le 12 Nov 2021
It looks to me like you may want to use a while loop for your second loop instead of a for loop. Right now, it is only running once in each loop of the first for loop.
for a = 3 + 0.001
a
end
a = 3.0010
  3 commentaires
Cris LaPierre
Cris LaPierre le 13 Nov 2021
I think the while conditional should be the opposite of what you have for your if statement, and the code you have in the if statement should be moved to after the while loop instead of inside it (without the if).
Also, you will want to add back in the code at the bottom of the for loop to set Prf back to 1 before the next loop starts.
Shawn Alcorn
Shawn Alcorn le 13 Nov 2021
Thank you very much for the help. I organized it the way you recommended and it worked perfectly.

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by