# Applying N to the roots found by Solve gives complex numbers when the roots are real [closed]

I have a function which is

f(x) = x^3 - 5 x^2 - x + 1.


When I solve for x to find the zeros

Solve[x^3 - 5 x^2 - x + 1 == 0, x]
N[%]


{{x -> 2.36147 - 1.11022*10^-16 I}, {x -> -2.52892 + 0. I}, {x -> 0.167449 + 0. I}}


and I want to plot these points on the graph but I don't get accurate results. please help!

• First, before asking you should look for similar answers, in fact this was answered many times, see e.g. Finding real roots of negative numbers (for example, −8−−−√3) Commented Sep 21, 2014 at 12:56
• Use Chop. It will cut off very small numbers. In yur case you'll be able to ged rid of the imaginary part. Commented Sep 21, 2014 at 12:57
• Just use NSolve instead of Solve Commented Sep 21, 2014 at 23:29

My first observation is that

{{x -> 2.36147 - 1.11022*10^-16 I}, {x -> -2.52892 + 0. I}, {x -> 0.167449 + 0. I}}


is not a set of solutions for

 x^3 - 5 x^2 - x + 1 == 0


This can be seen by plotting the polynomial

Plot[x^3 - 5 x^2 - x + 1, {x, -1., 6.}]


However, the problem of imaginary fuzz in the roots remains.

Solve[x^3 - 5 x^2 - x + 1 == 0, x] // N

{
{x -> -0.525428 - 4.44089*10^-16 I},
{x -> 0.369102 + 6.66134*10^-16 I},
{x -> 5.15633 - 1.4803*10^-16 I}
}


Solve takes a token, Reals, which instructs it constrain solutions to be over the real numbers. This will eliminate numeric imaginary fuzz.

Solve[x^3 - 5 x^2 - x + 1 == 0, x, Reals] // N

{{x -> -0.525428}, {x -> 0.369102}, {x -> 5.15633}}


Why is there a difference? In the first case, Solve returns a complex expression involving cube roots. Then, when N is applied, the machine numerics used for taking the cube root expressions produce a small error in the imaginary part. In the second Solve avoids the cube roots and returns Root objects.

The imaginary free result can also be achieved with

 Solve[x^3 - 5 x^2 - x + 1 == 0, x, Cubics -> False] // N