dnsqe.f 15 KB

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