Python Sympy : Why can't it find the root of the equation

Question:

I am trying to find the real root of the 5th degree equation shown below. As a result of the solution, there are 4 imaginary and 1 real root, but I found this solution in a different way. I couldn’t get it using python sympy. What could be the reason and how can I solve this in python?

x_pt = -3
y_pt = 3

x = sym.symbols("x" , real = True)

f = (x-3)**3

n= 1 /sym.diff(f,x)

eqn = sym.Eq(y_pt , f + -n * (x_pt - x))

x_roots = sym.solve(eqn)

print(x_roots)

The print is

[CRootOf(3*x**5 - 45*x**4 + 270*x**3 - 819*x**2 + 1270*x - 807, 0)]

I know root is x ≈ 2.278 but Python can not give this number. It gives expression above weirdly !

How can I solve this

Answers:

[sym.N(i) for i in sym.solve(eqn, check=False)]

outputs:

[2.27811833988274,
 2.29407705823809 - 1.36417683043667*I,
 2.29407705823809 + 1.36417683043667*I,
 4.06686377182054 - 0.190009600377279*I,
 4.06686377182054 + 0.190009600377279*I]

or, without check=False,

[sym.N(i) for i in sym.solve(eqn)] outputs [2.27811833988274]

Got this from: https://stackoverflow.com/a/36811640/11574713

Answered By: 11574713

The CRootOf object is a representation you can use to further work with the result.
If you just want to get one of the possible results you can evaluate the expression:

e.g. up to a certain precision

# ...
x_roots = sym.solve(eqn)
print(x_roots[0].eval_approx(10))

for full reference see (https://docs.sympy.org/latest/modules/polys/reference.html#sympy.polys.rootoftools.ComplexRootOf.eval_approx)

Answered By: lwohlhart
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.