A while into another while

• To: mathgroup at smc.vnet.net
• Subject: [mg121704] A while into another while
• From: Mariano Pierantozzi <mariano.pierantozzi at gmail.com>
• Date: Mon, 26 Sep 2011 20:05:08 -0400 (EDT)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com

```I'm wondering if exist a function like a dowhile or a dountil in
Mathematica.
I'm trying to put a while into another while, but the calculations do not
arrive at end.
I know that my codes is now too long, but i'm going to ask the same because
i don't know anyone that can help me; only this group!
Thank you for your help or suggestions.
MP

\[Omega] = {0.1521, 0.32684};
x = {0.0991, 1 - 0.0991};
Subscript[T, c] = {369.89, 374.21};
Subscript[P, c] = {42.512, 40.593};
sostanze = {propane, HFC - 143 a};
R = 83.14;
T = 273.15;
tollerancep = 0.5;
tollerancey = 0.5;
incrP = 0.01;
Subscript[T, r] = Table[T/Subscript[T, c][[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 \!\(
\(\*SubsuperscriptBox[\(T\), \(c\), \(2\)]\)[\(\([\)\(i\)\(]\)\)]\))/
Subscript[P, c][[i]] , {i, 2}];
b = Table[(0.07779607 R Subscript[T, c][[i]] )/
Subscript[P, c][[i]], {i, 2}];
(*--------------------INIZIO-----------------*)
P = {2.5};
Subscript[y, 1] = {0.3596};
Subscript[y, 2] = {1 - 0.3596};
(*number iterations for y*)
numy = 1;
(*number iterations for  P*)
nump = 1;
(*empty vector for f*)
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]]];
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])] ((
2 (x[[1]] A[[1]] + x[[2]] Subscript[A, 12]))/Subscript[A, liq] -
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])] ((
2 (x[[1]] Subscript[A, 12] + x[[2]] A[[2]]))/Subscript[A, liq] -
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])] ((
2 (Subscript[y, 1][[numy]] A[[1]] +
Subscript[y, 2][[numy]] Subscript[A, 12]))/Subscript[A,
vap] - 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])] ((
2 (Subscript[y, 1][[numy]] Subscript[A, 12] +
Subscript[y, 2][[numy]] A[[2]]))/Subscript[A, vap] - 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]]];
Subscript[f, 1 v][[numy]]
Subscript[f, 2 l][[numy]]
Subscript[f, 2 v][[numy]]
(*---------------BEGIN FIRST WHILE-----------------*)
While[Abs[Subscript[f, 1 l][[numy]] - Subscript[f, 1 v][[numy]]] +
Abs[Subscript[f, 2 l][[numy]] - 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]];
(*---------------BEGIN SECOND WHILE-----------------*)
While[Abs[Subscript[y, 1][[numy]] - Subscript[y, 1][[numy - 1]]] +
Abs[Subscript[y, 2][[numy]] - Subscript[y, 2][[numy - 1]]] >
tollerancey,
Subscript[B, vap] =
Subscript[y, 1][[numy]]*B[[1]] + Subscript[y, 2][[numy]]*B[[2]];
(* -----------If i put a comment here the program stops \
itself---------------*)
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])] ((
2 (Subscript[y, 1][[numy]] A[[1]] +
Subscript[y, 2][[numy]] Subscript[A, 12]))/Subscript[A,
vap] - 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])] ((
2 (Subscript[y, 1][[numy]] Subscript[A, 12] +
Subscript[y, 2][[numy]] A[[2]]))/Subscript[A, vap] -
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]]];]
(*END FIRST While y*)
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]]];
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])] ((
2 (x[[1]] A[[1]] + x[[2]] Subscript[A, 12]))/Subscript[A, liq] -
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])] ((
2 (x[[1]] Subscript[A, 12] + x[[2]] A[[2]]))/Subscript[A, liq] -
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])] ((
2 (Subscript[y, 1][[numy]] A[[1]] +
Subscript[y, 2][[numy]] Subscript[A, 12]))/Subscript[A,
vap] - 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])] ((
2 (Subscript[y, 1][[numy]] Subscript[A, 12] +
Subscript[y, 2][[numy]] A[[2]]))/Subscript[A, vap] - 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]]];
If[Abs[Subscript[f, 1 l][[numy]] - Subscript[f, 1 v][[numy]]] +
Abs[Subscript[f, 2 l][[numy]] - Subscript[f, 2 v][[numy]]] >
tollerancep, nump = nump + 1; P = Append[P, P[[nump - 1]] + incrP];
Subscript[y, 1] = {Subscript[y, 1][[numy]]};
Subscript[y, 2] = {Subscript[y, 2][[numy]]}; numy = 1, "BRAVO1"]
]
If[Abs[Subscript[f, 1 l][[numy]] - Subscript[f, 1 v][[numy]]] +
Abs[Subscript[f, 2 l][[numy]] - Subscript[f, 2 v][[numy]]] >
tollerancep, nump = nump + 1;
P = Append[P, P[[nump - 1]] + incrP], "BRAVO2"]
P[[nump]]

(*END SECOND While p*)

```

• Prev by Date: Re: More FinancialData glitches
• Next by Date: Re: Count Number of Iteration [FindRoot]
• Previous by thread: NMinimize not returning the best solution
• Next by thread: Constrain locator