dnsq.f 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. *DECK DNSQ
  2. SUBROUTINE DNSQ (FCN, JAC, IOPT, N, X, FVEC, FJAC, LDFJAC, XTOL,
  3. + MAXFEV, ML, MU, EPSFCN, DIAG, MODE, FACTOR, NPRINT, INFO, NFEV,
  4. + NJEV, R, LR, QTF, WA1, WA2, WA3, WA4)
  5. C***BEGIN PROLOGUE DNSQ
  6. C***PURPOSE Find a zero of a system of a N nonlinear functions in N
  7. C variables by a modification of the Powell hybrid method.
  8. C***LIBRARY SLATEC
  9. C***CATEGORY F2A
  10. C***TYPE DOUBLE PRECISION (SNSQ-S, DNSQ-D)
  11. C***KEYWORDS NONLINEAR SQUARE SYSTEM, POWELL HYBRID METHOD, ZEROS
  12. C***AUTHOR Hiebert, K. L. (SNLA)
  13. C***DESCRIPTION
  14. C
  15. C 1. Purpose.
  16. C
  17. C The purpose of DNSQ is to find a zero of a system of N nonlinear
  18. C functions in N variables by a modification of the Powell
  19. C hybrid method. The user must provide a subroutine which
  20. C calculates the functions. The user has the option of either to
  21. C provide a subroutine which calculates the Jacobian or to let the
  22. C code calculate it by a forward-difference approximation.
  23. C This code is the combination of the MINPACK codes (Argonne)
  24. C HYBRD and HYBRDJ.
  25. C
  26. C 2. Subroutine and Type Statements.
  27. C
  28. C SUBROUTINE DNSQ(FCN,JAC,IOPT,N,X,FVEC,FJAC,LDFJAC,XTOL,MAXFEV,
  29. C * ML,MU,EPSFCN,DIAG,MODE,FACTOR,NPRINT,INFO,NFEV,
  30. C * NJEV,R,LR,QTF,WA1,WA2,WA3,WA4)
  31. C INTEGER IOPT,N,MAXFEV,ML,MU,MODE,NPRINT,INFO,NFEV,LDFJAC,NJEV,LR
  32. C DOUBLE PRECISION XTOL,EPSFCN,FACTOR
  33. C DOUBLE PRECISION
  34. C X(N),FVEC(N),DIAG(N),FJAC(LDFJAC,N),R(LR),QTF(N),
  35. C * WA1(N),WA2(N),WA3(N),WA4(N)
  36. C EXTERNAL FCN,JAC
  37. C
  38. C 3. Parameters.
  39. C
  40. C Parameters designated as input parameters must be specified on
  41. C entry to DNSQ and are not changed on exit, while parameters
  42. C designated as output parameters need not be specified on entry
  43. C and are set to appropriate values on exit from DNSQ.
  44. C
  45. C FCN is the name of the user-supplied subroutine which calculates
  46. C the functions. FCN must be declared in an EXTERNAL statement
  47. C in the user calling program, and should be written as follows.
  48. C
  49. C SUBROUTINE FCN(N,X,FVEC,IFLAG)
  50. C INTEGER N,IFLAG
  51. C DOUBLE PRECISION X(N),FVEC(N)
  52. C ----------
  53. C CALCULATE THE FUNCTIONS AT X AND
  54. C RETURN THIS VECTOR IN FVEC.
  55. C ----------
  56. C RETURN
  57. C END
  58. C
  59. C The value of IFLAG should not be changed by FCN unless the
  60. C user wants to terminate execution of DNSQ. In this case set
  61. C IFLAG to a negative integer.
  62. C
  63. C JAC is the name of the user-supplied subroutine which calculates
  64. C the Jacobian. If IOPT=1, then JAC must be declared in an
  65. C EXTERNAL statement in the user calling program, and should be
  66. C written as follows.
  67. C
  68. C SUBROUTINE JAC(N,X,FVEC,FJAC,LDFJAC,IFLAG)
  69. C INTEGER N,LDFJAC,IFLAG
  70. C DOUBLE PRECISION X(N),FVEC(N),FJAC(LDFJAC,N)
  71. C ----------
  72. C Calculate the Jacobian at X and return this
  73. C matrix in FJAC. FVEC contains the function
  74. C values at X and should not be altered.
  75. C ----------
  76. C RETURN
  77. C END
  78. C
  79. C The value of IFLAG should not be changed by JAC unless the
  80. C user wants to terminate execution of DNSQ. In this case set
  81. C IFLAG to a negative integer.
  82. C
  83. C If IOPT=2, JAC can be ignored (treat it as a dummy argument).
  84. C
  85. C IOPT is an input variable which specifies how the Jacobian will
  86. C be calculated. If IOPT=1, then the user must supply the
  87. C Jacobian through the subroutine JAC. If IOPT=2, then the
  88. C code will approximate the Jacobian by forward-differencing.
  89. C
  90. C N is a positive integer input variable set to the number of
  91. C functions and variables.
  92. C
  93. C X is an array of length N. On input X must contain an initial
  94. C estimate of the solution vector. On output X contains the
  95. C final estimate of the solution vector.
  96. C
  97. C FVEC is an output array of length N which contains the functions
  98. C evaluated at the output X.
  99. C
  100. C FJAC is an output N by N array which contains the orthogonal
  101. C matrix Q produced by the QR factorization of the final
  102. C approximate Jacobian.
  103. C
  104. C LDFJAC is a positive integer input variable not less than N
  105. C which specifies the leading dimension of the array FJAC.
  106. C
  107. C XTOL is a nonnegative input variable. Termination occurs when
  108. C the relative error between two consecutive iterates is at most
  109. C XTOL. Therefore, XTOL measures the relative error desired in
  110. C the approximate solution. Section 4 contains more details
  111. C about XTOL.
  112. C
  113. C MAXFEV is a positive integer input variable. Termination occurs
  114. C when the number of calls to FCN is at least MAXFEV by the end
  115. C of an iteration.
  116. C
  117. C ML is a nonnegative integer input variable which specifies the
  118. C number of subdiagonals within the band of the Jacobian matrix.
  119. C If the Jacobian is not banded or IOPT=1, set ML to at
  120. C least N - 1.
  121. C
  122. C MU is a nonnegative integer input variable which specifies the
  123. C number of superdiagonals within the band of the Jacobian
  124. C matrix. If the Jacobian is not banded or IOPT=1, set MU to at
  125. C least N - 1.
  126. C
  127. C EPSFCN is an input variable used in determining a suitable step
  128. C for the forward-difference approximation. This approximation
  129. C assumes that the relative errors in the functions are of the
  130. C order of EPSFCN. If EPSFCN is less than the machine
  131. C precision, it is assumed that the relative errors in the
  132. C functions are of the order of the machine precision. If
  133. C IOPT=1, then EPSFCN can be ignored (treat it as a dummy
  134. C argument).
  135. C
  136. C DIAG is an array of length N. If MODE = 1 (see below), DIAG is
  137. C internally set. If MODE = 2, DIAG must contain positive
  138. C entries that serve as implicit (multiplicative) scale factors
  139. C for the variables.
  140. C
  141. C MODE is an integer input variable. If MODE = 1, the variables
  142. C will be scaled internally. If MODE = 2, the scaling is
  143. C specified by the input DIAG. Other values of MODE are
  144. C equivalent to MODE = 1.
  145. C
  146. C FACTOR is a positive input variable used in determining the
  147. C initial step bound. This bound is set to the product of
  148. C FACTOR and the Euclidean norm of DIAG*X if nonzero, or else to
  149. C FACTOR itself. In most cases FACTOR should lie in the
  150. C interval (.1,100.). 100. is a generally recommended value.
  151. C
  152. C NPRINT is an integer input variable that enables controlled
  153. C printing of iterates if it is positive. In this case, FCN is
  154. C called with IFLAG = 0 at the beginning of the first iteration
  155. C and every NPRINT iterations thereafter and immediately prior
  156. C to return, with X and FVEC available for printing. appropriate
  157. C print statements must be added to FCN(see example). If NPRINT
  158. C is not positive, no special calls of FCN with IFLAG = 0 are
  159. C made.
  160. C
  161. C INFO is an integer output variable. If the user has terminated
  162. C execution, INFO is set to the (negative) value of IFLAG. See
  163. C description of FCN and JAC. Otherwise, INFO is set as follows.
  164. C
  165. C INFO = 0 Improper input parameters.
  166. C
  167. C INFO = 1 Relative error between two consecutive iterates is
  168. C at most XTOL.
  169. C
  170. C INFO = 2 Number of calls to FCN has reached or exceeded
  171. C MAXFEV.
  172. C
  173. C INFO = 3 XTOL is too small. No further improvement in the
  174. C approximate solution X is possible.
  175. C
  176. C INFO = 4 Iteration is not making good progress, as measured
  177. C by the improvement from the last five Jacobian
  178. C evaluations.
  179. C
  180. C INFO = 5 Iteration is not making good progress, as measured
  181. C by the improvement from the last ten iterations.
  182. C
  183. C Sections 4 and 5 contain more details about INFO.
  184. C
  185. C NFEV is an integer output variable set to the number of calls to
  186. C FCN.
  187. C
  188. C NJEV is an integer output variable set to the number of calls to
  189. C JAC. (If IOPT=2, then NJEV is set to zero.)
  190. C
  191. C R is an output array of length LR which contains the upper
  192. C triangular matrix produced by the QR factorization of the
  193. C final approximate Jacobian, stored rowwise.
  194. C
  195. C LR is a positive integer input variable not less than
  196. C (N*(N+1))/2.
  197. C
  198. C QTF is an output array of length N which contains the vector
  199. C (Q transpose)*FVEC.
  200. C
  201. C WA1, WA2, WA3, and WA4 are work arrays of length N.
  202. C
  203. C
  204. C 4. Successful completion.
  205. C
  206. C The accuracy of DNSQ is controlled by the convergence parameter
  207. C XTOL. This parameter is used in a test which makes a comparison
  208. C between the approximation X and a solution XSOL. DNSQ
  209. C terminates when the test is satisfied. If the convergence
  210. C parameter is less than the machine precision (as defined by the
  211. C function D1MACH(4)), then DNSQ only attempts to satisfy the test
  212. C defined by the machine precision. Further progress is not
  213. C usually possible.
  214. C
  215. C The test assumes that the functions are reasonably well behaved,
  216. C and, if the Jacobian is supplied by the user, that the functions
  217. C and the Jacobian are coded consistently. If these conditions
  218. C are not satisfied, then DNSQ may incorrectly indicate
  219. C convergence. The coding of the Jacobian can be checked by the
  220. C subroutine DCKDER. If the Jacobian is coded correctly or IOPT=2,
  221. C then the validity of the answer can be checked, for example, by
  222. C rerunning DNSQ with a tighter tolerance.
  223. C
  224. C Convergence Test. If DENORM(Z) denotes the Euclidean norm of a
  225. C vector Z and D is the diagonal matrix whose entries are
  226. C defined by the array DIAG, then this test attempts to
  227. C guarantee that
  228. C
  229. C DENORM(D*(X-XSOL)) .LE. XTOL*DENORM(D*XSOL).
  230. C
  231. C If this condition is satisfied with XTOL = 10**(-K), then the
  232. C larger components of D*X have K significant decimal digits and
  233. C INFO is set to 1. There is a danger that the smaller
  234. C components of D*X may have large relative errors, but the fast
  235. C rate of convergence of DNSQ usually avoids this possibility.
  236. C Unless high precision solutions are required, the recommended
  237. C value for XTOL is the square root of the machine precision.
  238. C
  239. C
  240. C 5. Unsuccessful Completion.
  241. C
  242. C Unsuccessful termination of DNSQ can be due to improper input
  243. C parameters, arithmetic interrupts, an excessive number of
  244. C function evaluations, or lack of good progress.
  245. C
  246. C Improper Input Parameters. INFO is set to 0 if IOPT .LT .1,
  247. C or IOPT .GT. 2, or N .LE. 0, or LDFJAC .LT. N, or
  248. C XTOL .LT. 0.E0, or MAXFEV .LE. 0, or ML .LT. 0, or MU .LT. 0,
  249. C or FACTOR .LE. 0.E0, or LR .LT. (N*(N+1))/2.
  250. C
  251. C Arithmetic Interrupts. If these interrupts occur in the FCN
  252. C subroutine during an early stage of the computation, they may
  253. C be caused by an unacceptable choice of X by DNSQ. In this
  254. C case, it may be possible to remedy the situation by rerunning
  255. C DNSQ with a smaller value of FACTOR.
  256. C
  257. C Excessive Number of Function Evaluations. A reasonable value
  258. C for MAXFEV is 100*(N+1) for IOPT=1 and 200*(N+1) for IOPT=2.
  259. C If the number of calls to FCN reaches MAXFEV, then this
  260. C indicates that the routine is converging very slowly as
  261. C measured by the progress of FVEC, and INFO is set to 2. This
  262. C situation should be unusual because, as indicated below, lack
  263. C of good progress is usually diagnosed earlier by DNSQ,
  264. C causing termination with info = 4 or INFO = 5.
  265. C
  266. C Lack of Good Progress. DNSQ searches for a zero of the system
  267. C by minimizing the sum of the squares of the functions. In so
  268. C doing, it can become trapped in a region where the minimum
  269. C does not correspond to a zero of the system and, in this
  270. C situation, the iteration eventually fails to make good
  271. C progress. In particular, this will happen if the system does
  272. C not have a zero. If the system has a zero, rerunning DNSQ
  273. C from a different starting point may be helpful.
  274. C
  275. C
  276. C 6. Characteristics of The Algorithm.
  277. C
  278. C DNSQ is a modification of the Powell Hybrid method. Two of its
  279. C main characteristics involve the choice of the correction as a
  280. C convex combination of the Newton and scaled gradient directions,
  281. C and the updating of the Jacobian by the rank-1 method of
  282. C Broyden. The choice of the correction guarantees (under
  283. C reasonable conditions) global convergence for starting points
  284. C far from the solution and a fast rate of convergence. The
  285. C Jacobian is calculated at the starting point by either the
  286. C user-supplied subroutine or a forward-difference approximation,
  287. C but it is not recalculated until the rank-1 method fails to
  288. C produce satisfactory progress.
  289. C
  290. C Timing. The time required by DNSQ to solve a given problem
  291. C depends on N, the behavior of the functions, the accuracy
  292. C requested, and the starting point. The number of arithmetic
  293. C operations needed by DNSQ is about 11.5*(N**2) to process
  294. C each evaluation of the functions (call to FCN) and 1.3*(N**3)
  295. C to process each evaluation of the Jacobian (call to JAC,
  296. C if IOPT = 1). Unless FCN and JAC can be evaluated quickly,
  297. C the timing of DNSQ will be strongly influenced by the time
  298. C spent in FCN and JAC.
  299. C
  300. C Storage. DNSQ requires (3*N**2 + 17*N)/2 single precision
  301. C storage locations, in addition to the storage required by the
  302. C program. There are no internally declared storage arrays.
  303. C
  304. C *Long Description:
  305. C
  306. C 7. Example.
  307. C
  308. C The problem is to determine the values of X(1), X(2), ..., X(9),
  309. C which solve the system of tridiagonal equations
  310. C
  311. C (3-2*X(1))*X(1) -2*X(2) = -1
  312. C -X(I-1) + (3-2*X(I))*X(I) -2*X(I+1) = -1, I=2-8
  313. C -X(8) + (3-2*X(9))*X(9) = -1
  314. C C **********
  315. C
  316. C PROGRAM TEST
  317. C C
  318. C C Driver for DNSQ example.
  319. C C
  320. C INTEGER J,IOPT,N,MAXFEV,ML,MU,MODE,NPRINT,INFO,NFEV,LDFJAC,LR,
  321. C * NWRITE
  322. C DOUBLE PRECISION XTOL,EPSFCN,FACTOR,FNORM
  323. C DOUBLE PRECISION X(9),FVEC(9),DIAG(9),FJAC(9,9),R(45),QTF(9),
  324. C * WA1(9),WA2(9),WA3(9),WA4(9)
  325. C DOUBLE PRECISION DENORM,D1MACH
  326. C EXTERNAL FCN
  327. C DATA NWRITE /6/
  328. C C
  329. C IOPT = 2
  330. C N = 9
  331. C C
  332. C C THE FOLLOWING STARTING VALUES PROVIDE A ROUGH SOLUTION.
  333. C C
  334. C DO 10 J = 1, 9
  335. C X(J) = -1.E0
  336. C 10 CONTINUE
  337. C C
  338. C LDFJAC = 9
  339. C LR = 45
  340. C C
  341. C C SET XTOL TO THE SQUARE ROOT OF THE MACHINE PRECISION.
  342. C C UNLESS HIGH PRECISION SOLUTIONS ARE REQUIRED,
  343. C C THIS IS THE RECOMMENDED SETTING.
  344. C C
  345. C XTOL = SQRT(D1MACH(4))
  346. C C
  347. C MAXFEV = 2000
  348. C ML = 1
  349. C MU = 1
  350. C EPSFCN = 0.E0
  351. C MODE = 2
  352. C DO 20 J = 1, 9
  353. C DIAG(J) = 1.E0
  354. C 20 CONTINUE
  355. C FACTOR = 1.E2
  356. C NPRINT = 0
  357. C C
  358. C CALL DNSQ(FCN,JAC,IOPT,N,X,FVEC,FJAC,LDFJAC,XTOL,MAXFEV,ML,MU,
  359. C * EPSFCN,DIAG,MODE,FACTOR,NPRINT,INFO,NFEV,NJEV,
  360. C * R,LR,QTF,WA1,WA2,WA3,WA4)
  361. C FNORM = DENORM(N,FVEC)
  362. C WRITE (NWRITE,1000) FNORM,NFEV,INFO,(X(J),J=1,N)
  363. C STOP
  364. C 1000 FORMAT (5X,' FINAL L2 NORM OF THE RESIDUALS',E15.7 //
  365. C * 5X,' NUMBER OF FUNCTION EVALUATIONS',I10 //
  366. C * 5X,' EXIT PARAMETER',16X,I10 //
  367. C * 5X,' FINAL APPROXIMATE SOLUTION' // (5X,3E15.7))
  368. C END
  369. C SUBROUTINE FCN(N,X,FVEC,IFLAG)
  370. C INTEGER N,IFLAG
  371. C DOUBLE PRECISION X(N),FVEC(N)
  372. C INTEGER K
  373. C DOUBLE PRECISION ONE,TEMP,TEMP1,TEMP2,THREE,TWO,ZERO
  374. C DATA ZERO,ONE,TWO,THREE /0.E0,1.E0,2.E0,3.E0/
  375. C C
  376. C IF (IFLAG .NE. 0) GO TO 5
  377. C C
  378. C C INSERT PRINT STATEMENTS HERE WHEN NPRINT IS POSITIVE.
  379. C C
  380. C RETURN
  381. C 5 CONTINUE
  382. C DO 10 K = 1, N
  383. C TEMP = (THREE - TWO*X(K))*X(K)
  384. C TEMP1 = ZERO
  385. C IF (K .NE. 1) TEMP1 = X(K-1)
  386. C TEMP2 = ZERO
  387. C IF (K .NE. N) TEMP2 = X(K+1)
  388. C FVEC(K) = TEMP - TEMP1 - TWO*TEMP2 + ONE
  389. C 10 CONTINUE
  390. C RETURN
  391. C END
  392. C
  393. C Results obtained with different compilers or machines
  394. C may be slightly different.
  395. C
  396. C Final L2 norm of the residuals 0.1192636E-07
  397. C
  398. C Number of function evaluations 14
  399. C
  400. C Exit parameter 1
  401. C
  402. C Final approximate solution
  403. C
  404. C -0.5706545E+00 -0.6816283E+00 -0.7017325E+00
  405. C -0.7042129E+00 -0.7013690E+00 -0.6918656E+00
  406. C -0.6657920E+00 -0.5960342E+00 -0.4164121E+00
  407. C
  408. C***REFERENCES M. J. D. Powell, A hybrid method for nonlinear equa-
  409. C tions. In Numerical Methods for Nonlinear Algebraic
  410. C Equations, P. Rabinowitz, Editor. Gordon and Breach,
  411. C 1988.
  412. C***ROUTINES CALLED D1MACH, D1MPYQ, D1UPDT, DDOGLG, DENORM, DFDJC1,
  413. C DQFORM, DQRFAC, XERMSG
  414. C***REVISION HISTORY (YYMMDD)
  415. C 800301 DATE WRITTEN
  416. C 890531 Changed all specific intrinsics to generic. (WRB)
  417. C 890831 Modified array declarations. (WRB)
  418. C 890831 REVISION DATE from Version 3.2
  419. C 891214 Prologue converted to Version 4.0 format. (BAB)
  420. C 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
  421. C 920501 Reformatted the REFERENCES section. (WRB)
  422. C***END PROLOGUE DNSQ
  423. DOUBLE PRECISION D1MACH,DENORM
  424. INTEGER I, IFLAG, INFO, IOPT, ITER, IWA(1), J, JM1, L, LDFJAC,
  425. 1 LR, MAXFEV, ML, MODE, MU, N, NCFAIL, NCSUC, NFEV, NJEV,
  426. 2 NPRINT, NSLOW1, NSLOW2
  427. DOUBLE PRECISION ACTRED, DELTA, DIAG(*), EPSFCN, EPSMCH, FACTOR,
  428. 1 FJAC(LDFJAC,*), FNORM, FNORM1, FVEC(*), ONE, P0001, P001,
  429. 2 P1, P5, PNORM, PRERED, QTF(*), R(*), RATIO, SUM, TEMP,
  430. 3 WA1(*), WA2(*), WA3(*), WA4(*), X(*), XNORM, XTOL, ZERO
  431. EXTERNAL FCN
  432. LOGICAL JEVAL,SING
  433. SAVE ONE, P1, P5, P001, P0001, ZERO
  434. DATA ONE,P1,P5,P001,P0001,ZERO
  435. 1 /1.0D0,1.0D-1,5.0D-1,1.0D-3,1.0D-4,0.0D0/
  436. C
  437. C BEGIN BLOCK PERMITTING ...EXITS TO 320
  438. C***FIRST EXECUTABLE STATEMENT DNSQ
  439. EPSMCH = D1MACH(4)
  440. C
  441. INFO = 0
  442. IFLAG = 0
  443. NFEV = 0
  444. NJEV = 0
  445. C
  446. C CHECK THE INPUT PARAMETERS FOR ERRORS.
  447. C
  448. C ...EXIT
  449. IF (IOPT .LT. 1 .OR. IOPT .GT. 2 .OR. N .LE. 0
  450. 1 .OR. XTOL .LT. ZERO .OR. MAXFEV .LE. 0 .OR. ML .LT. 0
  451. 2 .OR. MU .LT. 0 .OR. FACTOR .LE. ZERO .OR. LDFJAC .LT. N
  452. 3 .OR. LR .LT. (N*(N + 1))/2) GO TO 320
  453. IF (MODE .NE. 2) GO TO 20
  454. DO 10 J = 1, N
  455. C .........EXIT
  456. IF (DIAG(J) .LE. ZERO) GO TO 320
  457. 10 CONTINUE
  458. 20 CONTINUE
  459. C
  460. C EVALUATE THE FUNCTION AT THE STARTING POINT
  461. C AND CALCULATE ITS NORM.
  462. C
  463. IFLAG = 1
  464. CALL FCN(N,X,FVEC,IFLAG)
  465. NFEV = 1
  466. C ...EXIT
  467. IF (IFLAG .LT. 0) GO TO 320
  468. FNORM = DENORM(N,FVEC)
  469. C
  470. C INITIALIZE ITERATION COUNTER AND MONITORS.
  471. C
  472. ITER = 1
  473. NCSUC = 0
  474. NCFAIL = 0
  475. NSLOW1 = 0
  476. NSLOW2 = 0
  477. C
  478. C BEGINNING OF THE OUTER LOOP.
  479. C
  480. 30 CONTINUE
  481. C BEGIN BLOCK PERMITTING ...EXITS TO 90
  482. JEVAL = .TRUE.
  483. C
  484. C CALCULATE THE JACOBIAN MATRIX.
  485. C
  486. IF (IOPT .EQ. 2) GO TO 40
  487. C
  488. C USER SUPPLIES JACOBIAN
  489. C
  490. CALL JAC(N,X,FVEC,FJAC,LDFJAC,IFLAG)
  491. NJEV = NJEV + 1
  492. GO TO 50
  493. 40 CONTINUE
  494. C
  495. C CODE APPROXIMATES THE JACOBIAN
  496. C
  497. IFLAG = 2
  498. CALL DFDJC1(FCN,N,X,FVEC,FJAC,LDFJAC,IFLAG,ML,MU,
  499. 1 EPSFCN,WA1,WA2)
  500. NFEV = NFEV + MIN(ML+MU+1,N)
  501. 50 CONTINUE
  502. C
  503. C .........EXIT
  504. IF (IFLAG .LT. 0) GO TO 320
  505. C
  506. C COMPUTE THE QR FACTORIZATION OF THE JACOBIAN.
  507. C
  508. CALL DQRFAC(N,N,FJAC,LDFJAC,.FALSE.,IWA,1,WA1,WA2,WA3)
  509. C
  510. C ON THE FIRST ITERATION AND IF MODE IS 1, SCALE ACCORDING
  511. C TO THE NORMS OF THE COLUMNS OF THE INITIAL JACOBIAN.
  512. C
  513. C ...EXIT
  514. IF (ITER .NE. 1) GO TO 90
  515. IF (MODE .EQ. 2) GO TO 70
  516. DO 60 J = 1, N
  517. DIAG(J) = WA2(J)
  518. IF (WA2(J) .EQ. ZERO) DIAG(J) = ONE
  519. 60 CONTINUE
  520. 70 CONTINUE
  521. C
  522. C ON THE FIRST ITERATION, CALCULATE THE NORM OF THE SCALED
  523. C X AND INITIALIZE THE STEP BOUND DELTA.
  524. C
  525. DO 80 J = 1, N
  526. WA3(J) = DIAG(J)*X(J)
  527. 80 CONTINUE
  528. XNORM = DENORM(N,WA3)
  529. DELTA = FACTOR*XNORM
  530. IF (DELTA .EQ. ZERO) DELTA = FACTOR
  531. 90 CONTINUE
  532. C
  533. C FORM (Q TRANSPOSE)*FVEC AND STORE IN QTF.
  534. C
  535. DO 100 I = 1, N
  536. QTF(I) = FVEC(I)
  537. 100 CONTINUE
  538. DO 140 J = 1, N
  539. IF (FJAC(J,J) .EQ. ZERO) GO TO 130
  540. SUM = ZERO
  541. DO 110 I = J, N
  542. SUM = SUM + FJAC(I,J)*QTF(I)
  543. 110 CONTINUE
  544. TEMP = -SUM/FJAC(J,J)
  545. DO 120 I = J, N
  546. QTF(I) = QTF(I) + FJAC(I,J)*TEMP
  547. 120 CONTINUE
  548. 130 CONTINUE
  549. 140 CONTINUE
  550. C
  551. C COPY THE TRIANGULAR FACTOR OF THE QR FACTORIZATION INTO R.
  552. C
  553. SING = .FALSE.
  554. DO 170 J = 1, N
  555. L = J
  556. JM1 = J - 1
  557. IF (JM1 .LT. 1) GO TO 160
  558. DO 150 I = 1, JM1
  559. R(L) = FJAC(I,J)
  560. L = L + N - I
  561. 150 CONTINUE
  562. 160 CONTINUE
  563. R(L) = WA1(J)
  564. IF (WA1(J) .EQ. ZERO) SING = .TRUE.
  565. 170 CONTINUE
  566. C
  567. C ACCUMULATE THE ORTHOGONAL FACTOR IN FJAC.
  568. C
  569. CALL DQFORM(N,N,FJAC,LDFJAC,WA1)
  570. C
  571. C RESCALE IF NECESSARY.
  572. C
  573. IF (MODE .EQ. 2) GO TO 190
  574. DO 180 J = 1, N
  575. DIAG(J) = MAX(DIAG(J),WA2(J))
  576. 180 CONTINUE
  577. 190 CONTINUE
  578. C
  579. C BEGINNING OF THE INNER LOOP.
  580. C
  581. 200 CONTINUE
  582. C
  583. C IF REQUESTED, CALL FCN TO ENABLE PRINTING OF ITERATES.
  584. C
  585. IF (NPRINT .LE. 0) GO TO 210
  586. IFLAG = 0
  587. IF (MOD(ITER-1,NPRINT) .EQ. 0)
  588. 1 CALL FCN(N,X,FVEC,IFLAG)
  589. C ............EXIT
  590. IF (IFLAG .LT. 0) GO TO 320
  591. 210 CONTINUE
  592. C
  593. C DETERMINE THE DIRECTION P.
  594. C
  595. CALL DDOGLG(N,R,LR,DIAG,QTF,DELTA,WA1,WA2,WA3)
  596. C
  597. C STORE THE DIRECTION P AND X + P. CALCULATE THE NORM OF P.
  598. C
  599. DO 220 J = 1, N
  600. WA1(J) = -WA1(J)
  601. WA2(J) = X(J) + WA1(J)
  602. WA3(J) = DIAG(J)*WA1(J)
  603. 220 CONTINUE
  604. PNORM = DENORM(N,WA3)
  605. C
  606. C ON THE FIRST ITERATION, ADJUST THE INITIAL STEP BOUND.
  607. C
  608. IF (ITER .EQ. 1) DELTA = MIN(DELTA,PNORM)
  609. C
  610. C EVALUATE THE FUNCTION AT X + P AND CALCULATE ITS NORM.
  611. C
  612. IFLAG = 1
  613. CALL FCN(N,WA2,WA4,IFLAG)
  614. NFEV = NFEV + 1
  615. C .........EXIT
  616. IF (IFLAG .LT. 0) GO TO 320
  617. FNORM1 = DENORM(N,WA4)
  618. C
  619. C COMPUTE THE SCALED ACTUAL REDUCTION.
  620. C
  621. ACTRED = -ONE
  622. IF (FNORM1 .LT. FNORM) ACTRED = ONE - (FNORM1/FNORM)**2
  623. C
  624. C COMPUTE THE SCALED PREDICTED REDUCTION.
  625. C
  626. L = 1
  627. DO 240 I = 1, N
  628. SUM = ZERO
  629. DO 230 J = I, N
  630. SUM = SUM + R(L)*WA1(J)
  631. L = L + 1
  632. 230 CONTINUE
  633. WA3(I) = QTF(I) + SUM
  634. 240 CONTINUE
  635. TEMP = DENORM(N,WA3)
  636. PRERED = ZERO
  637. IF (TEMP .LT. FNORM) PRERED = ONE - (TEMP/FNORM)**2
  638. C
  639. C COMPUTE THE RATIO OF THE ACTUAL TO THE PREDICTED
  640. C REDUCTION.
  641. C
  642. RATIO = ZERO
  643. IF (PRERED .GT. ZERO) RATIO = ACTRED/PRERED
  644. C
  645. C UPDATE THE STEP BOUND.
  646. C
  647. IF (RATIO .GE. P1) GO TO 250
  648. NCSUC = 0
  649. NCFAIL = NCFAIL + 1
  650. DELTA = P5*DELTA
  651. GO TO 260
  652. 250 CONTINUE
  653. NCFAIL = 0
  654. NCSUC = NCSUC + 1
  655. IF (RATIO .GE. P5 .OR. NCSUC .GT. 1)
  656. 1 DELTA = MAX(DELTA,PNORM/P5)
  657. IF (ABS(RATIO-ONE) .LE. P1) DELTA = PNORM/P5
  658. 260 CONTINUE
  659. C
  660. C TEST FOR SUCCESSFUL ITERATION.
  661. C
  662. IF (RATIO .LT. P0001) GO TO 280
  663. C
  664. C SUCCESSFUL ITERATION. UPDATE X, FVEC, AND THEIR NORMS.
  665. C
  666. DO 270 J = 1, N
  667. X(J) = WA2(J)
  668. WA2(J) = DIAG(J)*X(J)
  669. FVEC(J) = WA4(J)
  670. 270 CONTINUE
  671. XNORM = DENORM(N,WA2)
  672. FNORM = FNORM1
  673. ITER = ITER + 1
  674. 280 CONTINUE
  675. C
  676. C DETERMINE THE PROGRESS OF THE ITERATION.
  677. C
  678. NSLOW1 = NSLOW1 + 1
  679. IF (ACTRED .GE. P001) NSLOW1 = 0
  680. IF (JEVAL) NSLOW2 = NSLOW2 + 1
  681. IF (ACTRED .GE. P1) NSLOW2 = 0
  682. C
  683. C TEST FOR CONVERGENCE.
  684. C
  685. IF (DELTA .LE. XTOL*XNORM .OR. FNORM .EQ. ZERO) INFO = 1
  686. C .........EXIT
  687. IF (INFO .NE. 0) GO TO 320
  688. C
  689. C TESTS FOR TERMINATION AND STRINGENT TOLERANCES.
  690. C
  691. IF (NFEV .GE. MAXFEV) INFO = 2
  692. IF (P1*MAX(P1*DELTA,PNORM) .LE. EPSMCH*XNORM) INFO = 3
  693. IF (NSLOW2 .EQ. 5) INFO = 4
  694. IF (NSLOW1 .EQ. 10) INFO = 5
  695. C .........EXIT
  696. IF (INFO .NE. 0) GO TO 320
  697. C
  698. C CRITERION FOR RECALCULATING JACOBIAN
  699. C
  700. C ...EXIT
  701. IF (NCFAIL .EQ. 2) GO TO 310
  702. C
  703. C CALCULATE THE RANK ONE MODIFICATION TO THE JACOBIAN
  704. C AND UPDATE QTF IF NECESSARY.
  705. C
  706. DO 300 J = 1, N
  707. SUM = ZERO
  708. DO 290 I = 1, N
  709. SUM = SUM + FJAC(I,J)*WA4(I)
  710. 290 CONTINUE
  711. WA2(J) = (SUM - WA3(J))/PNORM
  712. WA1(J) = DIAG(J)*((DIAG(J)*WA1(J))/PNORM)
  713. IF (RATIO .GE. P0001) QTF(J) = SUM
  714. 300 CONTINUE
  715. C
  716. C COMPUTE THE QR FACTORIZATION OF THE UPDATED JACOBIAN.
  717. C
  718. CALL D1UPDT(N,N,R,LR,WA1,WA2,WA3,SING)
  719. CALL D1MPYQ(N,N,FJAC,LDFJAC,WA2,WA3)
  720. CALL D1MPYQ(1,N,QTF,1,WA2,WA3)
  721. C
  722. C END OF THE INNER LOOP.
  723. C
  724. JEVAL = .FALSE.
  725. GO TO 200
  726. 310 CONTINUE
  727. C
  728. C END OF THE OUTER LOOP.
  729. C
  730. GO TO 30
  731. 320 CONTINUE
  732. C
  733. C TERMINATION, EITHER NORMAL OR USER IMPOSED.
  734. C
  735. IF (IFLAG .LT. 0) INFO = IFLAG
  736. IFLAG = 0
  737. IF (NPRINT .GT. 0) CALL FCN(N,X,FVEC,IFLAG)
  738. IF (INFO .LT. 0) CALL XERMSG ('SLATEC', 'DNSQ',
  739. + 'EXECUTION TERMINATED BECAUSE USER SET IFLAG NEGATIVE.', 1, 1)
  740. IF (INFO .EQ. 0) CALL XERMSG ('SLATEC', 'DNSQ',
  741. + 'INVALID INPUT PARAMETER.', 2, 1)
  742. IF (INFO .EQ. 2) CALL XERMSG ('SLATEC', 'DNSQ',
  743. + 'TOO MANY FUNCTION EVALUATIONS.', 9, 1)
  744. IF (INFO .EQ. 3) CALL XERMSG ('SLATEC', 'DNSQ',
  745. + 'XTOL TOO SMALL. NO FURTHER IMPROVEMENT POSSIBLE.', 3, 1)
  746. IF (INFO .GT. 4) CALL XERMSG ('SLATEC', 'DNSQ',
  747. + 'ITERATION NOT MAKING GOOD PROGRESS.', 1, 1)
  748. RETURN
  749. C
  750. C LAST CARD OF SUBROUTINE DNSQ.
  751. C
  752. END