0

我为我的一门 com sci 课程设计了一个多项式类,我在获取正确的集成方法时遇到了问题,有人可以帮助我吗?

/** The polynomial class includes the methods: evaluate , add, multiply, 
 * Differentiate , integrate and square root. 
 */

public class polynomial {

private int degree;
private double[] coefficients;


// a constructor that creates a polynomial of degree degMax with all the coefficients are zeroes
public polynomial(int degMax) {
    degree= degMax; 
    coefficients = new double[degree + 1];  
}


// a setter method that let the users set the coefficients for the polynomial they constructed
public void setCoefficient(int d , double v ){
    if (d > degree) 
    {
        System.out.println("Erorr Message: the degree you specified is larger than the polynomial's degree that you have created ");
    }
    else {
        coefficients[d]=v;  
    }   
}

// a getter method to return the coefficient for the specified degree 
public double getCoefficient(int i){
    return coefficients[i];
}

// private method that counts the degree of the polynomial by searching for the last element in the coefficient array that
// does not contain zero
private int getDegree() {
    int deg = 0;
    for (int i = 0; i < coefficients.length; i++)
        if (coefficients[i] != 0) deg = i;
    return deg;
}

// a method that print out the polynomial as a string   
public String print(){
    if (degree ==  0) return "" + coefficients[0];
    if (degree ==  1) return coefficients[1] + "x + " + coefficients[0];
    String s = coefficients[degree] + "x^" + degree;
    for (int i = degree-1; i >= 0; i--) {
        if      (coefficients[i] == 0) continue;
        else if (coefficients[i]  > 0) s = s + " + " + ( coefficients[i]);
        else if (coefficients[i]  < 0) s = s + " - " + (-coefficients[i]);
        if      (i == 1) s = s + "x";
        else if (i >  1) s = s + "x^" + i;
    }
    return s;
}

// a method that evaluate the polynomial at specified value x 
 public double evaluate(double x) {
        double result = 0;
        for (int i = degree; i >= 0; i--)
            result = coefficients[i] + (x * result);
        return result;

    }

 // a method that perform symbolic addition of two polynomial 
 public polynomial addition(polynomial p2) {
        polynomial p1 = this;
        polynomial p3 = new polynomial(Math.max(p1.degree, p2.degree));
        for (int i = 0; i <= p1.degree; i++) p3.coefficients[i] += p1.coefficients[i];
        for (int i = 0; i <= p2.degree; i++) p3.coefficients[i] += p2.coefficients[i];
        p3.degree = p3.getDegree();
        return p3;
    }

 // a method that performs a symbolic multiplication 
 public polynomial multiply(polynomial p2) {
        polynomial p1 = this;
        polynomial p3 = new polynomial(p1.degree + p2.degree); 
        for (int i = 0; i <= p1.degree; i++)
            for (int j = 0; j <= p2.degree; j++)
                p3.coefficients[i+j] += (p1.coefficients[i] * p2.coefficients[j]);
        p3.degree = p3.getDegree();
        return p3;
    }

 // a method that apply differentiation to polynomial  
 public polynomial differentiate() {
        if (degree == 0) return new polynomial(0);
        polynomial derivative = new polynomial(degree - 1);
        derivative.degree = degree - 1;
        for (int i = 0; i < degree; i++){
            derivative.coefficients[i] = (i + 1) * coefficients[i + 1]; 
        }
        return derivative;
    }

 // a method that find a polynomial integral over the interval a to b 
 public double integration(double a , double b) {
     polynomial integral= new polynomial (degree+1);
     integral.degree= degree+1;
     for (int i=0 ; i<= degree+1 ; i++){
         if (i==0) {
             integral.coefficients[i]= 0;
         }
         else {
         integral.coefficients[i]= (coefficients[i-1]/i); 

         }
         }
     return (evaluate(b)- evaluate(a));
 }





public static void main(String[] args) {

polynomial p1   = new polynomial(3);
p1.setCoefficient(0, 3.0);
p1.setCoefficient(3, 5.0);   
String r = p1.print();   //3.0 + 5.0 x^3

polynomial p2   = new polynomial(2);
p2.setCoefficient(1, 4.0);
p2.setCoefficient(2, 2.0);

polynomial n    = p1.addition(p2);
String po       = n.print();

polynomial t    = p1.multiply(p2);
String tr       = t.print();

polynomial di   = p2.differentiate();
String dir      = di.print();

double ev       = p2.evaluate(5.0); 
double inte     = p1.integration(3.0, 7.0); 



System.out.println("p1(x) = " + r );
System.out.println("p1(x) + p2(x) = " + po);
System.out.println("p1(x) * p2(x) = " + tr);
System.out.println("p2'(x) = " + dir);
System.out.println("p1(x) integration over [3.0, 7.0] = " + inte);
System.out.println("p2(5.0) = " + ev);



}
}
4

3 回答 3

2

如果我是你,我会拆分方法:

public Polynomial integrate()
{
    Polynomial integral = new Polynomial(this.degree + 1);
    for (int i = 1; i <= this.degree+1; i++)
    {
            integral.coefficients[i] = (this.coefficients[i - 1] / i);
    }
    return integral;
}

// a method that find a Polynomial integral over the interval a to b
public double integration(double a, double b)
{
    Polynomial integral = integrate();
    return (integral.evaluate(b) - integral.evaluate(a));
}

好的,现在为什么它没有按您的预期工作:

public double integration(double a , double b) {
     polynomial integral= new polynomial (degree+1);
     integral.degree= degree+1;
     for (int i=0 ; i<= degree+1 ; i++){
         if (i==0) {
             integral.coefficients[i]= 0;
         }
         else {
         integral.coefficients[i]= (coefficients[i-1]/i); 

         }
         }
     return (evaluate(b)- evaluate(a));
 }

你用当前实例“this”弄乱了你的“整体”对象,首先清理你的代码:

public double integration(double a , double b) {
     polynomial integral= new polynomial (this.degree+1);
     integral.degree= this.degree+1;
     for (int i=0 ; i<= this.degree+1 ; i++){
         if (i==0) {
             integral.coefficients[i]= 0;
         }
         else {
            integral.coefficients[i]= (this.coefficients[i-1]/i); 

         }
         }
     return (this.evaluate(b)- this.evaluate(a));
 }

在这里,您可以看到您评估的是实例对象而不是“整体”对象。这就是为什么它搞砸了结果。

于 2012-02-10T08:55:18.457 回答
0

你几乎猜对了。唯一的问题是您应该调用:

return (integral.evaluate(b) - integral.evaluate(a));

代替:

return (evaluate(b)- evaluate(a));

否则代码似乎没问题。

于 2012-02-10T08:37:40.680 回答
0

添加到鲍里斯的答案,你可以简化这样的integrate方法:

public double integration(double a, double b) {
    polynomial integral = new polynomial(degree + 1);
    for (int i = 1; i <= degree + 1; i++) {
        integral.coefficients[i] = coefficients[i - 1] / i;
    }
    return integral.evaluate(b) - integral.evaluate(a);
}
于 2012-02-10T08:41:59.227 回答