Evaluating a polynomial with Horner's Algorith

2019-07-22 20:08发布

问题:

I need help on my java code. What I'm trying to accomplish is to calculate the size of each step on a polynomial: double s = (b-a)/nsteps; The inputs for the polynomial to be created is degree, coefficient, start value of x, stopping value of x, and the number of steps. Whenever I try to run a test, my output is 0 for x and y and I'm not sure what I am missing on my code. Here is my run test on how its supposed to work, but my result for x and y is 0:

Enter degree:2
Enter coefficient 2:1
Enter coefficient 1:0
Enter coefficient 0:0
f(x) = 1.0x^2 + .0x^1 + 0.0
Enter initial x:0
Enter final x:10
Enter number of steps:20
x = 0.0; f(x) = 0.0
x = 0.5; f(x) = 0.25
x = 1.0; f(x) = 1.0
x = 1.5; f(x) = 2.25
x = 2.0; f(x) = 4.0
x = 2.5; f(x) = 6.25
x = 3.0; f(x) = 9.0
x = 3.5; f(x) = 12.25
x = 4.0; f(x) = 16.0
x = 4.5; f(x) = 20.25
x = 5.0; f(x) = 25.0
x = 5.5; f(x) = 30.25
x = 6.0; f(x) = 36.0
x = 6.5; f(x) = 42.25
x = 7.0; f(x) = 49.0
x = 7.5; f(x) = 56.25
x = 8.0; f(x) = 64.0
x = 8.5; f(x) = 72.25
x = 9.0; f(x) = 81.0
x = 9.5; f(x) = 90.25
x = 10.0; f(x) = 100.0 

and here is my java code:

import java.util.*;

public class PolyAreaTwo{
   //method evalpoly Horner's rule
        public static double evalpoly(double[] c, double x) {
                    int n = c.length - 1;
                    double y = c[n];
                    for (int i = n - 1; i >= 0; i--) {
                        y = c[i] + (x * y);
                    }
                    return y;
        }

    //main method
        public static void main(String[] args){
        Scanner in = new Scanner(System.in);
                 int n;
                 double a, b;
                 int nsteps;


                //input degree
                 System.out.print("Enter degree of polynomial:");
                 n = in.nextInt();

                //input n+1 coefficients
                 double[] c = new double[n+1];

                    for (int i=n; i>=0; i--) {
                            System.out.print("Enter coefficent " + i + ":");
                            c[i] = in.nextDouble();
                 }
                    for (double d : c) { 
                    System.out.print(" x ^ " + d); 

                    }

                //input starting value x = a
                 System.out.println("Enter starting x: ");
                 a = in.nextDouble();

                //input stopping value x = b
                 System.out.print("Enter stop x: ");
                 b = in.nextDouble();

                //input number of steps between starting x and stopping x
                 System.out.print("Enter steps: ");
                 nsteps = in.nextInt();

                //calculate size of each step
                 double s = (b-a)/nsteps;
                 int steps = 0;


             //loop to call the evalpoly method         
                for (double x = a; x <= b; x += s) {
                   double y = evalpoly(c, x);

                   System.out.println("x ="+x+ " , y ="+y);


                } 

            }

}

回答1:

After removing the unnecessary outer while loop; consider using your calculated step size in the for loop: x += s.

As an aside, your implementation of Horner's method in evalpoly() can be made more efficient, as shown here and here, by initializing y to the highest order coefficient.

public static double evalpoly(double[] c, double x) {
    int n = c.length - 1;
    double y = c[n];
    for (int i = n - 1; i >= 0; i--) {
        y = c[i] + (x * y);
    }
    return y;
}