dsos.f 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. *DECK DSOS
  2. SUBROUTINE DSOS (FNC, NEQ, X, RTOLX, ATOLX, TOLF, IFLAG, RW, LRW,
  3. + IW, LIW)
  4. C***BEGIN PROLOGUE DSOS
  5. C***PURPOSE Solve a square system of nonlinear equations.
  6. C***LIBRARY SLATEC
  7. C***CATEGORY F2A
  8. C***TYPE DOUBLE 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 DSOS 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, DSOS is merely an interfacing routine for
  32. C calling subroutine DSOSEQ 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 DSOSEQ calls the accompanying routine DSOSSL 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 DSOSEQ) for each call
  41. C to the subprogram.
  42. C
  43. C DSOS 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 DOUBLE PRECISION work array which is split apart by DSOS
  86. C and used internally by DSOSEQ.
  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 DSOS and used
  92. C internally by DSOSEQ.
  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 *** DSOS 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
  168. C***REFERENCES K. M. Brown, Solution of simultaneous nonlinear
  169. C equations, Algorithm 316, Communications of the
  170. C A.C.M. 10, (1967), pp. 728-729.
  171. C K. M. Brown, A quadratically convergent Newton-like
  172. C method based upon Gaussian elimination, SIAM Journal
  173. C on Numerical Analysis 6, (1969), pp. 560-569.
  174. C***ROUTINES CALLED DSOSEQ, XERMSG
  175. C***REVISION HISTORY (YYMMDD)
  176. C 801001 DATE WRITTEN
  177. C 890831 Modified array declarations. (WRB)
  178. C 890831 REVISION DATE from Version 3.2
  179. C 891214 Prologue converted to Version 4.0 format. (BAB)
  180. C 900510 Convert XERRWV calls to XERMSG calls, change Prologue
  181. C comments to agree with SOS. (RWC)
  182. C 920501 Reformatted the REFERENCES section. (WRB)
  183. C***END PROLOGUE DSOS
  184. INTEGER IFLAG, INPFLG, IPRINT, IW(*), K1, K2, K3, K4, K5, K6,
  185. 1 LIW, LRW, MXIT, NC, NCJS, NEQ, NSRI, NSRRC
  186. DOUBLE PRECISION ATOLX, FNC, RTOLX, RW(*), TOLF, X(*)
  187. CHARACTER*8 XERN1
  188. CHARACTER*16 XERN3, XERN4
  189. EXTERNAL FNC
  190. C***FIRST EXECUTABLE STATEMENT DSOS
  191. INPFLG = IFLAG
  192. C
  193. C CHECK FOR VALID INPUT
  194. C
  195. IF (NEQ .LE. 0) THEN
  196. WRITE (XERN1, '(I8)') NEQ
  197. CALL XERMSG ('SLATEC', 'DSOS', 'THE NUMBER OF EQUATIONS ' //
  198. * 'MUST BE A POSITIVE INTEGER. YOU HAVE CALLED THE ' //
  199. * 'CODE WITH NEQ = ' // XERN1, 1, 1)
  200. IFLAG = 9
  201. ENDIF
  202. C
  203. IF (RTOLX .LT. 0.0D0 .OR. ATOLX .LT. 0.0D0) THEN
  204. WRITE (XERN3, '(1PE15.6)') ATOLX
  205. WRITE (XERN4, '(1PE15.6)') RTOLX
  206. CALL XERMSG ('SLATEC', 'DSOS', 'THE ERROR TOLERANCES FOR ' //
  207. * 'THE SOLUTION ITERATES CANNOT BE NEGATIVE. YOU HAVE ' //
  208. * 'CALLED THE CODE WITH RTOLX = ' // XERN3 //
  209. * ' AND ATOLX = ' // XERN4,2, 1)
  210. IFLAG = 9
  211. ENDIF
  212. C
  213. IF (TOLF .LT. 0.0D0) THEN
  214. WRITE (XERN3, '(1PE15.6)') TOLF
  215. CALL XERMSG ('SLATEC', 'DSOS', 'THE RESIDUAL ERROR ' //
  216. * 'TOLERANCE MUST BE NON-NEGATIVE. YOU HAVE CALLED THE ' //
  217. * 'CODE WITH TOLF = ' // XERN3, 3, 1)
  218. IFLAG = 9
  219. ENDIF
  220. C
  221. IPRINT = 0
  222. MXIT = 50
  223. IF (INPFLG .EQ. (-1)) THEN
  224. IF (IW(1) .EQ. (-1)) IPRINT = -1
  225. MXIT = IW(2)
  226. IF (MXIT .LE. 0) THEN
  227. WRITE (XERN1, '(I8)') MXIT
  228. CALL XERMSG ('SLATEC', 'DSOS', 'YOU HAVE TOLD THE CODE ' //
  229. * 'TO USE OPTIONAL INPUT ITEMS BY SETTING IFLAG=-1. ' //
  230. * 'HOWEVER YOU HAVE CALLED THE CODE WITH THE MAXIMUM ' //
  231. * 'ALLOWABLE NUMBER OF ITERATIONS SET TO IW(2) = ' //
  232. * XERN1, 4, 1)
  233. IFLAG = 9
  234. ENDIF
  235. ENDIF
  236. C
  237. NC = (NEQ*(NEQ+1))/2
  238. IF (LRW .LT. 1 + 6*NEQ + NC) THEN
  239. WRITE (XERN1, '(I8)') LRW
  240. CALL XERMSG ('SLATEC', 'DSOS', 'DIMENSION OF THE RW ARRAY ' //
  241. * 'MUST BE AT LEAST 1 + 6*NEQ + NEQ*(NEQ+1)/2 . YOU HAVE ' //
  242. * 'CALLED THE CODE WITH LRW = ' // XERN1, 5, 1)
  243. IFLAG = 9
  244. ENDIF
  245. C
  246. IF (LIW .LT. 3 + NEQ) THEN
  247. WRITE (XERN1, '(I8)') LIW
  248. CALL XERMSG ('SLATEC', 'DSOS', 'DIMENSION OF THE IW ARRAY ' //
  249. * 'MUST BE AT LEAST 3 + NEQ. YOU HAVE CALLED THE CODE ' //
  250. * 'WITH LIW = ' // XERN1, 6, 1)
  251. IFLAG = 9
  252. ENDIF
  253. C
  254. IF (IFLAG .NE. 9) THEN
  255. NCJS = 6
  256. NSRRC = 4
  257. NSRI = 5
  258. C
  259. K1 = NC + 2
  260. K2 = K1 + NEQ
  261. K3 = K2 + NEQ
  262. K4 = K3 + NEQ
  263. K5 = K4 + NEQ
  264. K6 = K5 + NEQ
  265. C
  266. CALL DSOSEQ(FNC, NEQ, X, RTOLX, ATOLX, TOLF, IFLAG, MXIT, NCJS,
  267. 1 NSRRC, NSRI, IPRINT, RW(1), RW(2), NC, RW(K1),
  268. 2 RW(K2), RW(K3), RW(K4), RW(K5), RW(K6), IW(4))
  269. C
  270. IW(3) = MXIT
  271. ENDIF
  272. RETURN
  273. END