In: Computer Science
Write a python program to evaluate polynomials in general and test it on the following polynomial: ? 4 − 3? 3 − 39? 2 + 47? + 90 = 0
a) Develop and test a function named polyCalc() that evaluates a polynomial at a given value of the variable. The function receives two arguments: a list of the polynomial coefficients, and the value of the variable. It returns the result of the evaluation as a float. Use your function to evaluate the polynomial at ? = -5.4, -5.2, -1.2, -1.0, 2.0, 2.2, 7.2 and 7.4. What do you conclude about the solutions to the equation above?
b) Write a second function named polySolve() that seeks a solution of the polynomial between two values of the variable where the function has changed sign. This function should receive the list of coefficients, the two values of the variable between which there is a solution. It should return the value of the variable at which a solution was found. This function may be based on an exhaustive search, a bisection search, or on Newton-Raphson’s method. Select a method with some justification.
def polyCalc(coefficient, x):
# find no of terms of coefficient
length = len(coefficient)
total = 0
e = length - 1
for i in range(length):
total += coefficient[i]*(x**e)
e -= 1
# return value of polynomial
return total
# polynomial coefficient list
coefficient = [1, -3, -39, 47, 90]
# list of x values
X = [ -5.4, -5.2, -1.2, -1.0, 2.0, 2.2, 7.2, 7.4]
# check for each value of X
for i in range(len(X)):
print("For x =",X[i]," polynomial generates:", polyCalc(coefficient, X[i]))
___________________________________________________________________
___________________________________________________________________
For x = -5.4 polynomial generates: 21.657600000000173
For x = -5.2 polynomial generates: -55.974399999999974
For x = -1.2 polynomial generates: -15.302400000000006
For x = -1.0 polynomial generates: 8.0
For x = 2.0 polynomial generates: 20.0
For x = 2.2 polynomial generates: -3.8784000000000276
For x = 7.2 polynomial generates: -25.71840000000026
For x = 7.4 polynomial generates: 85.14560000000012
___________________________________________________________________
The values of the polynomial function did sign change 4 times. It means there are the roots in the following intervals:
1. (-5.4, -5.2)
2. (-1.2, -1.0)
3. (2, 2.2)
4. (7.2, 7.4)
___________________________________________________________________
def polySolve(coefficent, x):
# find no of terms of coefficient
length = len(coefficient)
total = 0
e = length - 1
for i in range(length):
total += coefficient[i]*(x**e)
e -= 1
# return value of polynomial
return total
# bisection method
def bisection(coefficient, a,b):
if (polySolve(coefficient, a) * polySolve(coefficient, b) >= 0):
return
c = a
while ((b-a) >= 0.001):
# middle point
c = (a+b)/2
# Check if middle point is root
if (polySolve(coefficient, c) == 0.0):
break
# Decide the side to repeat the steps
if (polySolve(coefficient, c)* polySolve(coefficient, a) < 0):
b = c
else:
a = c
print("The value of root is : ","%.4f"%c)
# polynomial coefficient list
coefficient = [1, -3, -39, 47, 90]
# list of x values
X = [ -5.4, -5.2, -1.2, -1.0, 2.0, 2.2, 7.2, 7.4]
# check for each value of X
for i in range(1, len(X)):
bisection(coefficient, X[i-1],X[i]);
___________________________________________________________________
The value of root is : -5.3477
The value of root is : -1.0711
The value of root is : 2.1695
The value of root is : 7.2492
___________________________________________________________________
Note: If you have queries or confusion regarding this
question, please leave a comment. I would be happy to help you. If
you find it to be useful, please upvote.