NumPy: Find the roots of the given polynomials
16. Polynomial Roots
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:
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]
Explanation:
In the above code –
np.roots([1, -2, 1]) - Here, we have an input array [1, -2, 1], which corresponds to the polynomial x^2 - 2x + 1. The output of np.roots() will be the roots of this polynomial.
np.roots([1, -12, 10, 7, -10]) – Here, we have an input array [1, -12, 10, 7, -10], which corresponds to the polynomial x^4 - 12x^3 + 10x^2 + 7x - 10. The output of np.roots() will be the roots of this polynomial.
For more Practice: Solve these Related Problems:
- Implement a function that finds the roots of a quadratic polynomial using np.roots and tests for complex roots.
- Compute the roots of a quartic polynomial and verify that the output correctly handles complex conjugate pairs.
- Create a solution that finds repeated roots and checks the multiplicity using np.roots.
- Test the root finder on polynomials with non-integer coefficients and compare with analytical solutions.
Python-Numpy Code Editor:
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 Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics