sos.f 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. *DECK SOS
  2. SUBROUTINE SOS (FNC, NEQ, X, RTOLX, ATOLX, TOLF, IFLAG, RW, LRW,
  3. + IW, LIW)
  4. C***BEGIN PROLOGUE SOS
  5. C***PURPOSE Solve a square system of nonlinear equations.
  6. C***LIBRARY SLATEC
  7. C***CATEGORY F2A
  8. C***TYPE SINGLE PRECISION (SOS-S, DSOS-D)
  9. C***KEYWORDS BROWN'S METHOD, NEWTON'S METHOD, NONLINEAR EQUATIONS,
  10. C ROOTS, SOLUTIONS
  11. C***AUTHOR Watts, H. A., (SNLA)
  12. C***DESCRIPTION
  13. C
  14. C SOS solves a system of NEQ simultaneous nonlinear equations in
  15. C NEQ unknowns. That is, it solves the problem F(X)=0
  16. C where X is a vector with components X(1),...,X(NEQ) and F
  17. C is a vector of nonlinear functions. Each equation is of the form
  18. C
  19. C F (X(1),...,X(NEQ))=0 for K=1,...,NEQ.
  20. C K
  21. C
  22. C The algorithm is based on an iterative method which is a
  23. C variation of Newton's method using Gaussian elimination
  24. C in a manner similar to the Gauss-Seidel process. Convergence
  25. C is roughly quadratic. All partial derivatives required by
  26. C the algorithm are approximated by first difference quotients.
  27. C The convergence behavior of this code is affected by the
  28. C ordering of the equations, and it is advantageous to place linear
  29. C and mildly nonlinear equations first in the ordering.
  30. C
  31. C Actually, SOS is merely an interfacing routine for
  32. C calling subroutine SOSEQS which embodies the solution
  33. C algorithm. The purpose of this is to add greater
  34. C flexibility and ease of use for the prospective user.
  35. C
  36. C SOSEQS calls the accompanying routine SOSSOL, which solves special
  37. C triangular linear systems by back-substitution.
  38. C
  39. C The user must supply a function subprogram which evaluates the
  40. C K-th equation only (K specified by SOSEQS) for each call
  41. C to the subprogram.
  42. C
  43. C SOS represents an implementation of the mathematical algorithm
  44. C described in the references below. It is a modification of the
  45. C code SOSNLE written by H. A. Watts in 1973.
  46. C
  47. C **********************************************************************
  48. C -Input-
  49. C
  50. C FNC -Name of the function program which evaluates the equations.
  51. C This name must be in an EXTERNAL statement in the calling
  52. C program. The user must supply FNC in the form FNC(X,K),
  53. C where X is the solution vector (which must be dimensioned
  54. C in FNC) and FNC returns the value of the K-th function.
  55. C
  56. C NEQ -Number of equations to be solved.
  57. C
  58. C X -Solution vector. Initial guesses must be supplied.
  59. C
  60. C RTOLX -Relative error tolerance used in the convergence criteria.
  61. C Each solution component X(I) is checked by an accuracy test
  62. C of the form ABS(X(I)-XOLD(I)) .LE. RTOLX*ABS(X(I))+ATOLX,
  63. C where XOLD(I) represents the previous iteration value.
  64. C RTOLX must be non-negative.
  65. C
  66. C ATOLX -Absolute error tolerance used in the convergence criteria.
  67. C ATOLX must be non-negative. If the user suspects some
  68. C solution component may be zero, he should set ATOLX to an
  69. C appropriate (depends on the scale of the remaining variables)
  70. C positive value for better efficiency.
  71. C
  72. C TOLF -Residual error tolerance used in the convergence criteria.
  73. C Convergence will be indicated if all residuals (values of the
  74. C functions or equations) are not bigger than TOLF in
  75. C magnitude. Note that extreme care must be given in assigning
  76. C an appropriate value for TOLF because this convergence test
  77. C is dependent on the scaling of the equations. An
  78. C inappropriate value can cause premature termination of the
  79. C iteration process.
  80. C
  81. C IFLAG -Optional input indicator. You must set IFLAG=-1 if you
  82. C want to use any of the optional input items listed below.
  83. C Otherwise set it to zero.
  84. C
  85. C RW -A REAL work array which is split apart by SOS and used
  86. C internally by SOSEQS.
  87. C
  88. C LRW -Dimension of the RW array. LRW must be at least
  89. C 1 + 6*NEQ + NEQ*(NEQ+1)/2
  90. C
  91. C IW -An INTEGER work array which is split apart by SOS and used
  92. C internally by SOSEQS.
  93. C
  94. C LIW -Dimension of the IW array. LIW must be at least 3 + NEQ.
  95. C
  96. C -Optional Input-
  97. C
  98. C IW(1) -Internal printing parameter. You must set IW(1)=-1 if
  99. C you want the intermediate solution iterates to be printed.
  100. C
  101. C IW(2) -Iteration limit. The maximum number of allowable
  102. C iterations can be specified, if desired. To override the
  103. C default value of 50, set IW(2) to the number wanted.
  104. C
  105. C Remember, if you tell the code that you are using one of the
  106. C options (by setting IFLAG=-1), you must supply values
  107. C for both IW(1) and IW(2).
  108. C
  109. C **********************************************************************
  110. C -Output-
  111. C
  112. C X -Solution vector.
  113. C
  114. C IFLAG -Status indicator
  115. C
  116. C *** Convergence to a Solution ***
  117. C
  118. C 1 Means satisfactory convergence to a solution was achieved.
  119. C Each solution component X(I) satisfies the error tolerance
  120. C test ABS(X(I)-XOLD(I)) .LE. RTOLX*ABS(X(I))+ATOLX.
  121. C
  122. C 2 Means procedure converged to a solution such that all
  123. C residuals are at most TOLF in magnitude,
  124. C ABS(FNC(X,I)) .LE. TOLF.
  125. C
  126. C 3 Means that conditions for both IFLAG=1 and IFLAG=2 hold.
  127. C
  128. C 4 Means possible numerical convergence. Behavior indicates
  129. C limiting precision calculations as a result of user asking
  130. C for too much accuracy or else convergence is very slow.
  131. C Residual norms and solution increment norms have
  132. C remained roughly constant over several consecutive
  133. C iterations.
  134. C
  135. C *** Task Interrupted ***
  136. C
  137. C 5 Means the allowable number of iterations has been met
  138. C without obtaining a solution to the specified accuracy.
  139. C Very slow convergence may be indicated. Examine the
  140. C approximate solution returned and see if the error
  141. C tolerances seem appropriate.
  142. C
  143. C 6 Means the allowable number of iterations has been met and
  144. C the iterative process does not appear to be converging.
  145. C A local minimum may have been encountered or there may be
  146. C limiting precision difficulties.
  147. C
  148. C 7 Means that the iterative scheme appears to be diverging.
  149. C Residual norms and solution increment norms have
  150. C increased over several consecutive iterations.
  151. C
  152. C *** Task Cannot Be Continued ***
  153. C
  154. C 8 Means that a Jacobian-related matrix was singular.
  155. C
  156. C 9 Means improper input parameters.
  157. C
  158. C *** IFLAG should be examined after each call to ***
  159. C *** SOS with the appropriate action being taken. ***
  160. C
  161. C
  162. C RW(1) -Contains a norm of the residual.
  163. C
  164. C IW(3) -Contains the number of iterations used by the process.
  165. C
  166. C **********************************************************************
  167. C***REFERENCES K. M. Brown, Solution of simultaneous nonlinear
  168. C equations, Algorithm 316, Communications of the
  169. C A.C.M. 10, (1967), pp. 728-729.
  170. C K. M. Brown, A quadratically convergent Newton-like
  171. C method based upon Gaussian elimination, SIAM Journal
  172. C on Numerical Analysis 6, (1969), pp. 560-569.
  173. C***ROUTINES CALLED SOSEQS, XERMSG
  174. C***REVISION HISTORY (YYMMDD)
  175. C 801001 DATE WRITTEN
  176. C 890831 Modified array declarations. (WRB)
  177. C 890831 REVISION DATE from Version 3.2
  178. C 891214 Prologue converted to Version 4.0 format. (BAB)
  179. C 900510 Convert XERRWV calls to XERMSG calls, changed Prologue
  180. C comments to agree with DSOS. (RWC)
  181. C 920501 Reformatted the REFERENCES section. (WRB)
  182. C***END PROLOGUE SOS
  183. DIMENSION X(*), RW(*), IW(*)
  184. CHARACTER*8 XERN1
  185. CHARACTER*16 XERN3, XERN4
  186. EXTERNAL FNC
  187. C***FIRST EXECUTABLE STATEMENT SOS
  188. INPFLG = IFLAG
  189. C
  190. C CHECK FOR VALID INPUT
  191. C
  192. IF (NEQ .LE. 0) THEN
  193. WRITE (XERN1, '(I8)') NEQ
  194. CALL XERMSG ('SLATEC', 'SOS', 'THE NUMBER OF EQUATIONS ' //
  195. * 'MUST BE A POSITIVE INTEGER. YOU HAVE CALLED THE ' //
  196. * 'CODE WITH NEQ = ' // XERN1, 1, 1)
  197. IFLAG = 9
  198. ENDIF
  199. C
  200. IF (RTOLX .LT. 0.0D0 .OR. ATOLX .LT. 0.0D0) THEN
  201. WRITE (XERN3, '(1PE15.6)') ATOLX
  202. WRITE (XERN4, '(1PE15.6)') RTOLX
  203. CALL XERMSG ('SLATEC', 'SOS', 'THE ERROR TOLERANCES FOR ' //
  204. * 'THE SOLUTION ITERATES CANNOT BE NEGATIVE. YOU HAVE ' //
  205. * 'CALLED THE CODE WITH RTOLX = ' // XERN3 //
  206. * ' AND ATOLX = ' // XERN4,2, 1)
  207. IFLAG = 9
  208. ENDIF
  209. C
  210. IF (TOLF .LT. 0.0D0) THEN
  211. WRITE (XERN3, '(1PE15.6)') TOLF
  212. CALL XERMSG ('SLATEC', 'SOS', 'THE RESIDUAL ERROR ' //
  213. * 'TOLERANCE MUST BE NON-NEGATIVE. YOU HAVE CALLED THE ' //
  214. * 'CODE WITH TOLF = ' // XERN3, 3, 1)
  215. IFLAG = 9
  216. ENDIF
  217. C
  218. IPRINT = 0
  219. MXIT = 50
  220. IF (INPFLG .EQ. (-1)) THEN
  221. IF (IW(1) .EQ. (-1)) IPRINT = -1
  222. MXIT = IW(2)
  223. IF (MXIT .LE. 0) THEN
  224. WRITE (XERN1, '(I8)') MXIT
  225. CALL XERMSG ('SLATEC', 'SOS', 'YOU HAVE TOLD THE CODE ' //
  226. * 'TO USE OPTIONAL IN PUT ITEMS BY SETTING IFLAG=-1. ' //
  227. * 'HOWEVER YOU HAVE CALLED THE CODE WITH THE MAXIMUM ' //
  228. * 'ALLOWABLE NUMBER OF ITERATIONS SET TO IW(2) = ' //
  229. * XERN1, 4, 1)
  230. IFLAG = 9
  231. ENDIF
  232. ENDIF
  233. C
  234. NC = (NEQ*(NEQ+1))/2
  235. IF (LRW .LT. 1 + 6*NEQ + NC) THEN
  236. WRITE (XERN1, '(I8)') LRW
  237. CALL XERMSG ('SLATEC', 'SOS', 'DIMENSION OF THE RW ARRAY ' //
  238. * 'MUST BE AT LEAST 1 + 6*NEQ + NEQ*(NEQ+1)/2 . YOU HAVE ' //
  239. * 'CALLED THE CODE WITH LRW = ' // XERN1, 5, 1)
  240. IFLAG = 9
  241. ENDIF
  242. C
  243. IF (LIW .LT. 3 + NEQ) THEN
  244. WRITE (XERN1, '(I8)') LIW
  245. CALL XERMSG ('SLATEC', 'SOS', 'DIMENSION OF THE IW ARRAY ' //
  246. * 'MUST BE AT LEAST 3 + NEQ. YOU HAVE CALLED THE CODE ' //
  247. * 'WITH LIW = ' // XERN1, 6, 1)
  248. IFLAG = 9
  249. ENDIF
  250. C
  251. IF (IFLAG .NE. 9) THEN
  252. NCJS = 6
  253. NSRRC = 4
  254. NSRI = 5
  255. C
  256. K1 = NC + 2
  257. K2 = K1 + NEQ
  258. K3 = K2 + NEQ
  259. K4 = K3 + NEQ
  260. K5 = K4 + NEQ
  261. K6 = K5 + NEQ
  262. C
  263. CALL SOSEQS(FNC, NEQ, X, RTOLX, ATOLX, TOLF, IFLAG, MXIT, NCJS,
  264. 1 NSRRC, NSRI, IPRINT, RW(1), RW(2), NC, RW(K1),
  265. 2 RW(K2), RW(K3), RW(K4), RW(K5), RW(K6), IW(4))
  266. C
  267. IW(3) = MXIT
  268. ENDIF
  269. RETURN
  270. END