Search code examples
javaclassnumerical-integration

integration by java


I designing a polynomial class for one of my com sci courses , I have a problem of getting the integration method right can some one help me with that

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



}
}

Solution

  • If I were you, I would split the methods :

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

    Ok now why it didn't work as you expected :

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

    you messed up your "integral" object with the current instance "this", clean your code first :

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

    Here you can see that you evaluate on your instance object instead of "integral" object. That's why it messed up the result.