Roots of Polynomials

4.1. Roots of Polynomials#

Example

Let us find the roots of \(x^3 - 6 x^2 + 11 x -6\). You can factorize it as \((x-1)(x-2)(x-3)\) and thus the roots are obviously 1,2, and 3. Let us find them using sympy.

# We use sympy package
from sympy import symbols, poly

# define the symbol of the variable
x = symbols("x")

# find the root of the polynomial
roots = poly(x**3 - 6*x**2 + 11*x - 6).all_roots()

print(roots)
[1, 2, 3]

where poly module defines the polynomial and all_roots is a method that belongs to the object poly. OK. This was easy. Try $\(x^3 - 6 x^2 + 11 x -5\). Factorization seems difficult for this polynomial.

# find the root of the second polynomial
roots = poly(x**3 - 6*x**2 + 11*x - 5).all_roots()

print(roots)
[CRootOf(x**3 - 6*x**2 + 11*x - 5, 0), CRootOf(x**3 - 6*x**2 + 11*x - 5, 1), CRootOf(x**3 - 6*x**2 + 11*x - 5, 2)]

This output means that sympy could not find the symbolic answers. Now, we try get numerical roots.

# numerically evaluate each root with 15 digits if accuracy
nroots = [r.evalf(15) for r in roots]
print(nroots)
[0.675282042755254, 2.66235897862237 - 0.562279512062301*I, 2.66235897862237 + 0.562279512062301*I]

where evalf(15) means evaluate the object in floating point with 15 digits of accuracy. One of the output is a real root and two others are complex roots. It looks so simple so far. However, symbolic methods becomes very time consuming for higher degrees. It most likely fail above 20 degree. In the next section, general root-finding algorithms are introduced, which works for polynomials as well.


Last modified on 3/9/2024 by R. Kawai.