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!