home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d07xx / d0713.lha / ICalc / Scripts / trapzd.ic < prev    next >
Text File  |  1992-08-19  |  1KB  |  40 lines

  1. #
  2. # trapzd
  3. #
  4. # Routine from "Numerical Recipes in C" by Press et. al.
  5. # It is used by qtrap.ic, qsimp.ic and qromb.ic.
  6. #
  7. # This routine computes the n'th stage of refinement of an extended
  8. # trapezoidal rule. f is a function of global variable x (e.g. x*x, sin(x))
  9. # to be integrated between the limits a and b. When called with n=1, the
  10. # routine returns the crudest estimate of the integral. Subsequent calls
  11. # with n=2,3,... (in that sequential order) will improve the accuracy by
  12. # adding 2^(n-2) additional interior points.
  13. trapzd_s = trapzd_it = 0    # create required globals
  14. #
  15. func trapzd(~f,a,b,n) = {
  16.     local tmp,tnm,sum,del,j
  17.  
  18.     if (n == 1) {
  19.         trapzd_it = 1
  20.         x = a        # set x to value a
  21.         tmp = f        # evaluate f at a
  22.         x = b
  23.         tmp += f 
  24.         return trapzd_s = 0.5*(b-a)*tmp
  25.     } else {
  26.         tnm = trapzd_it
  27.         del = (b-a)/tnm
  28.         x = a + 0.5*del
  29.         sum = 0
  30.         for (j = 1; j <= trapzd_it; j += 1) {
  31.             sum += f    # i.e. sum += f evaluated at x
  32.             x += del
  33.         }
  34.         trapzd_it *= 2
  35.         trapzd_s = 0.5*(trapzd_s + (b-a)*sum/tnm)
  36.         return trapzd_s
  37.     }
  38. }
  39.