home *** CD-ROM | disk | FTP | other *** search
- #
- # trapzd
- #
- # Routine from "Numerical Recipes in C" by Press et. al.
- # It is used by qtrap.ic, qsimp.ic and qromb.ic.
- #
- # This routine computes the n'th stage of refinement of an extended
- # trapezoidal rule. f is a function of global variable x (e.g. x*x, sin(x))
- # to be integrated between the limits a and b. When called with n=1, the
- # routine returns the crudest estimate of the integral. Subsequent calls
- # with n=2,3,... (in that sequential order) will improve the accuracy by
- # adding 2^(n-2) additional interior points.
- #
- trapzd_s = trapzd_it = 0 # create required globals
- #
- func trapzd(~f,a,b,n) = {
- local tmp,tnm,sum,del,j
-
- if (n == 1) {
- trapzd_it = 1
- x = a # set x to value a
- tmp = f # evaluate f at a
- x = b
- tmp += f
- return trapzd_s = 0.5*(b-a)*tmp
- } else {
- tnm = trapzd_it
- del = (b-a)/tnm
- x = a + 0.5*del
- sum = 0
- for (j = 1; j <= trapzd_it; j += 1) {
- sum += f # i.e. sum += f evaluated at x
- x += del
- }
- trapzd_it *= 2
- trapzd_s = 0.5*(trapzd_s + (b-a)*sum/tnm)
- return trapzd_s
- }
- }
-