-2

I have to implement a numerical computation of double integrals in Java. Concentrating on the integrate() function, here's what I have to far:

public static double Integrate(Integravel integrando, boolean pontoMedio) {
    double sum = 0.0;
    double f2;

    double deltaX = (integrando.getb() - integrando.geta())
            / (double) integrando.getN();

    double deltaY = (integrando.getd() - integrando.getc())
            / (double) integrando.getN();

    double deltaArea = deltaX * deltaY;     

    double x, xi, y, yi;
    if (pontoMedio) {
        xi = integrando.geta() + deltaX/2;
        yi = integrando.getc() + deltaY/2;
    }
    else {
        xi = integrando.geta() + deltaX;
        yi = integrando.getc() + deltaY;
    }

    for (int j=0; j<integrando.getN(); j++) {
        for (int i = 0; i < integrando.getN(); i++) {
            if (j==0) {
                x = xi;

            } else {
                x = xi + deltaX;

            }

            if (i==0) {
                y = yi;
            } else {
                y = yi + deltaY;
            }

            f2 = integrando.funcao(x, y);
            sum += f2*deltaArea;                
        }
    }
    return sum;
}

It does work on certain cases, but whenever I make N too big (which should make the integral computation more precise, since it is supposed to go to infinite), it returns a wrong value for the integral estimation. Am I doing something wrong here? If more code from the project is needed, let me know. Any help would be incredibly appreciated. Thanks!

yves
  • 29
  • 7
  • How big is "too big"? – Ryan J May 12 '14 at 23:03
  • Can you give an example of input that you think gives you incorrect output, what output you expect, and what the output really is? – Dawood ibn Kareem May 12 '14 at 23:05
  • If n = 2, it returns the correct estimation. If it goes higher than 100, 1000 or 4000 it goes wrong. Not sure if it's something related to the method I'm using to calculate the sum or to some error in variables assignments. – yves May 12 '14 at 23:05
  • Possibly because you're simply typecasting N as a (double)? Is N actually a double? – explodingcreeperssss May 12 '14 at 23:06
  • 2
    Step through the code, and see when the variables in question begin to take the wrong values. [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Jonathon Reinhart May 12 '14 at 23:10
  • @explodingcreeperssss no matter the actual type of N, it will be automatically widened to a double because all computations are with doubles; java automatically promotes all other types to double when one of the operands of a numerical operation is double. – Bohemian May 12 '14 at 23:10
  • @yvesbastos For what function? Give me something I can try out! Tell me what input to put in, what function you're integrating, what output to expect, and what "wrong" results you're getting. Otherwise, how can I possibly see what your issue is? – Dawood ibn Kareem May 12 '14 at 23:12
  • Actually, I just computed with N=3 and it's returning the wrong value. I bet the j/i loop is wrong... – yves May 12 '14 at 23:13
  • @explodingcreeperssss He's only casting `N` to double for the purposes of the division, to make sure he doesn't get an "integer division" problem. Presumably `a` and `b` are double, so this shouldn't be necessary, but we can't tell. – Dawood ibn Kareem May 12 '14 at 23:15
  • Don't forget every time you go through the j loop, it's going to reset the value of j, therefore resetting x. Is this a problem? – explodingcreeperssss May 12 '14 at 23:17
  • Why would it be a problem? – Dawood ibn Kareem May 12 '14 at 23:18
  • I don't think that's a problem, given that the x and y values passed to the function are supposed to change on each iteration... – yves May 12 '14 at 23:59

1 Answers1

1

When i is not 0, the y value should be y + deltaY, not yi + deltaY. A similar change is needed for x.

With the current code, the first and second iterations of each loop will get the correct x and y. The third and subsequent iterations will go on using the x or y value for the second iteration.

There is another issue you are approaching, and may not be aware of. Up to a certain point, increasing N will improve accuracy, but using very large N will reduce accuracy. The error is the sum of two terms, floating point rounding error + error due to the difference between the actual function and the approximation due to the finiteness of N. The first term increases with N because you do more calculations. The second reduces as N gets closer to infinity.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75