dfzero.f 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. *DECK DFZERO
  2. SUBROUTINE DFZERO (F, B, C, R, RE, AE, IFLAG)
  3. C***BEGIN PROLOGUE DFZERO
  4. C***PURPOSE Search for a zero of a function F(X) in a given interval
  5. C (B,C). It is designed primarily for problems where F(B)
  6. C and F(C) have opposite signs.
  7. C***LIBRARY SLATEC
  8. C***CATEGORY F1B
  9. C***TYPE DOUBLE PRECISION (FZERO-S, DFZERO-D)
  10. C***KEYWORDS BISECTION, NONLINEAR, ROOTS, ZEROS
  11. C***AUTHOR Shampine, L. F., (SNLA)
  12. C Watts, H. A., (SNLA)
  13. C***DESCRIPTION
  14. C
  15. C DFZERO searches for a zero of a DOUBLE PRECISION function F(X)
  16. C between the given DOUBLE PRECISION values B and C until the width
  17. C of the interval (B,C) has collapsed to within a tolerance
  18. C specified by the stopping criterion,
  19. C ABS(B-C) .LE. 2.*(RW*ABS(B)+AE).
  20. C The method used is an efficient combination of bisection and the
  21. C secant rule and is due to T. J. Dekker.
  22. C
  23. C Description Of Arguments
  24. C
  25. C F :EXT - Name of the DOUBLE PRECISION external function. This
  26. C name must be in an EXTERNAL statement in the calling
  27. C program. F must be a function of one DOUBLE
  28. C PRECISION argument.
  29. C
  30. C B :INOUT - One end of the DOUBLE PRECISION interval (B,C). The
  31. C value returned for B usually is the better
  32. C approximation to a zero of F.
  33. C
  34. C C :INOUT - The other end of the DOUBLE PRECISION interval (B,C)
  35. C
  36. C R :IN - A (better) DOUBLE PRECISION guess of a zero of F
  37. C which could help in speeding up convergence. If F(B)
  38. C and F(R) have opposite signs, a root will be found in
  39. C the interval (B,R); if not, but F(R) and F(C) have
  40. C opposite signs, a root will be found in the interval
  41. C (R,C); otherwise, the interval (B,C) will be
  42. C searched for a possible root. When no better guess
  43. C is known, it is recommended that R be set to B or C,
  44. C since if R is not interior to the interval (B,C), it
  45. C will be ignored.
  46. C
  47. C RE :IN - Relative error used for RW in the stopping criterion.
  48. C If the requested RE is less than machine precision,
  49. C then RW is set to approximately machine precision.
  50. C
  51. C AE :IN - Absolute error used in the stopping criterion. If
  52. C the given interval (B,C) contains the origin, then a
  53. C nonzero value should be chosen for AE.
  54. C
  55. C IFLAG :OUT - A status code. User must check IFLAG after each
  56. C call. Control returns to the user from DFZERO in all
  57. C cases.
  58. C
  59. C 1 B is within the requested tolerance of a zero.
  60. C The interval (B,C) collapsed to the requested
  61. C tolerance, the function changes sign in (B,C), and
  62. C F(X) decreased in magnitude as (B,C) collapsed.
  63. C
  64. C 2 F(B) = 0. However, the interval (B,C) may not have
  65. C collapsed to the requested tolerance.
  66. C
  67. C 3 B may be near a singular point of F(X).
  68. C The interval (B,C) collapsed to the requested tol-
  69. C erance and the function changes sign in (B,C), but
  70. C F(X) increased in magnitude as (B,C) collapsed, i.e.
  71. C ABS(F(B out)) .GT. MAX(ABS(F(B in)),ABS(F(C in)))
  72. C
  73. C 4 No change in sign of F(X) was found although the
  74. C interval (B,C) collapsed to the requested tolerance.
  75. C The user must examine this case and decide whether
  76. C B is near a local minimum of F(X), or B is near a
  77. C zero of even multiplicity, or neither of these.
  78. C
  79. C 5 Too many (.GT. 500) function evaluations used.
  80. C
  81. C***REFERENCES L. F. Shampine and H. A. Watts, FZERO, a root-solving
  82. C code, Report SC-TM-70-631, Sandia Laboratories,
  83. C September 1970.
  84. C T. J. Dekker, Finding a zero by means of successive
  85. C linear interpolation, Constructive Aspects of the
  86. C Fundamental Theorem of Algebra, edited by B. Dejon
  87. C and P. Henrici, Wiley-Interscience, 1969.
  88. C***ROUTINES CALLED D1MACH
  89. C***REVISION HISTORY (YYMMDD)
  90. C 700901 DATE WRITTEN
  91. C 890531 Changed all specific intrinsics to generic. (WRB)
  92. C 890531 REVISION DATE from Version 3.2
  93. C 891214 Prologue converted to Version 4.0 format. (BAB)
  94. C 920501 Reformatted the REFERENCES section. (WRB)
  95. C***END PROLOGUE DFZERO
  96. DOUBLE PRECISION A,ACBS,ACMB,AE,AW,B,C,CMB,D1MACH,ER,
  97. + F,FA,FB,FC,FX,FZ,P,Q,R,RE,RW,T,TOL,Z
  98. INTEGER IC,IFLAG,KOUNT
  99. C
  100. C***FIRST EXECUTABLE STATEMENT DFZERO
  101. C
  102. C ER is two times the computer unit roundoff value which is defined
  103. C here by the function D1MACH.
  104. C
  105. ER = 2.0D0 * D1MACH(4)
  106. C
  107. C Initialize.
  108. C
  109. Z = R
  110. IF (R .LE. MIN(B,C) .OR. R .GE. MAX(B,C)) Z = C
  111. RW = MAX(RE,ER)
  112. AW = MAX(AE,0.D0)
  113. IC = 0
  114. T = Z
  115. FZ = F(T)
  116. FC = FZ
  117. T = B
  118. FB = F(T)
  119. KOUNT = 2
  120. IF (SIGN(1.0D0,FZ) .EQ. SIGN(1.0D0,FB)) GO TO 1
  121. C = Z
  122. GO TO 2
  123. 1 IF (Z .EQ. C) GO TO 2
  124. T = C
  125. FC = F(T)
  126. KOUNT = 3
  127. IF (SIGN(1.0D0,FZ) .EQ. SIGN(1.0D0,FC)) GO TO 2
  128. B = Z
  129. FB = FZ
  130. 2 A = C
  131. FA = FC
  132. ACBS = ABS(B-C)
  133. FX = MAX(ABS(FB),ABS(FC))
  134. C
  135. 3 IF (ABS(FC) .GE. ABS(FB)) GO TO 4
  136. C
  137. C Perform interchange.
  138. C
  139. A = B
  140. FA = FB
  141. B = C
  142. FB = FC
  143. C = A
  144. FC = FA
  145. C
  146. 4 CMB = 0.5D0*(C-B)
  147. ACMB = ABS(CMB)
  148. TOL = RW*ABS(B) + AW
  149. C
  150. C Test stopping criterion and function count.
  151. C
  152. IF (ACMB .LE. TOL) GO TO 10
  153. IF (FB .EQ. 0.D0) GO TO 11
  154. IF (KOUNT .GE. 500) GO TO 14
  155. C
  156. C Calculate new iterate implicitly as B+P/Q, where we arrange
  157. C P .GE. 0. The implicit form is used to prevent overflow.
  158. C
  159. P = (B-A)*FB
  160. Q = FA - FB
  161. IF (P .GE. 0.D0) GO TO 5
  162. P = -P
  163. Q = -Q
  164. C
  165. C Update A and check for satisfactory reduction in the size of the
  166. C bracketing interval. If not, perform bisection.
  167. C
  168. 5 A = B
  169. FA = FB
  170. IC = IC + 1
  171. IF (IC .LT. 4) GO TO 6
  172. IF (8.0D0*ACMB .GE. ACBS) GO TO 8
  173. IC = 0
  174. ACBS = ACMB
  175. C
  176. C Test for too small a change.
  177. C
  178. 6 IF (P .GT. ABS(Q)*TOL) GO TO 7
  179. C
  180. C Increment by TOLerance.
  181. C
  182. B = B + SIGN(TOL,CMB)
  183. GO TO 9
  184. C
  185. C Root ought to be between B and (C+B)/2.
  186. C
  187. 7 IF (P .GE. CMB*Q) GO TO 8
  188. C
  189. C Use secant rule.
  190. C
  191. B = B + P/Q
  192. GO TO 9
  193. C
  194. C Use bisection (C+B)/2.
  195. C
  196. 8 B = B + CMB
  197. C
  198. C Have completed computation for new iterate B.
  199. C
  200. 9 T = B
  201. FB = F(T)
  202. KOUNT = KOUNT + 1
  203. C
  204. C Decide whether next step is interpolation or extrapolation.
  205. C
  206. IF (SIGN(1.0D0,FB) .NE. SIGN(1.0D0,FC)) GO TO 3
  207. C = A
  208. FC = FA
  209. GO TO 3
  210. C
  211. C Finished. Process results for proper setting of IFLAG.
  212. C
  213. 10 IF (SIGN(1.0D0,FB) .EQ. SIGN(1.0D0,FC)) GO TO 13
  214. IF (ABS(FB) .GT. FX) GO TO 12
  215. IFLAG = 1
  216. RETURN
  217. 11 IFLAG = 2
  218. RETURN
  219. 12 IFLAG = 3
  220. RETURN
  221. 13 IFLAG = 4
  222. RETURN
  223. 14 IFLAG = 5
  224. RETURN
  225. END