通过java集成

问题描述 投票:0回答:3

我为我的一门 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);



}
}
java class numerical-integration
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));
 }

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


0
投票

你几乎答对了。唯一的问题是你应该打电话:

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

而不是:

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

否则代码看起来没问题。


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);
}
© www.soinside.com 2019 - 2024. All rights reserved.