# Grabbing two Roots and Assigning them to subsequent variables?

Posted 5 years ago
5699 Views
|
|
0 Total Likes
|
 So basically I have this assignment where I need to compute a Pxy Diagram of a species Mixture. I have to "guess" x1, x2, P (Pressure) such that all these calculations lead the last line in the code to equal 1.Currently I am just copying and pasting the values of the roots into Zl and Zv. I know I know how to take one root i.e. the first by adding [[1]] but I do not know how to assign that to the appropriate variable. In this case I need the first root to equal Zl and the third root to Zv. This would save me like 30 minutes of copying and pasting. Here is the code. Its not a fun calculation. bar = 10^5; P = 1 bar;(*choose*) x1 = 1;(*choose*) x2 = 0;(*Choose*) omega1 = 0.48 + 1.574 acc1 - 0.176 acc1^2; a1 = 0.42748 (R^2 Tc1^2)/Pc1 (1 + omega1 (1 - Sqrt[Tred1]))^2 b1 = 0.08664 (R Tc1)/Pc1; omega2 = 0.48 + 1.574 acc2 - 0.176 acc2^2; a2 = 0.42748 (R^2 Tc2^2)/Pc2 (1 + omega2 (1 - Sqrt[Tred2]))^2; b2 = 0.08664 (R Tc2)/Pc2; a = x1^2 a1 + x2^2 a2 + 2.0 x1 x2 (1 - k12) Sqrt[a1 a2]; b = x1 b1 + x2 b2; Ap = ( a P)/(R T)^2; Bp = ( b P)/(R T); **NRoots[Z^3 - Z^2 + (Ap - Bp - Bp^2) Z - Ap Bp == 0, Z]** *How do get the first root equal Zl and third to equal Zv without just copying and pasting?* **Zl = 0.005206170900992007;** **Zv = 0.955658798860841**; C1 = Ap/Bp (-b1/b + 2/a*(x1 a1 + x2 Sqrt[a1 a2] (1 - k12))); C2 = Ap/Bp ((-b2/b) + (2/a) (x2 a2 + x1 Sqrt[a1 a2]*(1 - k12))); phiL1 = Exp[b1/b (Zl - 1) - Log[(Zl - Bp)] - C1*Log[(Zl + Bp)/Zl]]; phiV1 = Exp[b1/b (Zv - 1) - Log[(Zv - Bp)] - C1*Log[(Zv + Bp)/Zv]]; phiL2 = Exp[b2/b (Zl - 1) - Log[(Zl - Bp)] - C2*Log[(Zl + Bp)/Zl]]; phiV2 = Exp[b2/b (Zv - 1) - Log[(Zv - Bp)] - C2*Log[(Zv + Bp)/Zv]]; K1 = phiL1/phiV1; K2 = phiL2/phiV2; **x1 K1 + x2 K2 (*Need to satisfy x1 K1+x2 K2\[Equal]1*)** 
 Consider this expression {z1, z2, z3} = Last /@ (List @@ NRoots[(z + 2) (z - 3) (z - 4) == 0, z]) Here's an explanation of what's going on: The NRoots part returns an Or. We convert the Or to a list by applying List@@. That gives us a list of 3 equations. We take the right hand side of each equation using Last/@. Then, we set the values of z1, z2, z3 by assigning the list to {z1, z2, z3}, or whatever names we wish.