w3resource

NumPy: Find the roots of the given polynomials

NumPy Mathematics: Exercise-16 with Solution

Write a NumPy program to find the roots of the following polynomials.
a) x2 − 4x + 7.
b) x4 − 11x3 + 9x2 + 11x – 10

Sample Solution:

Python Code:

import numpy as np
print("Roots of the first polynomial:")
print(np.roots([1, -2, 1]))
print("Roots of the second polynomial:")
print(np.roots([1, -12, 10, 7, -10]))

Sample Output:

Roots of the first polynomial:                                         
[ 1.  1.]                                                              
Roots of the second polynomial:                                        
[ 11.04461946+0.j         -0.87114210+0.j          0.91326132+0.4531004
j                                                                      
   0.91326132-0.4531004j]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to generate a matrix product of two arrays.
Next: Write a NumPy program to compute the following polynomial values.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Getting rid of unwanted characters:

You can get rid of whitespaces or any specific character using strip methods in Python. You can use either plain strip for both sides, lstrip for the left side and rstrip for the right side only.

str="+++Welcome python+++"
str.strip()

print(str)

Output:

+++Welcome python+++