Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

NumPy - Finding Roots of Polynomials



Finding Roots of Polynomials in NumPy

Finding the roots of a polynomial means determining the values of x where the polynomial equals zero. In NumPy, you can find the roots of a polynomial using the numpy.roots() function.

Roots are fundamental in various mathematical applications, such as solving equations, optimization problems, and analyzing the behavior of polynomials in signal processing or numerical analysis.

The numpy.roots() Function

The numpy.roots() function takes the coefficients of a polynomial as an input and returns the roots of the polynomial.

The polynomial is expressed in terms of its coefficients, starting with the highest power and ending with the constant term. The function finds all the roots of the polynomial, including real and complex roots.

Example

In this example, we find the roots of a polynomial defined by the equation using the roots() function in NumPy −

import numpy as np

# Define the coefficients of the polynomial 1x - 6x + 11x - 6
coefficients = np.array([1, -6, 11, -6])

# Find the roots of the polynomial using numpy.roots
roots = np.roots(coefficients)

print("Roots of the polynomial:", roots)

The result of finding the roots for the given polynomial is as follows −

Roots of the polynomial: [3. 2. 1.]

Real and Complex Roots

NumPy handles both real and complex roots. If the polynomial has complex roots, numpy.roots() will return them as complex numbers. For example, the roots of a quadratic polynomial with no real roots will appear as complex numbers.

Example: Finding Complex Roots

In this example, we find the roots of a quadratic polynomial using the numpy.roots() function −

import numpy as np

# Define the coefficients of the polynomial x + 1
coefficients_complex = np.array([1, 0, 1])

# Find the roots of the polynomial using numpy.roots
roots_complex = np.roots(coefficients_complex)

print("Roots of the complex polynomial:", roots_complex)

The result of finding the roots of the complex polynomial is as shown below −

Roots of the complex polynomial: [-0.+1.j  0.-1.j]

Verifying the Roots

After finding the roots, it is often useful to verify that they satisfy the original polynomial equation. This can be done by substituting the roots back into the polynomial and checking if the result is close to zero.

Example

In this example, we verify the roots of a polynomial by evaluating the polynomial at the roots −

import numpy as np

# Define the polynomial coefficients
coefficients = np.array([1, -6, 11, -6])

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

# Verify the roots by evaluating the polynomial at the roots
verification = np.polyval(coefficients, roots)

print("Verification results:", verification)

The verification results (should be close to zero for all roots) is −

Verification results: [3.55271368e-15 2.66453526e-15 0.00000000e+00]

Handling Higher-Degree Polynomials

The NumPy roots() function is capable of finding the roots of polynomials of any degree. As the degree of the polynomial increases, the number of roots increases as well. The function will return all roots, real and complex, of the given polynomial.

Example

In this example, we find the roots of a polynomial of degree 5 using the roots() function −

import numpy as np

# Define the coefficients of the polynomial 1x - 3x + 5x - 7x + 11x - 13
coefficients_high_degree = np.array([1, -3, 5, -7, 11, -13])

# Find the roots of the polynomial using numpy.roots
roots_high_degree = np.roots(coefficients_high_degree)

print("Roots of the higher-degree polynomial:", roots_high_degree)

The result of finding the roots for the higher-degree polynomial is as follows −

Roots of the higher-degree polynomial: [-0.56466348+1.42799478j -0.56466348-1.42799478j  1.18589358+1.31548183j
1.18589358-1.31548183j  1.7575398 +0.j        ]
Advertisements