fzero.f 7.0 KB

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