snsqe.f 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. *DECK SNSQE
  2. SUBROUTINE SNSQE (FCN, JAC, IOPT, N, X, FVEC, TOL, NPRINT, INFO,
  3. + WA, LWA)
  4. C***BEGIN PROLOGUE SNSQE
  5. C***PURPOSE An easy-to-use code to find a zero of a system of N
  6. C nonlinear functions in N variables by a modification of
  7. C the Powell hybrid method.
  8. C***LIBRARY SLATEC
  9. C***CATEGORY F2A
  10. C***TYPE SINGLE PRECISION (SNSQE-S, DNSQE-D)
  11. C***KEYWORDS EASY-TO-USE, NONLINEAR SQUARE SYSTEM,
  12. C POWELL HYBRID METHOD, ZEROS
  13. C***AUTHOR Hiebert, K. L., (SNLA)
  14. C***DESCRIPTION
  15. C
  16. C 1. Purpose.
  17. C
  18. C
  19. C The purpose of SNSQE is to find a zero of a system of N non-
  20. C linear functions in N variables by a modification of the Powell
  21. C hybrid method. This is done by using the more general nonlinear
  22. C equation solver SNSQ. The user must provide a subroutine which
  23. C calculates the functions. The user has the option of either to
  24. C provide a subroutine which calculates the Jacobian or to let the
  25. C code calculate it by a forward-difference approximation. This
  26. C code is the combination of the MINPACK codes (Argonne) HYBRD1
  27. C and HYBRJ1.
  28. C
  29. C
  30. C 2. Subroutine and Type Statements.
  31. C
  32. C SUBROUTINE SNSQE(FCN,JAC,IOPT,N,X,FVEC,TOL,NPRINT,INFO,
  33. C * WA,LWA)
  34. C INTEGER IOPT,N,NPRINT,INFO,LWA
  35. C REAL TOL
  36. C REAL X(N),FVEC(N),WA(LWA)
  37. C EXTERNAL FCN,JAC
  38. C
  39. C
  40. C 3. Parameters.
  41. C
  42. C Parameters designated as input parameters must be specified on
  43. C entry to SNSQE and are not changed on exit, while parameters
  44. C designated as output parameters need not be specified on entry
  45. C and are set to appropriate values on exit from SNSQE.
  46. C
  47. C FCN is the name of the user-supplied subroutine which calculates
  48. C the functions. FCN must be declared in an EXTERNAL statement
  49. C in the user calling program, and should be written as follows.
  50. C
  51. C SUBROUTINE FCN(N,X,FVEC,IFLAG)
  52. C INTEGER N,IFLAG
  53. C REAL X(N),FVEC(N)
  54. C ----------
  55. C Calculate the functions at X and
  56. C return this vector in FVEC.
  57. C ----------
  58. C RETURN
  59. C END
  60. C
  61. C The value of IFLAG should not be changed by FCN unless the
  62. C user wants to terminate execution of SNSQE. In this case, set
  63. C IFLAG to a negative integer.
  64. C
  65. C JAC is the name of the user-supplied subroutine which calculates
  66. C the Jacobian. If IOPT=1, then JAC must be declared in an
  67. C EXTERNAL statement in the user calling program, and should be
  68. C written as follows.
  69. C
  70. C SUBROUTINE JAC(N,X,FVEC,FJAC,LDFJAC,IFLAG)
  71. C INTEGER N,LDFJAC,IFLAG
  72. C REAL X(N),FVEC(N),FJAC(LDFJAC,N)
  73. C ----------
  74. C Calculate the Jacobian at X and return this
  75. C matrix in FJAC. FVEC contains the function
  76. C values at X and should not be altered.
  77. C ----------
  78. C RETURN
  79. C END
  80. C
  81. C The value of IFLAG should not be changed by JAC unless the
  82. C user wants to terminate execution of SNSQE. In this case, set
  83. C IFLAG to a negative integer.
  84. C
  85. C If IOPT=2, JAC can be ignored (treat it as a dummy argument).
  86. C
  87. C IOPT is an input variable which specifies how the Jacobian will
  88. C be calculated. If IOPT=1, then the user must supply the
  89. C Jacobian through the subroutine JAC. If IOPT=2, then the
  90. C code will approximate the Jacobian by forward-differencing.
  91. C
  92. C N is a positive integer input variable set to the number of
  93. C functions and variables.
  94. C
  95. C X is an array of length N. On input, X must contain an initial
  96. C estimate of the solution vector. On output, X contains the
  97. C final estimate of the solution vector.
  98. C
  99. C FVEC is an output array of length N which contains the functions
  100. C evaluated at the output X.
  101. C
  102. C TOL is a non-negative input variable. Termination occurs when
  103. C the algorithm estimates that the relative error between X and
  104. C the solution is at most TOL. Section 4 contains more details
  105. C about TOL.
  106. C
  107. C NPRINT is an integer input variable that enables controlled
  108. C printing of iterates if it is positive. In this case, FCN is
  109. C called with IFLAG = 0 at the beginning of the first iteration
  110. C and every NPRINT iteration thereafter and immediately prior
  111. C to return, with X and FVEC available for printing. Appropriate
  112. C print statements must be added to FCN (see example). If NPRINT
  113. C is not positive, no special calls of FCN with IFLAG = 0 are
  114. C made.
  115. C
  116. C INFO is an integer output variable. If the user has terminated
  117. C execution, INFO is set to the (negative) value of IFLAG. See
  118. C description of FCN and JAC. Otherwise, INFO is set as follows.
  119. C
  120. C INFO = 0 improper input parameters.
  121. C
  122. C INFO = 1 algorithm estimates that the relative error between
  123. C X and the solution is at most TOL.
  124. C
  125. C INFO = 2 number of calls to FCN has reached or exceeded
  126. C 100*(N+1) for IOPT=1 or 200*(N+1) for IOPT=2.
  127. C
  128. C INFO = 3 TOL is too small. No further improvement in the
  129. C approximate solution X is possible.
  130. C
  131. C INFO = 4 iteration is not making good progress.
  132. C
  133. C Sections 4 and 5 contain more details about INFO.
  134. C
  135. C WA is a work array of length LWA.
  136. C
  137. C LWA is a positive integer input variable not less than
  138. C (3*N**2+13*N))/2.
  139. C
  140. C
  141. C 4. Successful Completion.
  142. C
  143. C The accuracy of SNSQE is controlled by the convergence parame-
  144. C ter TOL. This parameter is used in a test which makes a compar-
  145. C ison between the approximation X and a solution XSOL. SNSQE
  146. C terminates when the test is satisfied. If TOL is less than the
  147. C machine precision (as defined by the function R1MACH(4)), then
  148. C SNSQE attempts only to satisfy the test defined by the machine
  149. C precision. Further progress is not usually possible. Unless
  150. C high precision solutions are required, the recommended value
  151. C for TOL is the square root of the machine precision.
  152. C
  153. C The test assumes that the functions are reasonably well behaved,
  154. C and, if the Jacobian is supplied by the user, that the functions
  155. C and the Jacobian coded consistently. If these conditions
  156. C are not satisfied, SNSQE may incorrectly indicate convergence.
  157. C The coding of the Jacobian can be checked by the subroutine
  158. C CHKDER. If the Jacobian is coded correctly or IOPT=2, then
  159. C the validity of the answer can be checked, for example, by
  160. C rerunning SNSQE with a tighter tolerance.
  161. C
  162. C Convergence Test. If ENORM(Z) denotes the Euclidean norm of a
  163. C vector Z, then this test attempts to guarantee that
  164. C
  165. C ENORM(X-XSOL) .LE. TOL*ENORM(XSOL).
  166. C
  167. C If this condition is satisfied with TOL = 10**(-K), then the
  168. C larger components of X have K significant decimal digits and
  169. C INFO is set to 1. There is a danger that the smaller compo-
  170. C nents of X may have large relative errors, but the fast rate
  171. C of convergence of SNSQE usually avoids this possibility.
  172. C
  173. C
  174. C 5. Unsuccessful Completion.
  175. C
  176. C Unsuccessful termination of SNSQE can be due to improper input
  177. C parameters, arithmetic interrupts, an excessive number of func-
  178. C tion evaluations, errors in the functions, or lack of good prog-
  179. C ress.
  180. C
  181. C Improper Input Parameters. INFO is set to 0 if IOPT .LT. 1, or
  182. C IOPT .GT. 2, or N .LE. 0, or TOL .LT. 0.E0, or
  183. C LWA .LT. (3*N**2+13*N)/2.
  184. C
  185. C Arithmetic Interrupts. If these interrupts occur in the FCN
  186. C subroutine during an early stage of the computation, they may
  187. C be caused by an unacceptable choice of X by SNSQE. In this
  188. C case, it may be possible to remedy the situation by not evalu-
  189. C ating the functions here, but instead setting the components
  190. C of FVEC to numbers that exceed those in the initial FVEC.
  191. C
  192. C Excessive Number of Function Evaluations. If the number of
  193. C calls to FCN reaches 100*(N+1) for IOPT=1 or 200*(N+1) for
  194. C IOPT=2, then this indicates that the routine is converging
  195. C very slowly as measured by the progress of FVEC, and INFO is
  196. C set to 2. This situation should be unusual because, as
  197. C indicated below, lack of good progress is usually diagnosed
  198. C earlier by SNSQE, causing termination with INFO = 4.
  199. C
  200. C Errors in the Functions. When IOPT=2, the choice of step length
  201. C in the forward-difference approximation to the Jacobian
  202. C assumes that the relative errors in the functions are of the
  203. C order of the machine precision. If this is not the case,
  204. C SNSQE may fail (usually with INFO = 4). The user should
  205. C then either use SNSQ and set the step length or use IOPT=1
  206. C and supply the Jacobian.
  207. C
  208. C Lack of Good Progress. SNSQE searches for a zero of the system
  209. C by minimizing the sum of the squares of the functions. In so
  210. C doing, it can become trapped in a region where the minimum
  211. C does not correspond to a zero of the system and, in this situ-
  212. C ation, the iteration eventually fails to make good progress.
  213. C In particular, this will happen if the system does not have a
  214. C zero. If the system has a zero, rerunning SNSQE from a dif-
  215. C ferent starting point may be helpful.
  216. C
  217. C
  218. C 6. Characteristics of the Algorithm.
  219. C
  220. C SNSQE is a modification of the Powell hybrid method. Two of
  221. C its main characteristics involve the choice of the correction as
  222. C a convex combination of the Newton and scaled gradient direc-
  223. C tions, and the updating of the Jacobian by the rank-1 method of
  224. C Broyden. The choice of the correction guarantees (under reason-
  225. C able conditions) global convergence for starting points far from
  226. C the solution and a fast rate of convergence. The Jacobian is
  227. C calculated at the starting point by either the user-supplied
  228. C subroutine or a forward-difference approximation, but it is not
  229. C recalculated until the rank-1 method fails to produce satis-
  230. C factory progress.
  231. C
  232. C Timing. The time required by SNSQE to solve a given problem
  233. C depends on N, the behavior of the functions, the accuracy
  234. C requested, and the starting point. The number of arithmetic
  235. C operations needed by SNSQE is about 11.5*(N**2) to process
  236. C each evaluation of the functions (call to FCN) and 1.3*(N**3)
  237. C to process each evaluation of the Jacobian (call to JAC,
  238. C if IOPT = 1). Unless FCN and JAC can be evaluated quickly,
  239. C the timing of SNSQE will be strongly influenced by the time
  240. C spent in FCN and JAC.
  241. C
  242. C Storage. SNSQE requires (3*N**2 + 17*N)/2 single precision
  243. C storage locations, in addition to the storage required by the
  244. C program. There are no internally declared storage arrays.
  245. C
  246. C
  247. C 7. Example.
  248. C
  249. C The problem is to determine the values of X(1), X(2), ..., X(9),
  250. C which solve the system of tridiagonal equations
  251. C
  252. C (3-2*X(1))*X(1) -2*X(2) = -1
  253. C -X(I-1) + (3-2*X(I))*X(I) -2*X(I+1) = -1, I=2-8
  254. C -X(8) + (3-2*X(9))*X(9) = -1
  255. C
  256. C **********
  257. C
  258. C PROGRAM TEST
  259. C C
  260. C C Driver for SNSQE example.
  261. C C
  262. C INTEGER J,N,IOPT,NPRINT,INFO,LWA,NWRITE
  263. C REAL TOL,FNORM
  264. C REAL X(9),FVEC(9),WA(180)
  265. C REAL ENORM,R1MACH
  266. C EXTERNAL FCN
  267. C DATA NWRITE /6/
  268. C C
  269. C IOPT = 2
  270. C N = 9
  271. C C
  272. C C The following starting values provide a rough solution.
  273. C C
  274. C DO 10 J = 1, 9
  275. C X(J) = -1.E0
  276. C 10 CONTINUE
  277. C
  278. C LWA = 180
  279. C NPRINT = 0
  280. C C
  281. C C Set TOL to the square root of the machine precision.
  282. C C Unless high precision solutions are required,
  283. C C this is the recommended setting.
  284. C C
  285. C TOL = SQRT(R1MACH(4))
  286. C C
  287. C CALL SNSQE(FCN,JAC,IOPT,N,X,FVEC,TOL,NPRINT,INFO,WA,LWA)
  288. C FNORM = ENORM(N,FVEC)
  289. C WRITE (NWRITE,1000) FNORM,INFO,(X(J),J=1,N)
  290. C STOP
  291. C 1000 FORMAT (5X,' FINAL L2 NORM OF THE RESIDUALS',E15.7 //
  292. C * 5X,' EXIT PARAMETER',16X,I10 //
  293. C * 5X,' FINAL APPROXIMATE SOLUTION' // (5X,3E15.7))
  294. C END
  295. C SUBROUTINE FCN(N,X,FVEC,IFLAG)
  296. C INTEGER N,IFLAG
  297. C REAL X(N),FVEC(N)
  298. C INTEGER K
  299. C REAL ONE,TEMP,TEMP1,TEMP2,THREE,TWO,ZERO
  300. C DATA ZERO,ONE,TWO,THREE /0.E0,1.E0,2.E0,3.E0/
  301. C C
  302. C DO 10 K = 1, N
  303. C TEMP = (THREE - TWO*X(K))*X(K)
  304. C TEMP1 = ZERO
  305. C IF (K .NE. 1) TEMP1 = X(K-1)
  306. C TEMP2 = ZERO
  307. C IF (K .NE. N) TEMP2 = X(K+1)
  308. C FVEC(K) = TEMP - TEMP1 - TWO*TEMP2 + ONE
  309. C 10 CONTINUE
  310. C RETURN
  311. C END
  312. C
  313. C Results obtained with different compilers or machines
  314. C may be slightly different.
  315. C
  316. C FINAL L2 NORM OF THE RESIDUALS 0.1192636E-07
  317. C
  318. C EXIT PARAMETER 1
  319. C
  320. C FINAL APPROXIMATE SOLUTION
  321. C
  322. C -0.5706545E+00 -0.6816283E+00 -0.7017325E+00
  323. C -0.7042129E+00 -0.7013690E+00 -0.6918656E+00
  324. C -0.6657920E+00 -0.5960342E+00 -0.4164121E+00
  325. C
  326. C***REFERENCES M. J. D. Powell, A hybrid method for nonlinear equa-
  327. C tions. In Numerical Methods for Nonlinear Algebraic
  328. C Equations, P. Rabinowitz, Editor. Gordon and Breach,
  329. C 1988.
  330. C***ROUTINES CALLED SNSQ, XERMSG
  331. C***REVISION HISTORY (YYMMDD)
  332. C 800301 DATE WRITTEN
  333. C 890831 Modified array declarations. (WRB)
  334. C 890831 REVISION DATE from Version 3.2
  335. C 891214 Prologue converted to Version 4.0 format. (BAB)
  336. C 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
  337. C 920501 Reformatted the REFERENCES section. (WRB)
  338. C***END PROLOGUE SNSQE
  339. INTEGER IOPT,N,NPRINT,INFO,LWA
  340. REAL TOL
  341. REAL X(*),FVEC(*),WA(LWA)
  342. EXTERNAL FCN, JAC
  343. INTEGER INDEX,J,LR,MAXFEV,ML,MODE,MU,NFEV,NJEV
  344. REAL EPSFCN,FACTOR,ONE,XTOL,ZERO
  345. SAVE FACTOR, ONE, ZERO
  346. DATA FACTOR,ONE,ZERO /1.0E2,1.0E0,0.0E0/
  347. C***FIRST EXECUTABLE STATEMENT SNSQE
  348. INFO = 0
  349. C
  350. C CHECK THE INPUT PARAMETERS FOR ERRORS.
  351. C
  352. IF (IOPT .LT. 1 .OR. IOPT .GT. 2 .OR. N .LE. 0
  353. 1 .OR. TOL .LT. ZERO .OR. LWA .LT. (3*N**2 +13*N)/2)
  354. 2 GO TO 20
  355. C
  356. C CALL SNSQ.
  357. C
  358. MAXFEV = 100*(N + 1)
  359. IF (IOPT .EQ. 2) MAXFEV = 2*MAXFEV
  360. XTOL = TOL
  361. ML = N - 1
  362. MU = N - 1
  363. EPSFCN = ZERO
  364. MODE = 2
  365. DO 10 J = 1, N
  366. WA(J) = ONE
  367. 10 CONTINUE
  368. LR = (N*(N + 1))/2
  369. INDEX=6*N+LR
  370. CALL SNSQ(FCN,JAC,IOPT,N,X,FVEC,WA(INDEX+1),N,XTOL,MAXFEV,ML,MU,
  371. 1 EPSFCN,WA(1),MODE,FACTOR,NPRINT,INFO,NFEV,NJEV,
  372. 2 WA(6*N+1),LR,WA(N+1),WA(2*N+1),WA(3*N+1),WA(4*N+1),
  373. 3 WA(5*N+1))
  374. IF (INFO .EQ. 5) INFO = 4
  375. 20 CONTINUE
  376. IF (INFO .EQ. 0) CALL XERMSG ('SLATEC', 'SNSQE',
  377. + 'INVALID INPUT PARAMETER.', 2, 1)
  378. RETURN
  379. C
  380. C LAST CARD OF SUBROUTINE SNSQE.
  381. C
  382. END