Search code examples
pythonsympyequation-solving

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


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


Solution

  • [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