Counting iterations in a while loop

109 vues (au cours des 30 derniers jours)
Wei-Ta, Huang
Wei-Ta, Huang le 1 Mai 2016
How do I now how many iterations do it need to achieve the convergence ?
% Four stroke Otto cycle model
% input parameters
Ti = 300; % inlet temperature, K
Pi = 50; % inlet pressure, kPa
Pe = 100; % exhaust pressure, kPa
r = 10; % compression ratio
qin = 2500; % heat input, kJ/kg (mixture)
gamma = 1.3; % ideal gas specific heat ratio
R = 0.287; % gas constant kJ/kg K
cv= R/(gamma-1); %const vol specific heat, kJ/kg K
f=0.053;% guess value of residual fraction f
Te = 1000; % guess value of exhaust temp, K
tol = 1e-9; % tolerance for convergence
err = 2*tol; %error initialization
gam=(gamma -1)/gamma;
while (err > tol) %while loop for cycle calc
%intake stroke
T1=(1-f)*Ti + f*(1 - (1- Pi/Pe)*gam)*Te;
P1=Pi;
%isentropic compression
P2=P1*r^gamma;
T2=T1*r^(gamma-1);
%const v heat addition
T3=T2 + qin*(1-f)/cv;
P3=P2*(T3/T2);
%isentropic expansion
P4=P3*(1/r)^gamma;
T4=T3*(1/r)^(gamma-1);
%isentropic blowdown
P5=Pe;
T5=T4*(P4/Pe)^(-gam);
%const p exhaust stroke
Te=T5;
fnew=(1/r)*(Pe/P4)^(1/gamma); %new residual fraction
err=abs(fnew-f)/fnew;
f=fnew;
end
%cycle parameters
eta= 1 - 1/r^(gamma-1);
imep = P1*(qin*(1-f)/(R*T1))/(1-(1/r))*eta;
pmep=Pe-Pi;
etanet= eta*(1-pmep/imep);
imepnet= (imep-pmep)/100.;
voleff=1-(Pe/Pi -1)/(gamma*(r-1));
%output calcs
fprintf(' \nFour Stroke Otto Cycle \n')
fprintf('State 1 2 3 4 \n');
fprintf('Pressure (kPa): %6.1f %6.1f %6.1f %6.1f \n',P1,P2,P3,P4);
fprintf('Temperature (K): %6.1f %6.1f %6.1f %6.1f \n',T1,T2,T3,T4);
fprintf('Ideal Thermal Eff.= %6.3f Net Thermal Eff.= %6.3f \n',eta, etanet);
fprintf('Exhaust Temp. (K)= %6.1f Volumetric Eff.= %6.2f \n',Te, voleff);
fprintf('Residual Fraction %6.3f Net Imep (bar)= %6.2f \n',f, imepnet);
  1 commentaire
louis rassinfosse
louis rassinfosse le 1 Mai 2016
This is unreadable. Please use the "{}code" option..

Connectez-vous pour commenter.

Réponse acceptée

Geoff Hayes
Geoff Hayes le 1 Mai 2016
To count the number of iterations in your while loop, define a local variable that is incremented for each iteration of the loop. For example,
iterCount = 0;
while (err > tol)
iterCount = iterCount + 1;
% etc.
end
Once outside of the loop, iterCount will tell you how many iterations were performed.
Note that you should always be careful with while loops - you can easily get stuck if your solutions doesn't converge and so your condition is never satisfied. You should always guard against this as
MAX_ITERS = 1000;
iterCount = 0;
while (err > too) && iterCount < MAX_ITERS
iterCount = iterCount + 1;
% etc.
end
In the above, we only allow a maximum of 1000 iterations before exiting the while loop.

Plus de réponses (1)

Dhananjay Karad
Dhananjay Karad le 29 Avr 2022
Clear;clc K=0; S=0; While S<12 S=S+K; K=K+1 end Find the iteration in the while loop

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by