Question

In: Computer Science

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 evaluate the polynomial at ? = -5.2, -5.0, -1.8, -1.6, 2.6, 2.8, 7.0 and 7.2. What do you conclude about the solutions to the equation above?

b) Write a second function named solvePoly() 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

Here is the code:

import numpy as np
# 1) Write a pyhton program and to evaluate polynomials in general and
# test it on the following: ? 4 − 3? 3 − 39? 2 + 47? + 170 = 0

poly_result = np.poly1d([1,-3,-39,47,170]) 
poly_result


# 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.

def evalPoly(coeff,variable):
    poly = np.poly1d(coeff)
    poly_result = poly(variable)
    
    return(poly_result)

# Use your function to evaluate the polynomial at ? = -5.2, -5.0, -1.8, -1.6, 2.6, 2.8, 7.0 and 7.2
x = [-5.2, -5.0, -1.8, -1.6, 2.6, 2.8, 7.0, 7.2]
coeff = [1,-3,-39,47,170]
result = list()
for item in x:
    result.append(evalPoly(coeff,item))
    print(evalPoly(coeff,item))



import matplotlib.pyplot as plt   # plotting the results
plt.plot(result, x)

# Conclustion: results are varying between positive and negative axes


# Write a second function named solvePoly() 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

def solvePoly(coeff,value1,value2):
    poly = np.poly1d(coeff)
    poly_result1 = poly(value1)
    while(True):
        poly_result2 = poly(value1)
        value1 += 0.2    # This function is based on an exhaustive search
        poly_result1 = poly(value1)
        if(poly_result1 < 0 and poly_result2 > 0):
            return(value1-0.2)   # It should return the value of the variable at which a solution was found.
    
# calling the function
solvePoly(coeff,-1.8, -1.6)

Here is the results:


Related Solutions

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...
Write a pyhton program to read from a file the names and grades of a class...
Write a pyhton program to read from a file the names and grades of a class of students, to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a dataInput() function that reads data from a file and stores it and returns it as a dictionary....
Python This week you will write a program in Pyhton that mimics an online shopping cart...
Python This week you will write a program in Pyhton that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers...
CODE IN PYHTON Write a program which DECRYPTS the secret messages which are created by the...
CODE IN PYHTON Write a program which DECRYPTS the secret messages which are created by the ENCRYPT program which we went over in class. It should first prompt the user for the scrambled alphabet which was created by ENCRYPT (which you should be able to copy & paste from the preceeding program's run). It then should ask for the secret message. Finally, it outputs the unscrambled version. Note that there are exactly 26 characters input for the scrambled alphabet. All...
Write a pyhton program that reads a stream of bits, e.g. 11001, and calculates and prints...
Write a pyhton program that reads a stream of bits, e.g. 11001, and calculates and prints the decimal value of the binary number represented by the entered bits, i.e. 25 in this case.
Write a program to multiply two polynomials. Code needed in Java.
Write a program to multiply two polynomials. Code needed in Java.
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....
Write a program that adds and subtracts two polynomials. It creates an array of nodes and...
Write a program that adds and subtracts two polynomials. It creates an array of nodes and connects them into the freeStore. This implementation uses one array to store multiple array to store multiple polynomial instances and the free store. I need help to finish the LinkedListInArrayPolynomial class. Output should look like below: Forth test is linked list of terms in an array. linkInArray1 = 3x^11+4x^10+4x^4 linkInArray2 = 4x^19+5x^14-3x^12-78 sum of linkInArray1 and linkInArray2 = 4x^19+5x^14-3x^12+3x^11+4x^10+4x^4-78 linkInArray1 minus linkInArray2 = -4x^19-5x^14+3x^12+3x^11+4x^10+4x^4+78...
Write a program and test a program that translates the following Bubble Sort algorithm to a...
Write a program and test a program that translates the following Bubble Sort algorithm to a bubblesort function. The function's prototype is, void bubblesort(int a[], int size); Bubble Sort The inner loop moves the largest element in the unsorted part of the array to the last position of the unsorted part of the array; the outer loop moves the last position of the unsorted part of the array. The Bubble sort exchanges elements with adjacent elements as it moves the...
Write the following function and provide a program to test it (main and function in one...
Write the following function and provide a program to test it (main and function in one .py) def repeat(string, n, delim) that returns the string string repeated n times, separated by the string delim. For example repeat(“ho”, 3, “,”) returns “ho, ho, ho”. keep it simple.Python
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT