# Using “if” to compare 2 expressions (Complex and real numbers) Hello. I want to solve this cubic equation and compare the answers then I can assing the lower result to "VmL" and the greatest result to "VmG" but I have problems when the result is a complex number and two real, Is there anyone who can help me ? I don't really know how to program with Mathematica (I'm a rookie) so, if anyone can help me with a basic example I'll appreciate so much.

And if the answer have only one real number I want to see that value with the other two ones to change the value of "p".

Thanks.

Code:

ClearAll["Global*"];
R = 83.14; (* cm3*bar/mol*K *)
T = 480;(*K*)
(*Datos para el Heptano*)
Pc = 27.40; (*bar*)
Tc = 540.20; (*K*)
a = 27/64*(R*Tc)^2/Pc;(*cm^6*bar/mol^2*)
b = 1/8*(R*Tc)/Pc; (*cm^3/mol*)
Vc = 3/8*(R*Tc)/Pc;(*cm^3/mol*)
Zc = Rationalize[(Pc*Vc)/(R*Tc)];
For[p = 10, p < 30, p *= ob1,
ob[Vm1_] := Vm1^3 - ((R*T)/p + b) Vm1^2 + a/p Vm1 - (a*b)/p;
\[Phi][T_, Vm1_] :=
E^(b/(Vm1 - b) - (2*a)/(R*T*Vm1) -
Log[1 - (a (Vm1 - b))/(R*T*Vm1^2)]);
f[T_, Vm1_] := p*\[Phi][T, Vm1]; (*bar*)
Vm = Vm1 /. Solve[ob[Vm1] == 0, Vm1];
If[Vm[] < Vm[], {VmL = Vm[], VmG = Vm[]}, 0];
ob1 = f[T, VmL]/f[T, VmG];
If[ob1 == 1, Break[]]
];
p

• Please provide the code as a text, such that we could copy and test. We can help with formatting the code, but is a lot of work to type the code from an image. – Johu Sep 30 '18 at 10:32
• Thanks, now you can see the code – Hulycez Cruz Oct 1 '18 at 12:31

If you just want to ignore the imaginary solutions (not physical) then you can specify the domain to be Reals from
Solve[ob[Vm1]==0,Vm1,Reals]

Then Solve will only return real solutions and you can sort them using Sort, which also takes care of corner cases like 1 or 3 solutions, which is hard to do with If` statements.