Hello everyone
I was trying to implement a program in which I can calculate a double integral as a function of x and y with the trapezium rule (that's the one I understood the best). However, in addition to not getting the result I want the program has a very large calculation time for very large n ranges.
I thought first of calculating the integral of the function with respect to x, and then in the body of the program it calls it in relation to the parameters presented and calculate its value in function of y, but it did not work very well and I can not think of a way to do it.
Could someone help me to implement the program correctly and more efficiently?
here is the code as far as i did:
Code:
#include <stdio.h>
#include <math.h>
double f(double x, double y, double ay,double by, double ny);
int main()
{
int i;
int nx = 0;
double ax, bx;
double sumx = 0;
double soma=0;
double hx,hy;
double x,y;
double ay,by;
int ny;
printf("inferior limit ax = ");
scanf("%lf", &ax);
printf("upper limit bx (b>a) = ");
scanf("%lf", &bx);
printf("interval nx (n>1) n = ");
scanf("%d", &nx);
hx = (bx - ax) / (nx - 1);
x = ax;
printf("inferior limit ay = ");
scanf("%lf", &ay);
printf("upper limit by (b>a) = ");
scanf("%lf", &by);
printf("interval ny (n>1) n = ");
scanf("%d", &ny);
for (i = 1; i <= nx - 1; i++)
{
sumx = sumx + f(x,y,ay,by,ny) + f(x + hx,y,ay,by,ny);
x = x + hx;
}
sumx = sumx * (hx / 2);
printf("O resultado da soma e': %lf\n", sumx);
return 0;
} // end main()
//
//
//
double f(double x, double y, double ay,double by, double ny)
{
double func;
int j=0;
double hy;
double sumy=0;
func= sin(x+y) / ((x*x) + y);
hy = (by - ay) / (ny -1);
y= ay;
for (j =1; j <= ny - 1; j++)
{
sumy = sumy + sin(x + (y + hy)/ (x*x) + y+ hy);
y = y +hy;
}
return sumy = sumy * (hy / 2);
} // end f()
the limits of integration are
0.1 <y< 0.9
0.2 <x<0.5