qage.f 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. *DECK QAGE
  2. SUBROUTINE QAGE (F, A, B, EPSABS, EPSREL, KEY, LIMIT, RESULT,
  3. + ABSERR, NEVAL, IER, ALIST, BLIST, RLIST, ELIST, IORD, LAST)
  4. C***BEGIN PROLOGUE QAGE
  5. C***PURPOSE The routine calculates an approximation result to a given
  6. C definite integral I = Integral of F over (A,B),
  7. C hopefully satisfying following claim for accuracy
  8. C ABS(I-RESLT).LE.MAX(EPSABS,EPSREL*ABS(I)).
  9. C***LIBRARY SLATEC (QUADPACK)
  10. C***CATEGORY H2A1A1
  11. C***TYPE SINGLE PRECISION (QAGE-S, DQAGE-D)
  12. C***KEYWORDS AUTOMATIC INTEGRATOR, GAUSS-KRONROD RULES,
  13. C GENERAL-PURPOSE, GLOBALLY ADAPTIVE, INTEGRAND EXAMINATOR,
  14. C QUADPACK, QUADRATURE
  15. C***AUTHOR Piessens, Robert
  16. C Applied Mathematics and Programming Division
  17. C K. U. Leuven
  18. C de Doncker, Elise
  19. C Applied Mathematics and Programming Division
  20. C K. U. Leuven
  21. C***DESCRIPTION
  22. C
  23. C Computation of a definite integral
  24. C Standard fortran subroutine
  25. C Real version
  26. C
  27. C PARAMETERS
  28. C ON ENTRY
  29. C F - Real
  30. C Function subprogram defining the integrand
  31. C function F(X). The actual name for F needs to be
  32. C declared E X T E R N A L in the driver program.
  33. C
  34. C A - Real
  35. C Lower limit of integration
  36. C
  37. C B - Real
  38. C Upper limit of integration
  39. C
  40. C EPSABS - Real
  41. C Absolute accuracy requested
  42. C EPSREL - Real
  43. C Relative accuracy requested
  44. C If EPSABS.LE.0
  45. C and EPSREL.LT.MAX(50*REL.MACH.ACC.,0.5D-28),
  46. C the routine will end with IER = 6.
  47. C
  48. C KEY - Integer
  49. C Key for choice of local integration rule
  50. C A Gauss-Kronrod pair is used with
  51. C 7 - 15 points if KEY.LT.2,
  52. C 10 - 21 points if KEY = 2,
  53. C 15 - 31 points if KEY = 3,
  54. C 20 - 41 points if KEY = 4,
  55. C 25 - 51 points if KEY = 5,
  56. C 30 - 61 points if KEY.GT.5.
  57. C
  58. C LIMIT - Integer
  59. C Gives an upper bound on the number of subintervals
  60. C in the partition of (A,B), LIMIT.GE.1.
  61. C
  62. C ON RETURN
  63. C RESULT - Real
  64. C Approximation to the integral
  65. C
  66. C ABSERR - Real
  67. C Estimate of the modulus of the absolute error,
  68. C which should equal or exceed ABS(I-RESULT)
  69. C
  70. C NEVAL - Integer
  71. C Number of integrand evaluations
  72. C
  73. C IER - Integer
  74. C IER = 0 Normal and reliable termination of the
  75. C routine. It is assumed that the requested
  76. C accuracy has been achieved.
  77. C IER.GT.0 Abnormal termination of the routine
  78. C The estimates for result and error are
  79. C less reliable. It is assumed that the
  80. C requested accuracy has not been achieved.
  81. C ERROR MESSAGES
  82. C IER = 1 Maximum number of subdivisions allowed
  83. C has been achieved. One can allow more
  84. C subdivisions by increasing the value
  85. C of LIMIT.
  86. C However, if this yields no improvement it
  87. C is rather advised to analyze the integrand
  88. C in order to determine the integration
  89. C difficulties. If the position of a local
  90. C difficulty can be determined(e.g.
  91. C SINGULARITY, DISCONTINUITY within the
  92. C interval) one will probably gain from
  93. C splitting up the interval at this point
  94. C and calling the integrator on the
  95. C subranges. If possible, an appropriate
  96. C special-purpose integrator should be used
  97. C which is designed for handling the type of
  98. C difficulty involved.
  99. C = 2 The occurrence of roundoff error is
  100. C detected, which prevents the requested
  101. C tolerance from being achieved.
  102. C = 3 Extremely bad integrand behaviour occurs
  103. C at some points of the integration
  104. C interval.
  105. C = 6 The input is invalid, because
  106. C (EPSABS.LE.0 and
  107. C EPSREL.LT.MAX(50*REL.MACH.ACC.,0.5D-28),
  108. C RESULT, ABSERR, NEVAL, LAST, RLIST(1) ,
  109. C ELIST(1) and IORD(1) are set to zero.
  110. C ALIST(1) and BLIST(1) are set to A and B
  111. C respectively.
  112. C
  113. C ALIST - Real
  114. C Vector of dimension at least LIMIT, the first
  115. C LAST elements of which are the left
  116. C end points of the subintervals in the partition
  117. C of the given integration range (A,B)
  118. C
  119. C BLIST - Real
  120. C Vector of dimension at least LIMIT, the first
  121. C LAST elements of which are the right
  122. C end points of the subintervals in the partition
  123. C of the given integration range (A,B)
  124. C
  125. C RLIST - Real
  126. C Vector of dimension at least LIMIT, the first
  127. C LAST elements of which are the
  128. C integral approximations on the subintervals
  129. C
  130. C ELIST - Real
  131. C Vector of dimension at least LIMIT, the first
  132. C LAST elements of which are the moduli of the
  133. C absolute error estimates on the subintervals
  134. C
  135. C IORD - Integer
  136. C Vector of dimension at least LIMIT, the first K
  137. C elements of which are pointers to the
  138. C error estimates over the subintervals,
  139. C such that ELIST(IORD(1)), ...,
  140. C ELIST(IORD(K)) form a decreasing sequence,
  141. C with K = LAST if LAST.LE.(LIMIT/2+2), and
  142. C K = LIMIT+1-LAST otherwise
  143. C
  144. C LAST - Integer
  145. C Number of subintervals actually produced in the
  146. C subdivision process
  147. C
  148. C***REFERENCES (NONE)
  149. C***ROUTINES CALLED QK15, QK21, QK31, QK41, QK51, QK61, QPSRT, R1MACH
  150. C***REVISION HISTORY (YYMMDD)
  151. C 800101 DATE WRITTEN
  152. C 890531 Changed all specific intrinsics to generic. (WRB)
  153. C 890831 Modified array declarations. (WRB)
  154. C 890831 REVISION DATE from Version 3.2
  155. C 891214 Prologue converted to Version 4.0 format. (BAB)
  156. C***END PROLOGUE QAGE
  157. C
  158. REAL A,ABSERR,ALIST,AREA,AREA1,AREA12,AREA2,A1,A2,B,BLIST,
  159. 1 B1,B2,DEFABS,DEFAB1,DEFAB2,R1MACH,ELIST,EPMACH,
  160. 2 EPSABS,EPSREL,ERRBND,ERRMAX,ERROR1,ERROR2,ERRO12,ERRSUM,F,
  161. 3 RESABS,RESULT,RLIST,UFLOW
  162. INTEGER IER,IORD,IROFF1,IROFF2,K,KEY,KEYF,LAST,
  163. 1 LIMIT,MAXERR,NEVAL,NRMAX
  164. C
  165. DIMENSION ALIST(*),BLIST(*),ELIST(*),IORD(*),
  166. 1 RLIST(*)
  167. C
  168. EXTERNAL F
  169. C
  170. C LIST OF MAJOR VARIABLES
  171. C -----------------------
  172. C
  173. C ALIST - LIST OF LEFT END POINTS OF ALL SUBINTERVALS
  174. C CONSIDERED UP TO NOW
  175. C BLIST - LIST OF RIGHT END POINTS OF ALL SUBINTERVALS
  176. C CONSIDERED UP TO NOW
  177. C RLIST(I) - APPROXIMATION TO THE INTEGRAL OVER
  178. C (ALIST(I),BLIST(I))
  179. C ELIST(I) - ERROR ESTIMATE APPLYING TO RLIST(I)
  180. C MAXERR - POINTER TO THE INTERVAL WITH LARGEST
  181. C ERROR ESTIMATE
  182. C ERRMAX - ELIST(MAXERR)
  183. C AREA - SUM OF THE INTEGRALS OVER THE SUBINTERVALS
  184. C ERRSUM - SUM OF THE ERRORS OVER THE SUBINTERVALS
  185. C ERRBND - REQUESTED ACCURACY MAX(EPSABS,EPSREL*
  186. C ABS(RESULT))
  187. C *****1 - VARIABLE FOR THE LEFT SUBINTERVAL
  188. C *****2 - VARIABLE FOR THE RIGHT SUBINTERVAL
  189. C LAST - INDEX FOR SUBDIVISION
  190. C
  191. C
  192. C MACHINE DEPENDENT CONSTANTS
  193. C ---------------------------
  194. C
  195. C EPMACH IS THE LARGEST RELATIVE SPACING.
  196. C UFLOW IS THE SMALLEST POSITIVE MAGNITUDE.
  197. C
  198. C***FIRST EXECUTABLE STATEMENT QAGE
  199. EPMACH = R1MACH(4)
  200. UFLOW = R1MACH(1)
  201. C
  202. C TEST ON VALIDITY OF PARAMETERS
  203. C ------------------------------
  204. C
  205. IER = 0
  206. NEVAL = 0
  207. LAST = 0
  208. RESULT = 0.0E+00
  209. ABSERR = 0.0E+00
  210. ALIST(1) = A
  211. BLIST(1) = B
  212. RLIST(1) = 0.0E+00
  213. ELIST(1) = 0.0E+00
  214. IORD(1) = 0
  215. IF(EPSABS.LE.0.0E+00.AND.
  216. 1 EPSREL.LT.MAX(0.5E+02*EPMACH,0.5E-14)) IER = 6
  217. IF(IER.EQ.6) GO TO 999
  218. C
  219. C FIRST APPROXIMATION TO THE INTEGRAL
  220. C -----------------------------------
  221. C
  222. KEYF = KEY
  223. IF(KEY.LE.0) KEYF = 1
  224. IF(KEY.GE.7) KEYF = 6
  225. NEVAL = 0
  226. IF(KEYF.EQ.1) CALL QK15(F,A,B,RESULT,ABSERR,DEFABS,RESABS)
  227. IF(KEYF.EQ.2) CALL QK21(F,A,B,RESULT,ABSERR,DEFABS,RESABS)
  228. IF(KEYF.EQ.3) CALL QK31(F,A,B,RESULT,ABSERR,DEFABS,RESABS)
  229. IF(KEYF.EQ.4) CALL QK41(F,A,B,RESULT,ABSERR,DEFABS,RESABS)
  230. IF(KEYF.EQ.5) CALL QK51(F,A,B,RESULT,ABSERR,DEFABS,RESABS)
  231. IF(KEYF.EQ.6) CALL QK61(F,A,B,RESULT,ABSERR,DEFABS,RESABS)
  232. LAST = 1
  233. RLIST(1) = RESULT
  234. ELIST(1) = ABSERR
  235. IORD(1) = 1
  236. C
  237. C TEST ON ACCURACY.
  238. C
  239. ERRBND = MAX(EPSABS,EPSREL*ABS(RESULT))
  240. IF(ABSERR.LE.0.5E+02*EPMACH*DEFABS.AND.ABSERR.GT.
  241. 1 ERRBND) IER = 2
  242. IF(LIMIT.EQ.1) IER = 1
  243. IF(IER.NE.0.OR.(ABSERR.LE.ERRBND.AND.ABSERR.NE.RESABS)
  244. 1 .OR.ABSERR.EQ.0.0E+00) GO TO 60
  245. C
  246. C INITIALIZATION
  247. C --------------
  248. C
  249. C
  250. ERRMAX = ABSERR
  251. MAXERR = 1
  252. AREA = RESULT
  253. ERRSUM = ABSERR
  254. NRMAX = 1
  255. IROFF1 = 0
  256. IROFF2 = 0
  257. C
  258. C MAIN DO-LOOP
  259. C ------------
  260. C
  261. DO 30 LAST = 2,LIMIT
  262. C
  263. C BISECT THE SUBINTERVAL WITH THE LARGEST ERROR ESTIMATE.
  264. C
  265. A1 = ALIST(MAXERR)
  266. B1 = 0.5E+00*(ALIST(MAXERR)+BLIST(MAXERR))
  267. A2 = B1
  268. B2 = BLIST(MAXERR)
  269. IF(KEYF.EQ.1) CALL QK15(F,A1,B1,AREA1,ERROR1,RESABS,DEFAB1)
  270. IF(KEYF.EQ.2) CALL QK21(F,A1,B1,AREA1,ERROR1,RESABS,DEFAB1)
  271. IF(KEYF.EQ.3) CALL QK31(F,A1,B1,AREA1,ERROR1,RESABS,DEFAB1)
  272. IF(KEYF.EQ.4) CALL QK41(F,A1,B1,AREA1,ERROR1,RESABS,DEFAB1)
  273. IF(KEYF.EQ.5) CALL QK51(F,A1,B1,AREA1,ERROR1,RESABS,DEFAB1)
  274. IF(KEYF.EQ.6) CALL QK61(F,A1,B1,AREA1,ERROR1,RESABS,DEFAB1)
  275. IF(KEYF.EQ.1) CALL QK15(F,A2,B2,AREA2,ERROR2,RESABS,DEFAB2)
  276. IF(KEYF.EQ.2) CALL QK21(F,A2,B2,AREA2,ERROR2,RESABS,DEFAB2)
  277. IF(KEYF.EQ.3) CALL QK31(F,A2,B2,AREA2,ERROR2,RESABS,DEFAB2)
  278. IF(KEYF.EQ.4) CALL QK41(F,A2,B2,AREA2,ERROR2,RESABS,DEFAB2)
  279. IF(KEYF.EQ.5) CALL QK51(F,A2,B2,AREA2,ERROR2,RESABS,DEFAB2)
  280. IF(KEYF.EQ.6) CALL QK61(F,A2,B2,AREA2,ERROR2,RESABS,DEFAB2)
  281. C
  282. C IMPROVE PREVIOUS APPROXIMATIONS TO INTEGRAL
  283. C AND ERROR AND TEST FOR ACCURACY.
  284. C
  285. NEVAL = NEVAL+1
  286. AREA12 = AREA1+AREA2
  287. ERRO12 = ERROR1+ERROR2
  288. ERRSUM = ERRSUM+ERRO12-ERRMAX
  289. AREA = AREA+AREA12-RLIST(MAXERR)
  290. IF(DEFAB1.EQ.ERROR1.OR.DEFAB2.EQ.ERROR2) GO TO 5
  291. IF(ABS(RLIST(MAXERR)-AREA12).LE.0.1E-04*ABS(AREA12)
  292. 1 .AND.ERRO12.GE.0.99E+00*ERRMAX) IROFF1 = IROFF1+1
  293. IF(LAST.GT.10.AND.ERRO12.GT.ERRMAX) IROFF2 = IROFF2+1
  294. 5 RLIST(MAXERR) = AREA1
  295. RLIST(LAST) = AREA2
  296. ERRBND = MAX(EPSABS,EPSREL*ABS(AREA))
  297. IF(ERRSUM.LE.ERRBND) GO TO 8
  298. C
  299. C TEST FOR ROUNDOFF ERROR AND EVENTUALLY
  300. C SET ERROR FLAG.
  301. C
  302. IF(IROFF1.GE.6.OR.IROFF2.GE.20) IER = 2
  303. C
  304. C SET ERROR FLAG IN THE CASE THAT THE NUMBER OF
  305. C SUBINTERVALS EQUALS LIMIT.
  306. C
  307. IF(LAST.EQ.LIMIT) IER = 1
  308. C
  309. C SET ERROR FLAG IN THE CASE OF BAD INTEGRAND BEHAVIOUR
  310. C AT A POINT OF THE INTEGRATION RANGE.
  311. C
  312. IF(MAX(ABS(A1),ABS(B2)).LE.(0.1E+01+0.1E+03*
  313. 1 EPMACH)*(ABS(A2)+0.1E+04*UFLOW)) IER = 3
  314. C
  315. C APPEND THE NEWLY-CREATED INTERVALS TO THE LIST.
  316. C
  317. 8 IF(ERROR2.GT.ERROR1) GO TO 10
  318. ALIST(LAST) = A2
  319. BLIST(MAXERR) = B1
  320. BLIST(LAST) = B2
  321. ELIST(MAXERR) = ERROR1
  322. ELIST(LAST) = ERROR2
  323. GO TO 20
  324. 10 ALIST(MAXERR) = A2
  325. ALIST(LAST) = A1
  326. BLIST(LAST) = B1
  327. RLIST(MAXERR) = AREA2
  328. RLIST(LAST) = AREA1
  329. ELIST(MAXERR) = ERROR2
  330. ELIST(LAST) = ERROR1
  331. C
  332. C CALL SUBROUTINE QPSRT TO MAINTAIN THE DESCENDING ORDERING
  333. C IN THE LIST OF ERROR ESTIMATES AND SELECT THE
  334. C SUBINTERVAL WITH THE LARGEST ERROR ESTIMATE (TO BE
  335. C BISECTED NEXT).
  336. C
  337. 20 CALL QPSRT(LIMIT,LAST,MAXERR,ERRMAX,ELIST,IORD,NRMAX)
  338. C ***JUMP OUT OF DO-LOOP
  339. IF(IER.NE.0.OR.ERRSUM.LE.ERRBND) GO TO 40
  340. 30 CONTINUE
  341. C
  342. C COMPUTE FINAL RESULT.
  343. C ---------------------
  344. C
  345. 40 RESULT = 0.0E+00
  346. DO 50 K=1,LAST
  347. RESULT = RESULT+RLIST(K)
  348. 50 CONTINUE
  349. ABSERR = ERRSUM
  350. 60 IF(KEYF.NE.1) NEVAL = (10*KEYF+1)*(2*NEVAL+1)
  351. IF(KEYF.EQ.1) NEVAL = 30*NEVAL+15
  352. 999 RETURN
  353. END