w3resource logo
Python Exercises

Python NumPy: Compute the given polynomial values

Python NumPy Math, Statistics, Trigonometry and Linear algebra: Exercise-17 with Solution

Write a Python program to compute the following polynomial values.
a) x2 − 4x + 7, when x = 2
b) x4 − 11x3 + 9x2 + 11x – 10, when x = 3

Sample Solution:-

Python Code:

import numpy as np
print("Polynomial value when x = 2:")
print(np.polyval([1, -2, 1], 2))
print("Polynomial value when x = 3:")
print(np.polyval([1, -12, 10, 7, -10], 3))

Sample Output:

Polynomial value when x = 2:                                           
1                                                                      
Polynomial value when x = 3:                                           
-142

Python Code Editor:

Improve this sample solution and post your code through Disqus