0
$\begingroup$

I'm using NRoots to quickly extract the roots for cubic polynomials, as my testing showed this was faster then either FindRoot or NSolve for this particularly simple case. However, I only need the real solutions. But according to the documentation, NRoots does not take Reals as an option. Is there a simple way to pick the real solutions out?

$\endgroup$
1
  • 6
    $\begingroup$ FYI: A concrete example in Mathematica code almost always speeds the appearance of good answers. $\endgroup$
    – Mr.Wizard
    Feb 17, 2017 at 15:25

1 Answer 1

3
$\begingroup$

Maybe like that

Cases[x /. {ToRules@NRoots[x^3 + 2 x + 1 == 0, x]}, _Real]
$\endgroup$
1
  • $\begingroup$ Or if you wish the result to be rules: Cases[{ToRules@ NRoots[x^3 + 2 x + 1 == 0, x]}, _?(Element[x /. #, Reals] &)] or Select[{ToRules@NRoots[x^3 + 2 x + 1 == 0, x]}, Element[x /. #, Reals] &] $\endgroup$
    – Bob Hanlon
    Feb 17, 2017 at 18:59

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