/* aquad.c adaptive quadrature numerical integration */ /* for a desired accuracy on irregular functions. */ /* define the function to be integrated as: */ /* double f(double x) */ /* { */ /* // compute value */ /* return value; */ /* } */ /* */ /* integrate from xmin to xmax */ /* approximate desired absolute error in all intervals, eps */ /* accuracy absolutely not guaranteed */ #undef abs #define abs(x) ((x)<0.0?(-(x)):(x)) double aquad(double f(double x), double xmin, double xmax, double eps) { double area, temp, part, h; double err; int nmax = 2000; double sbin[2000]; double ebin[2000]; double abin[2000]; int fbin[2000]; int i, j, k, n, nn, done; int kmax = 20; n=32; /* initial number of bins */ h = (xmax-xmin)/(double)n; for(i=0; i=kmax) break; /* quit if more than kmax subdivisions */ area = 0.0; for(i=0; i=nmax) /* out of space, quit */ { done = 1; for(j=i+1; j