本文介绍了使用霍纳算法评估多项式并计算步骤(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关Java代码的帮助.我要完成的工作是计算多项式上每个步骤的大小:double s = (b-a)/nsteps;要创建的多项式的输入是度,系数,x的起始值,x的终止值以及步数.每当我尝试运行测试时,我的输出是xy0,并且我不确定代码中缺少什么.这是我对它应该如何工作的运行测试,但是我对xy的结果是0:

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

这是我的Java代码:

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);


                }

            }

}

推荐答案

在删除了不必要的外部while循环之后;考虑在for循环中使用计算出的步长:x += s.

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

顺便说一句,您在 霍纳方法 中的实现如此处此处,方法是将y初始化为最高阶系数.

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;
}

这篇关于使用霍纳算法评估多项式并计算步骤(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:06