enter image description here

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".



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[[1]] < Vm[[3]], {VmL = Vm[[1]], VmG = Vm[[3]]}, 0];
    ob1 = f[T, VmL]/f[T, VmG];
    If[ob1 == 1, Break[]]
  • 1
    $\begingroup$ 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. $\endgroup$
    – Johu
    Sep 30, 2018 at 10:32
  • $\begingroup$ Thanks, now you can see the code $\endgroup$ Oct 1, 2018 at 12:31

1 Answer 1


If you just want to ignore the imaginary solutions (not physical) then you can specify the domain to be Reals from


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.

  • $\begingroup$ I'll see if it works and if so, I'll ✓ your answer, I swear it Thanks $\endgroup$ Oct 1, 2018 at 12:37
  • $\begingroup$ Thank you, your answer really helped me a lot. $\endgroup$ Oct 2, 2018 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.