Exit a loop

• To: mathgroup at smc.vnet.net
• Subject: [mg122594] Exit a loop
• From: Mary R <mathematica023 at gmail.com>
• Date: Thu, 3 Nov 2011 03:43:00 -0500 (EST)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com

```Hi to everyone,
i try to exit a loop when this criterion is satisfied:
If[( (matriceP[[i - 1, 1]] - matriceP[[i - 1, -1]])/matriceP[[i - 1, 1]])^2
< ( (matriceP[[i, 1]] - matriceP[[i, -1]])/matriceP[[i, 1]])^2
i try with Goto[thispoint]-Label[thispoint], and it works, but have this
error:
I try again with Catch and Throw (as you can see in comment), but the
calculations goes on without stopping.
Have any idea?
The part of algoritm interesting is signed by (*THIS PART*) near the end of
the notebook.
M.
----------------------------------------------
Timing[indicek = 1;
matriceP = {};
valorek = {};
differencep = {};
mfugv1 = {{}};
mfugl1 = {{}};
mfugv2 = {{}};
mfugl2 = {{}};
my1 = {{}};
my2 = {{}};
vettore = {};
Do[\[Omega] = {0.1521, 0.32684};
x = {0.0991, 1 - 0.0991};
Tc = {369.89, 374.21};
Subscript[P, c] = {42.512, 40.593};
R = 83.14;
T = 273.15;
tollerancep = 10^-8;
tollerancey = 10^-8;
incrP = 0.8;
decremento = 0;
incremento = 0;
ris = {};
passo = 1;
label = {"step", "P", "\!\(\*SubscriptBox[\(y\), \(1  n\)]\)",
"\!\(\*SubscriptBox[\(y\), \(2  n\)]\)",
"\!\(\*SubscriptBox[\(f\), \(1  l\)]\)",
"\!\(\*SubscriptBox[\(f\), \(2  l\)]\)",
"\!\(\*SubscriptBox[\(f\), \(1  v\)]\)",
"\!\(\*SubscriptBox[\(f\), \(2  v\)]\)", "\[CapitalDelta]"};
Subscript[T, r] = Table[T/Tc[[i]], {i, 2}];
alfa =
Table[(1 + (0.37464 + 1.54226 \[Omega][[i]] -
0.26993 (\[Omega][[i]])^2 ) (1 - Sqrt[
Subscript[T, r][[i]]]))^2, {i, 2}];
a = Table[(0.45723553 alfa[[i]]  R^2 (Tc^2)[[i]])/
Subscript[P, c][[i]] , {i, 2}];
b = Table[(0.07779607 R Tc[[i]] )/Subscript[P, c][[i]], {i, 2}];
P = {2.8};
Subscript[y, 1] = {0.3596};
Subscript[y, 1 n] = {0.3596};
Subscript[y, 2] = {1 - 0.3596};
Subscript[y, 2 n] = {1 - 0.3596};
numy = 1;
nump = 1;
Subscript[f, 1 l] = {};
Subscript[f, 2 l] = {};
Subscript[f, 1 v] = {};
Subscript[f, 2 v] = {};
A = Table[(a[[i]] P[[nump]])/(R^2 T^2), {i, 2}];
B = Table[(b[[i]] P[[nump]])/(R T), {i, 2}];
Subscript[A, 12] = Sqrt[A[[1]]*A[[2]]]*(1 - k);

Subscript[A, liq] =
x[[1]]^2 A[[1]] + 2*(x[[1]]*x[[2]])* Subscript[A, 12] +
x[[2]]^2 A[[2]];
Subscript[B, liq] = x[[1]]*B[[1]] + x[[2]]*B[[2]];
solzl =
NSolve[Z - 1/(1 - Subscript[B, liq]/Z) +
Subscript[A, liq]/Subscript[B, liq]*Subscript[B, liq]/Z/(
1 + 2 *Subscript[B, liq]/Z - (Subscript[B, liq]/Z)^2) == 0, Z];
soll = Z /. {solzl[[1]], solzl[[3]]};
Subscript[ln\[Phi], 1 l] =
B[[1]]/Subscript[B, liq] (soll[[2]] - 1) -
Log[soll[[2]] - Subscript[B, liq]] -
Subscript[A, liq]/(Subscript[B, liq] Sqrt[8])
Log[(soll[[2]] + (1 + Sqrt[2]) Subscript[B, liq])/(
soll[[2]] + (1 - Sqrt[2]) Subscript[B, liq])] (1/Subscript[A,
liq] 2 (x[[1]] A[[1]] + x[[2]] Subscript[A, 12]) - B[[1]]/
Subscript[B, liq]);
Subscript[ln\[Phi], 2 l] =
B[[2]]/Subscript[B, liq] (soll[[2]] - 1) -
Log[soll[[2]] - Subscript[B, liq]] -
Subscript[A, liq]/(Subscript[B, liq] Sqrt[8])
Log[(soll[[2]] + (1 + Sqrt[2]) Subscript[B, liq])/(
soll[[2]] + (1 - Sqrt[2]) Subscript[B, liq])] (1/Subscript[A,
liq] 2 (x[[1]] Subscript[A, 12] + x[[2]] A[[2]]) - B[[2]]/
Subscript[B, liq]);
Subscript[\[Phi], 1 l] = Exp[Subscript[ln\[Phi], 1 l]];
Subscript[\[Phi], 2 l] = Exp[Subscript[ln\[Phi], 2 l]];
Subscript[f, 1 l] =
Append[Subscript[f, 1 l], Subscript[\[Phi], 1 l]*x[[1]]];
Subscript[f, 2 l] =
Append[Subscript[f, 2 l], Subscript[\[Phi], 2 l]*x[[2]]];
Subscript[B, vap] =
Subscript[y, 1][[numy]]*B[[1]] + Subscript[y, 2][[numy]]*B[[2]];
Subscript[A, vap] =
Subscript[y, 1][[numy]]^2 A[[1]] +
2 Subscript[y, 1][[numy]] Subscript[y, 2][[numy]] Subscript[A,
12] + Subscript[y, 2][[numy]]^2 A[[2]];
solzv =
NSolve[Z - 1/(1 - Subscript[B, vap]/Z) +
Subscript[A, vap]/Subscript[B, vap]*Subscript[B, vap]/Z/(
1 + 2 *Subscript[B, vap]/Z - (Subscript[B, vap]/Z)^2) == 0, Z];
solv = Z /. {solzv[[1]], solzv[[3]]};
Subscript[ln\[Phi], 1 v] =
B[[1]]/Subscript[B, vap] (solv[[1]] - 1) -
Log[solv[[1]] - Subscript[B, vap]] -
Subscript[A, vap]/(Subscript[B, vap] Sqrt[8])
Log[(solv[[1]] + (1 + Sqrt[2]) Subscript[B, vap])/(
solv[[1]] + (1 - Sqrt[2]) Subscript[B, vap])] (1/Subscript[A,
vap] 2 (Subscript[y, 1][[numy]] A[[1]] +
Subscript[y, 2][[numy]] Subscript[A, 12]) - B[[1]]/
Subscript[B, vap]);
Subscript[ln\[Phi], 2 v] =
B[[2]]/Subscript[B, vap] (solv[[1]] - 1) -
Log[solv[[1]] - Subscript[B, vap]] -
Subscript[A, vap]/(Subscript[B, vap] Sqrt[8])
Log[(solv[[1]] + (1 + Sqrt[2]) Subscript[B, vap])/(
solv[[1]] + (1 - Sqrt[2]) Subscript[B, vap])] (1/Subscript[A,
vap] 2 (Subscript[y, 1][[numy]] Subscript[A, 12] +
Subscript[y, 2][[numy]] A[[2]]) - B[[2]]/Subscript[B, vap]);
Subscript[\[Phi], 1 v] = Exp[Subscript[ln\[Phi], 1 v]];
Subscript[\[Phi], 2 v] = Exp[Subscript[ln\[Phi], 2 v]];
Subscript[f, 1 v] =
Append[Subscript[f, 1 v],
Subscript[\[Phi], 1 v]*Subscript[y, 1][[numy]]];
Subscript[f, 2 v] =
Append[Subscript[f, 2 v],
Subscript[\[Phi], 2 v]*Subscript[y, 2][[numy]]];

While[Abs[Subscript[f, 1 l][[nump]] - Subscript[f, 1 v][[numy]]] +
Abs[Subscript[f, 2 l][[nump]] - Subscript[f, 2 v][[numy]]] >
tollerancep,
numy = numy + 1;
Subscript[k, 1] = Subscript[\[Phi], 1 l]/Subscript[\[Phi], 1 v];
Subscript[k, 2] = Subscript[\[Phi], 2 l]/Subscript[\[Phi], 2 v];
Subscript[y, 1] =
Append[Subscript[y, 1], (x[[1]]*Subscript[k, 1])];
Subscript[y, 2] =
Append[Subscript[y, 2], (x[[2]]*Subscript[k, 2])];
Subscript[y, 1 n] =
Append[Subscript[y,
1 n], (x[[1]]*Subscript[k, 1])/(x[[1]]*Subscript[k, 1] +
x[[2]]*Subscript[k, 2])];
Subscript[y, 2 n] =
Append[Subscript[y,
2 n], (x[[2]]*Subscript[k, 2])/(x[[1]]*Subscript[k, 1] +
x[[2]]*Subscript[k, 2])];
Subscript[B, vap] =
Subscript[y, 1 n][[numy]]*B[[1]] +
Subscript[y, 2 n][[numy]]*B[[2]];
Subscript[A, vap] =
Subscript[y, 1 n][[numy]]^2 A[[1]] +
2 Subscript[y, 1 n][[numy]] Subscript[y, 2 n][[numy]] Subscript[
A, 12] + Subscript[y, 2 n][[numy]]^2 A[[2]];
solzv =
NSolve[Z - 1/(1 - Subscript[B, vap]/Z) +
Subscript[A, vap]/Subscript[B, vap]*Subscript[B, vap]/Z/(
1 + 2 *Subscript[B, vap]/Z - (Subscript[B, vap]/Z)^2) == 0,
Z];
solv = Z /. {solzv[[1]], solzv[[3]]};
Subscript[ln\[Phi], 1 v] =
B[[1]]/Subscript[B, vap] (solv[[1]] - 1) -
Log[solv[[1]] - Subscript[B, vap]] -
Subscript[A, vap]/(Subscript[B, vap] Sqrt[8])
Log[(solv[[1]] + (1 + Sqrt[2]) Subscript[B, vap])/(
solv[[1]] + (1 - Sqrt[2]) Subscript[B, vap])] (1/Subscript[A,
vap] 2 (Subscript[y, 1 n][[numy]] A[[1]] +
Subscript[y, 2 n][[numy]] Subscript[A, 12]) - B[[1]]/
Subscript[B, vap]);
Subscript[ln\[Phi], 2 v] =
B[[2]]/Subscript[B, vap] (solv[[1]] - 1) -
Log[solv[[1]] - Subscript[B, vap]] -
Subscript[A, vap]/(Subscript[B, vap] Sqrt[8])
Log[(solv[[1]] + (1 + Sqrt[2]) Subscript[B, vap])/(
solv[[1]] + (1 - Sqrt[2]) Subscript[B, vap])] (1/Subscript[A,
vap] 2 (Subscript[y, 1 n][[numy]] Subscript[A, 12] +
Subscript[y, 2 n][[numy]] A[[2]]) - B[[2]]/Subscript[B,
vap]);
Subscript[\[Phi], 1 v] = Exp[Subscript[ln\[Phi], 1 v]];
Subscript[\[Phi], 2 v] = Exp[Subscript[ln\[Phi], 2 v]];
Subscript[f, 1 v] =
Append[Subscript[f, 1 v],
Subscript[\[Phi], 1 v]*Subscript[y, 1 n][[numy]]];
Subscript[f, 2 v] =
Append[Subscript[f, 2 v],
Subscript[\[Phi], 2 v]*Subscript[y, 2 n][[numy]]];
Abs[Subscript[f, 1 v][[numy]] - Subscript[f, 1 v][[numy - 1]]] +
Abs[Subscript[f, 2 v][[numy]] - Subscript[f, 2 v][[numy - 1]]];
Abs[Subscript[f, 1 l][[nump]] - Subscript[f, 1 v][[numy]]] +
Abs[Subscript[f, 2 l][[nump]] - Subscript[f, 2 v][[numy]]];
While[
Abs[Subscript[f, 1 v][[numy]] - Subscript[f, 1 v][[numy - 1]]] +
Abs[Subscript[f, 2 v][[numy]] -
Subscript[f, 2 v][[numy - 1]]] >
tollerancey,(*inizio while su y*)
numy = numy + 1;
Subscript[k, 1] = Subscript[\[Phi], 1 l]/Subscript[\[Phi], 1 v];
Subscript[k, 2] = Subscript[\[Phi], 2 l]/Subscript[\[Phi], 2 v];
Subscript[y, 1] =
Append[Subscript[y, 1], (x[[1]]*Subscript[k, 1])];
Subscript[y, 2] =
Append[Subscript[y, 2], (x[[2]]*Subscript[k, 2])];
Subscript[y, 1 n] =
Append[Subscript[y,
1 n], (x[[1]]*Subscript[k, 1])/(x[[1]]*Subscript[k, 1] +
x[[2]]*Subscript[k, 2])];
Subscript[y, 2 n] =
Append[Subscript[y,
2 n], (x[[2]]*Subscript[k, 2])/(x[[1]]*Subscript[k, 1] +
x[[2]]*Subscript[k, 2])];
Subscript[B, vap] =
Subscript[y, 1 n][[numy]]*B[[1]] +
Subscript[y, 2 n][[numy]]*B[[2]];
Subscript[A, vap] =
Subscript[y, 1 n][[numy]]^2 A[[1]] +
2 Subscript[y, 1 n][[numy]] Subscript[y, 2 n][[
numy]] Subscript[A, 12] + Subscript[y, 2 n][[numy]]^2 A[[2]];
solzv =
NSolve[Z - 1/(1 - Subscript[B, vap]/Z) +
Subscript[A, vap]/Subscript[B, vap]*Subscript[B, vap]/Z/(
1 + 2 *Subscript[B, vap]/Z - (Subscript[B, vap]/Z)^2) == 0,
Z];
solv = Z /. {solzv[[1]], solzv[[3]]};
Subscript[ln\[Phi], 1 v] =
B[[1]]/Subscript[B, vap] (solv[[1]] - 1) -
Log[solv[[1]] - Subscript[B, vap]] -
Subscript[A, vap]/(Subscript[B, vap] Sqrt[8])
Log[(solv[[1]] + (1 + Sqrt[2]) Subscript[B, vap])/(solv[[
1]] + (1 - Sqrt[2]) Subscript[B, vap])] (1/Subscript[A,
vap] 2 (Subscript[y, 1][[numy]] A[[1]] +
Subscript[y, 2][[numy]] Subscript[A, 12]) - B[[1]]/
Subscript[B, vap]);
Subscript[ln\[Phi], 2 v] =
B[[2]]/Subscript[B, vap] (solv[[1]] - 1) -
Log[solv[[1]] - Subscript[B, vap]] -
Subscript[A, vap]/(Subscript[B, vap] Sqrt[8])
Log[(solv[[1]] + (1 + Sqrt[2]) Subscript[B, vap])/(solv[[
1]] + (1 - Sqrt[2]) Subscript[B, vap])] (1/Subscript[A,
vap] 2 (Subscript[y, 1][[numy]] Subscript[A, 12] +
Subscript[y, 2][[numy]] A[[2]]) - B[[2]]/Subscript[B,
vap]);
Subscript[\[Phi], 1 v] = Exp[Subscript[ln\[Phi], 1 v]];
Subscript[\[Phi], 2 v] = Exp[Subscript[ln\[Phi], 2 v]];
Subscript[f, 1 v] =
Append[Subscript[f, 1 v],
Subscript[\[Phi], 1 v]*Subscript[y, 1 n][[numy]]];
Subscript[f, 2 v] =
Append[Subscript[f, 2 v],
Subscript[\[Phi], 2 v]*Subscript[y, 2 n][[numy]]];]
If
[Abs[Subscript[f, 1 l][[nump]] - Subscript[f, 1 v][[numy]]] +
Abs[Subscript[f, 2 l][[nump]] - Subscript[f, 2 v][[numy]]] >
tollerancep,

ris =
Append[{ris},
Join[{P[[nump]]}, {Subscript[y, 1 n]}, {Subscript[y,
2 n]}, {Subscript[f, 1 l][[nump]]}, {Subscript[f, 2 l][[
nump]]}, {Subscript[f, 1 v]}, {Subscript[f,
2 v]}, {Abs[
Subscript[f, 1 l][[nump]] - Subscript[f, 1 v][[numy]]] +
Abs[Subscript[f, 2 l][[nump]] -
Subscript[f, 2 v][[numy]]]}]] // MatrixForm;
If
[
Abs[P[[nump - 2]] - P[[nump - 1]]] ==
Abs[P[[nump - 1]] - P[[nump]]],
incrP = incrP/2;

incrP
];

nump = nump + 1;
P =
Append[P,
If[Subscript[y, 1][[numy]] + Subscript[y, 2][[numy]] > 1,
P[[nump - 1]] + incrP, P[[nump - 1]] - incrP]];
A = Table[(a[[i]] P[[nump]])/(R^2 T^2), {i, 2}];
B = Table[(b[[i]] P[[nump]])/(R T), {i, 2}];
Subscript[A, 12] = Sqrt[A[[1]]*A[[2]]]*(1 - k);
Subscript[A, liq] =
x[[1]]^2 A[[1]] + 2*(x[[1]]*x[[2]])* Subscript[A, 12] +
x[[2]]^2 A[[2]];
Subscript[B, liq] = x[[1]]*B[[1]] + x[[2]]*B[[2]];
solzl =
NSolve[Z - 1/(1 - Subscript[B, liq]/Z) +
Subscript[A, liq]/Subscript[B, liq]*Subscript[B, liq]/Z/(
1 + 2 *Subscript[B, liq]/Z - (Subscript[B, liq]/Z)^2) == 0,
Z];
soll = Z /. {solzl[[1]], solzl[[3]]};
Subscript[ln\[Phi], 1 l] =
B[[1]]/Subscript[B, liq] (soll[[2]] - 1) -
Log[soll[[2]] - Subscript[B, liq]] -
Subscript[A, liq]/(Subscript[B, liq] Sqrt[8])
Log[(soll[[2]] + (1 + Sqrt[2]) Subscript[B, liq])/(soll[[
2]] + (1 - Sqrt[2]) Subscript[B, liq])] (1/Subscript[A,
liq] 2 (x[[1]] A[[1]] + x[[2]] Subscript[A, 12]) - B[[1]]/
Subscript[B, liq]);
Subscript[ln\[Phi], 2 l] =
B[[2]]/Subscript[B, liq] (soll[[2]] - 1) -
Log[soll[[2]] - Subscript[B, liq]] -
Subscript[A, liq]/(Subscript[B, liq] Sqrt[8])
Log[(soll[[2]] + (1 + Sqrt[2]) Subscript[B, liq])/(soll[[
2]] + (1 - Sqrt[2]) Subscript[B, liq])] (1/Subscript[A,
liq] 2 (x[[1]] Subscript[A, 12] + x[[2]] A[[2]]) - B[[2]]/
Subscript[B, liq]);
Subscript[\[Phi], 1 l] = Exp[Subscript[ln\[Phi], 1 l]];
Subscript[\[Phi], 2 l] = Exp[Subscript[ln\[Phi], 2 l]];
Subscript[f, 1 l] =
Append[Subscript[f, 1 l], Subscript[\[Phi], 1 l]*x[[1]]];
Subscript[f, 2 l] =
Append[Subscript[f, 2 l], Subscript[\[Phi], 2 l]*x[[2]]];
Subscript[y, 1 n] = {Subscript[y, 1 n][[numy]]};
Subscript[y, 2 n] = {Subscript[y, 2 n][[numy]]};
Subscript[y, 1] = {Subscript[y, 1][[numy]]};
Subscript[y, 2] = {Subscript[y, 2][[numy]]};
Subscript[f, 1 v] = {Subscript[f, 1 v][[numy]]};
Subscript[f, 2 v] = {Subscript[f, 2 v][[numy]]};
numy = 1;
passo = passo + 1;
]
];
matriceP = Append[matriceP, P];
mfugv1 = {{}};
mfugl1 = {{}};
mfugv2 = {{}};
mfugl2 = {{}};
my1 = {{}};
my2 = {{}};
indicek = indicek + 1;
valorek = Append[valorek, k];

(*--------------------------------------------------------THIS
PART------------------------------------------------------------------*)
\

(**Catch*[Do[If[( (matriceP[[i-1,1]]-matriceP[[i-1,-1]])/
matriceP[[i-1,1]])^2>( (matriceP[[i,1]]-matriceP[[i,-1]])/
matriceP[[i,1]])^2,Throw[i+1]],{i,2,Length[valorek]}]]*)
Do[If[( (matriceP[[i - 1, 1]] - matriceP[[i - 1, -1]])/
matriceP[[i - 1, 1]])^2 < ( (
matriceP[[i, 1]] - matriceP[[i, -1]])/matriceP[[i, 1]])^2,
* Goto[thispoint]*], {i, 2, Length[valorek]}];
, {k, -0.2, 0.2, 0.1}
]
* Label[thispoint]*;
ris = Append[{ris},
Join[{P[[nump]]}, {Subscript[y, 1 n]}, {Subscript[y,
2 n]}, {Subscript[f, 1 l][[nump]]}, {Subscript[f, 2 l][[
nump]]}, {Subscript[f, 1 v]}, {Subscript[f,
2 v]}, {Abs[
Subscript[f, 1 l][[nump]] - Subscript[f, 1 v][[numy]]] +
Abs[Subscript[f, 2 l][[nump]] - Subscript[f, 2 v][[numy]]]}]] //
MatrixForm;
DiscretePlot[P[[i]], {i, 1, Length[P]}];

Do[Print["k12: ", valorek[[i]], " Arrive pressure: ",
matriceP[[i, -1]],
" Deviation: ", ( (matriceP[[i, 1]] - matriceP[[i, -1]])/
matriceP[[i, 1]])^2], {i, Length[valorek]}] ]
Table[( (matriceP[[i, 1]] - matriceP[[i, -1]])/
matriceP[[i, 1]])^2, {i,
Length[valorek]}](*Tabella di tutte le deviazioni *)
Print["k12 -> ",
valorek[[Flatten[