/* fourd_int.c integrate a function over a four dimensional cube */ /* this is numerical quadrature */ /* basically compute a 5D volume under 4D function */ /* compare methods for accuracy */ /* first trapezoid rule, varying step size, int f(x,y,z,t) dx dx dz dt*/ /* then using Gauss-Legendre coordinates and weights */ /* remember np points or nodes means np*np*np function evaluations */ /* or (np-1)^4 four D elements */ #include #include #undef abs #define abs(x) ((x)<0.0?(-(x)):(x)) static void gaulegf(double x1, double x2, double x[], double w[], int n); static double f1(double x, double y, double z, double t) { return x+2.0*y+3.0*z+4.0*t+5.0; /* linear, first order */ } static double f2(double x, double y, double z, double t) { return x*x+2.0*y*y+3.0*z*z+4.0*t*t+5.0*x*y+6.0*x*z+7.0*y*z+ 8.0*x*t+9.0*y*t+10.0*z*t+ 11.0*x+12.0*y+23.0*z+14.0*t+15.0; /* quadratic, second order */ } static double f3(double x, double y, double z, double t) { return x*x*x+2.0*y*y*y+3.0*z*z*z+4.0*t*t*t+5.0*x*x*y+ 6.0*x*x*z+7.0*x*y*y+8.0*x*y*z+9.0*x*z*z+ 10.0*y*y*z+11.0*y*z*z+12.0*x*x*t+13.0*x*t*t+14.0*x*y*t+ 15.0*y*y*t+16.0*y*t*t+17.0*y*z*t+18.0*z*z*t+19.0*z*t*t+ 20.0*x*z*t+21.0*x*x+22.0*y*y+23.0*z*z+24.0*x*y+25.0*x*z+ 26.0*y*z+27.0*x*t+28.0*y*t+29.0*z*t+ 30.0*x+31.0*y+32.0*z+33.0*t+34.0; /* cubic, third order */ } static double fe(double x, double y, double z, double t) { return exp(x)*exp(y)*exp(z)*exp(t); } int main(int argc, char *argv[]) { /* simple trapezoidal integration from x=1 to x=3*/ double volume; double x, y, z, t, hx, hy, hz, ht; double xmin = 1.0; double xmax = 3.0; double ymin = 0.5; double ymax = 2.0; double zmin = 0.25; double zmax = 2.25; double tmin = 0.5; double tmax = 2.5; double f1exact = 231.0; double f2exact = 2684.0; double f3exact = 17976.25; double feexact = 8615.146615676; double xx[200], wx[200]; /* for Gauss-Legendre */ double yy[200], wy[200]; /* for Gauss-Legendre */ double zz[200], wz[200]; /* for Gauss-Legendre */ double tt[200], wt[200]; /* for Gauss-Legendre */ int i, j, k, m, np; printf("fourd_int.c integrate to find 5D volume under 4D surface \n"); printf("Numerical quadrature, x=%g to x=%g, y=%g to y=%g, \n z=%g to z=%g, t=%g to t=%g \n", xmin, xmax, ymin, ymax, zmin, zmax, tmin, tmax); printf("\n"); printf("Using trapezoidal method \n"); np = 4; /* f1 */ hx = (xmax-xmin)/(double)(np-1); hy = (ymax-ymin)/(double)(np-1); hz = (zmax-zmin)/(double)(np-1); ht = (tmax-tmin)/(double)(np-1); printf("f1(x,y,z,t)=x+2*y+3*z+4*t+5 \n"); printf("np=%d, hx=%g, hy=%g, hz=%g, ht=%g \n", np, hx, hy, hz, ht); volume = 0.0; for(i=0; i