How to write the equivalent of an 'until loop' in matlab?
184 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
if T>=673
U=(4*11.3)/(5000*0.0762);
"until T=593"
else U=1/(298-T);
end;
Basically I want to be able to say until T = 593 but in a way understood by Matlab.
The whole code is such:
function f = dndw(w,n)
b=n(1);
o=n(2);
bd=n(3);
h=n(4);
co=n(5);
T=n(6);
k1=(85)*exp(-53100/(8.314*T));
k2=(9944444.444)*exp(-128300/(8.314*T));
k3=(8722222.222)*exp(-154700/(8.314*T));
k4=(95000000)*exp(-156000/(8.314*T));
np=4.317;
nn=639.2;
nt=b+o+bd+h+np+co+nn;
P=202.650;
pb=(b/nt)*P;
po=(o/nt)*P;
pbd=(b/nt)*P;
r1=(k1*k2*(po^0.5)*pb)/(0.5*k1*pb+k2*(po^0.5));
r2=k3*(po^0.7)*(pb^0.25)*(pbd^-0.86);
r3=k4*(po^0.25)*(pbd^0.85);
dbdw=-r1-r2;
dodw=-0.5*r1-6*r2-5.5*r3;
dbddw=r1-r3;
dhdw=r1+4*r2+3*r3;
dcodw=4*r2+4*r3;
***dTdw=(U*(493-T)*((165170.736*r1)+(2711247*r2)+(2431510*r3)))/(40*nt)***;
f=[dbdw;dodw;dbddw;dhdw;dcodw;dTdw];
end
Thanks!
0 Commenti
Risposte (2)
Roger Stafford
il 3 Apr 2016
To reproduce the equivalent of repeat --- until in C, do this:
b = true;
while b
do the loop computations
b = ~until % <-- Form the logical "NOT" of the desired "until" condition
end
3 Commenti
Kavya
il 1 Lug 2023
it shows error. can you suggest other way we could represent repeat-until loop?
the loop limit is not known but it must terminate when a particular condition is reached.
Image Analyst
il 1 Lug 2023
You certainly CAN use break in an if block as long as the if block is inside a while or for block. You should also use a failsafe to prevent an infinite loop. Here is an example:
% Demonstration of how to avoid an infinite loop by setting up a failsafe.
% Set up a failsafe
maxIterations = 100; % Way more than you think it would ever need.
loopCounter = 0;
% Now loop until we obtain the required condition: a random number equals exactly 0.5.
% If that never happens, the failsafe will kick us out of the loop so we do not get an infinite loop.
r = nan; % Initialize so we can enter the loop the first time.
while (r ~= 0.5) && loopCounter < maxIterations
loopCounter = loopCounter + 1;
fprintf('Iteration #%d.\n', loopCounter)
% Now break if some other condition becomes true somehow.
someOtherCondition = whatever;
if someOtherCondition
break;
end
% Get another, new random value.
r = rand;
end
% Alert user if we exited normally, or if the failsafe kicked us out to avoid an infinite loop.
if loopCounter < maxIterations
% Then the loop found the condition and exited early, which means normally.
fprintf('Loop exited normally after %d iterations.\n', loopCounter);
else
% Then the loop never found the condition and exited when the number of iterations
% hit the maximum number of iterations allowed, which means abnormally.
fprintf('Loop exited abnormally after iterating the maximimum number of iterations (%d) without obtaining the exit criteria.\n', maxIterations);
end
fprintf('All done after %d iterations.\n', loopCounter)
Azzi Abdelmalek
il 3 Apr 2016
2 Commenti
Azzi Abdelmalek
il 3 Apr 2016
%Example: calcul the sum s= 1+2+3+...+n with s<=1000
s=0;
k=1;
while k<1000
k=2*k
s=s+k
end
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!