Question: I don't know how to do this. Need help please. Thank you. Polynomial Evaluation Learning Objectives : Writing output to a Text File, Using 1-D
I don't know how to do this. Need help please. Thank you.
Polynomial Evaluation Learning Objectives :
Writing output to a Text File,
Using 1-D Arrays,
Using 1-D Arrays as Formal Parameters,
More on Modular Programming
Definition 1. A univariate polynomial is a mathematical expression involving a sum of powers in one variable multiplied by coefficients.
A polynomial in one variable with constant coefficients is given by the expression p(x) = cnx n + cn1x n1 + cn2x n2 + + c1x + c0, where the ci 0s are numeric values representing the coefficients. The polynomial is said to be an nth-degree polynomial if its highest power is n.
For example, 3x 42x 3+x 21 is a fourth-degree polynomial. To evaluate a univariate polynomial, given a numeric value, the value is substituted for the variable and the expression is evaluated.
For example, given the polynomial p(x) = 3x 42x 3+x 21, p(2) = 67 since 3(2)4 2(2)3+(2)21 = 67. A univariate polynomial can be represented as an array of its coefficients in descending powers. For example, the polynomials 3x 4 2x 3 + x 2 1 and 3x 2 + 2x 5 are represented as [3,2,1,0,1] and [3,2,5], respectively.
In this project, you will write a program to evaluate a polynomial using Horners method and without the use of the standard Java Math library Math.pow method. The naive way to evaluate a dense polynomial, one with relatively many non-zero coefficients, is to compute the power of each term and multiply it by the coefficient and then sum all the products. A much more efficient approach for evaluating a dense polynomial is Horners method. The naive approach is very inefficient when evaluating a dense polynomial because it uses a lot more multiplications than necessary.
Horners method for evaluating a univariate polynomial involves the following steps:
1. Initialize the sum to the coefficient of the highest order term,
2. Repeat the following n times, where n is the degree of the polynomial:
(a) Multiply sum by x, where x is the value being used to evaluate the polynomial.
(b) Add the coefficient of the next highest order term to the sum. At the end of these steps, sum will be the value of the polynomial at x.
Given the polynomial p(x) = 2x 4 3x 3 + x 2 + 4x + 1, we compute p(2) using Horners method:
sum = 2 sum = 2(-2) + -3 = -7
sum = -7(-2) + 1 = 15
sum = 15(-2) + 4 = -26
sum = -26(-2) + 1 = 53
So p(2) = 53.
Observe that this uses a lot fewer multiplications than the naive evaluation of this polynomial would have used. The program will consist of three static methods. The Main Method Your program prompts the user to enter the coefficients of the univariate polynomial in order of descending powers, the value to be used to evaluate the polynomial and the name of a text file. The main method does the following:
1. It calls the polyToString method to generate a string representation of the polynomial.
2. It prints the polynomial as shown in the sample run to the screen and writes it to the file.
3. It calls the hornerEval method to evaluate the polynomial.
4. It displays this result on the screen and writes it to the file.
In addition to the main, your program should have the following methods, all defined in the main class:
/** * Evaluates a polynomial using the specified value. * @param coeffs an array of coefficients of a polynomial arranged in * order of descending powers with its first element, the coefficients of * the highest order term being non-zero except when the polynomial is 0. * @param x the value to be used when evaluating the polynomial * @return the value of the polynomial evaluated at the specified value */ public static double hornerEval(double[] coeffs, double x)
/** * Gives a string representation of this polynomial in the format * c_{n}x^n + c_{n-1}x^{n-1} + c_{n-2}x^{n-2} + ... + c_1x + c_0 * For example, given the array [2, -3, 1, -4, 1] it generates the * string 2x^4 - 3x^3 + x^2 - 4x + 1. The string generated should not * include -1, 0 or 1 as coefficients of terms with exponent >= 1 and * should only include 0 when the polynomial is the monomial 0. * @param coeffs an array of coefficients of a polynomial arranged in * order of descending powers with its first element, the coefficients of * the highest order term being non-zero except when the polynomial is 0. * @return a string representation of this polynomial */
public static String polyToString(double[] coeffs)
Your program will consist of only one class, PolynomialEvaluator. Define all the methods listed above in the main class before the main method. Once your program displays the correct output on the screen, modify the program so that it also writes the output to text file. Your program should close the output file. Program Interaction A typical program interaction would be:
Listing 1: Sample Run
1 Enter the degree of the polynomial (deg p) -> 0
2 Enter coefficients of p(x) in descending powers -> 0
3 Enter x to compute p(x) -> -7
4 Enter the output file name -> p(-7)0.txt
5 p(x) = 0.0 6 p(-7.0) = 0.0
Listing 2: Sample Run 2
1 Enter the degree of the polynomial (deg p) -> 0
2 Enter coefficients of p(x) in descending powers -> -5
3 Enter x to compute p(x) -> 8
4 Enter the output file name -> p(8)-5.txt
5 p(x) = -5.0 6 p(8.0) = -5.0
Listing 3: Sample Run 3
1 Enter the degree of the polynomial (deg p) -> 1
2 Enter coefficients of p(x) descending powers -> 2 -4
3 Enter x to compute p(x) -> 5
4 Enter the output file name -> p(5)2-4.txt
5 p(x) = 2.0x - 4.0 6 p(5.0) = 6.0
Listing 4: Sample Run 4
1 Enter the degree of the polynomial (deg p) -> 4
2 Enter coefficients of p(x) descending powers -> 2 0 -3 1 0
3 Enter x to compute p(x) -> -2
4 Enter the output file name -> p(-2)20-310.txt
5 p(x) = 2.0x^4 - 3.0x^2 + x 6 p(-2.0) = 18.0
Additional Requirements
When your code can generate and display the correct output on the screen, do the following:
1. Specify the standard Java language IOException by re-throwing it. Modify the signature of the main method as follows: public static void main (String[] args) throws IOException
2. As the polynomial and the result of its evaluation are printed on the screen, also write both lines to the output file.
3. Do not forget to close the PrintWriter at the end of the main method if you are not using a try-with-resource statement.
4. Finally, no input validation is required. Assume that the degree and list of coefficients entered are valid and that the list of coefficients only begins with 0 for the zero polynomial.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
