I’ve been trying to implement trapezoid rule for double integral. I’ve tried many approaches, but I can’t get it to work right.
static double f(double x) {
return Math.exp(- x * x / 2);
}
// trapezoid rule
static double trapezoid(double a, double b, int N) {
double h = (b - a) / N;
double sum = 0.5 * h * (f(a) + f(b));
for (int k = 1; k < N; k++)
sum = sum + h * f(a + h*k);
return sum;
}
I understand the method for a single variable integral, but I don’t know how to do it for a 2D integral, say: x + (y*y).
Could someone please explain it briefly?
If you’re intent on using the trapezoid rule then you would do it like so:
Hope this helps. Feel free to ask any questions you may have about my code (warning: The code is untested).