# Find the roots of the polynomials using NumPy

Finding the roots of polynomials is an essential operation in mathematics, and NumPy provides an easy and efficient way to accomplish this task. NumPy is a powerful library for scientific computing in Python, and its functions for polynomial manipulation are especially useful.

## Syntax

The NumPy library has a function named roots() that can be used to find the roots of a polynomial.

numpy.roots(p)


where p is the polynomial coefficients represented as a 1D array or sequence. The function returns an array of the roots of the polynomial.

## Algorithm

With NumPy, finding a polynomial's roots is a rather straightforward process and it entails giving the coefficients of the polynomial to the roots() function, which will then provide the polynomial's roots. Here is a detailed procedure for using NumPy to determine a polynomial's roots −

• Import the NumPy library.

• Define the coefficients of the polynomial as a 1D array or sequence.

• Use the polynomial coefficients as the parameter when calling the roots() method.

• The polynomial's roots are returned as an array by the roots() method.

## Example 1

Take the quadratic polynomial p(x) = x2 + 3x + 2 as an example. Using NumPy, we can do the following to determine this polynomial's roots −

import numpy as np
p = [1, 3, 2]
roots = np.roots(p)
print(roots)


## Output

[-2. -1.]


Import the NumPy library and define the quadratic polynomial's coefficients as the list [1, 3, 2], which equals x2 + 3x + 2. The polynomial coefficients are sent to the roots() method at the end, and the function returns an array containing the polynomial's roots.

## Example 2

Consider the cubic polynomial p(x) = x3 - 6x2 + 11x - 6. Using NumPy, we can perform the following to determine this polynomial's roots −

import numpy as np
p = [1, -6, 11, -6]
roots = np.roots(p)
print(roots)


## Output

[3. 2. 1.]


In this illustration, the cubic polynomial's coefficients are defined as the list [1, -6, 11, -6], which equals x3 - 6x2 + 11x - 6. The roots() method is then invoked with the polynomial coefficients as its input, and it produces an array containing the polynomial's roots.

## Example 3

import numpy as np

# Define the coefficients of the third-degree polynomial
p = np.array([1, -7, 14, -8])

# Find the roots of the polynomial
roots = np.roots(p)

# Print the roots of the polynomial
print(roots)


## Output

[4. 2. 1.]


## Applications

The ability to find the roots of polynomials is useful in many areas of mathematics and science. Some of the applications of polynomial root finding are −

• Signal processing − used to analyze the frequency response of filters.

• Control theory − used to design controllers that stabilize unstable systems.

• Physics − used to solve problems in classical mechanics and quantum mechanics.

• Numerical analysis − used to solve differential equations and to find the extrema of functions

• Optimization − used to find the minimum or maximum of a function.

• Graph theory − used to determine the chromatic number of a graph.

## Conclusion

This topic has discussed how to find polynomial roots using Python's NumPy package. The NumPy package offers the two functions np.poly1d() and np.roots() that are essential for finding polynomial roots and in extension it can be used to find the roots of polynomials of varying degrees. Finding polynomial roots is a common practice in many mathematical and scientific fields and hence, as a result, a wide spectrum of experts in diverse disciplines may find this content to be useful. Users may utilize Python's NumPy module to rapidly and precisely calculate data with the information and code examples given in this tutorial.

Updated on: 09-May-2023

590 Views 