Question

In: Computer Science

Write a python program to evaluate polynomials in general and test it on the following polynomial:...

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.

Solutions

Expert Solution

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.


Related Solutions

Write a pyhton program and to evaluate polynomials in general and test it on the following:...
Write a pyhton program and to evaluate polynomials in general and test it on the following: ? 4 − 3? 3 − 39? 2 + 47? + 170 = 0 a) Develop and test a function named evalPoly() 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...
Beginning Python Programming - Sorting: Write and test a Python program to print a set of...
Beginning Python Programming - Sorting: Write and test a Python program to print a set of real numbers in descending order. The program should also print out the median of the numbers input and how many numbers were input. The program should read in numbers until a negative number is read. The negative number serves as a sentinel or marker telling the program when to stop reading numbers. The program should then sort the numbers and print them out in...
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program...
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program should display a letter grade for each score and the average test score. Hint: Declare local variables under main() program Prompts the user to enter 5 test scores Define a function to calculate the average score: this should accept 5 test scores as argument and return the avg Define a function to determine the letter grade: this should accept a test score as argument...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
Write a program in Python language which will do the following: Write a program to prompt...
Write a program in Python language which will do the following: Write a program to prompt the user to enter a company name, city, state and zip code. Create a dictionary with the data. Output the dictionary. Then remove the city from the dictionary and output again.
Write a program to multiply two polynomials. Code needed in Java.
Write a program to multiply two polynomials. Code needed in Java.
Write and test a Python program to print a set of real numbers in descending order....
Write and test a Python program to print a set of real numbers in descending order. The program should also print out the median of the numbers input and how many numbers were input. The program should read in numbers until a negative number is read. The negative number serves as a sentinel or marker telling the program when to stop reading numbers. The program should then sort the numbers and print them out in order from largest to smallest....
Write a complete and syntactically correct Python program to solve the following problem: Write a program...
Write a complete and syntactically correct Python program to solve the following problem: Write a program for your professor that allows him to keep a record of the students’ average grade in his class. The program must be written in accordance with the following specs: 1. The input must be interactive from the keyboard. You will take input for 12 students. 2. You will input the students’ name and an average grade. The student cannot enter an average below zero...
Write a menu program to have the above options for the polynomials. Your menu program should...
Write a menu program to have the above options for the polynomials. Your menu program should not use global data; data should be allowed to be read in and stored dynamically. Test your output with the data below. Poly #1: {{2, 1/1}, {1, 3/4}, {0, 5/12}} Poly #2: {{4, 1/1}, {2, -3/7}, {1, 4/9}, {0, 2/11}} provide a C code (only C please) that gives the output below: ************************************ *         Menu HW #4 * * POLYNOMIAL OPERATIONS * * 1....
#Python 5. Write function called evaluate() that evaluates the following Python expressions or assignments as specified:...
#Python 5. Write function called evaluate() that evaluates the following Python expressions or assignments as specified: Request input from the user for three variables (floating-point or integer) x, y, z, and myAverage. If the average of the first three numbers equals the fourth number, print 'Your average is correct.'. If not print 'Your average is not correct'. and print the correct average. Print the largest value among x, y, and z. Print the minimum value of x, y, y. >>>...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT