snsq.f 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. *DECK SNSQ
  2. SUBROUTINE SNSQ (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 SNSQ
  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 SINGLE 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 SNSQ is to find a zero of a system of N non-
  18. C linear functions in N variables by a modification of the Powell
  19. C hybrid method. The user must provide a subroutine which calcu-
  20. C lates 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
  27. C 2. Subroutine and Type Statements.
  28. C
  29. C SUBROUTINE SNSQ(FCN,JAC,IOPT,N,X,FVEC,FJAC,LDFJAC,XTOL,MAXFEV,
  30. C * ML,MU,EPSFCN,DIAG,MODE,FACTOR,NPRINT,INFO,NFEV,
  31. C * NJEV,R,LR,QTF,WA1,WA2,WA3,WA4)
  32. C INTEGER IOPT,N,MAXFEV,ML,MU,MODE,NPRINT,INFO,NFEV,LDFJAC,NJEV,LR
  33. C REAL XTOL,EPSFCN,FACTOR
  34. C REAL 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
  39. C 3. Parameters.
  40. C
  41. C Parameters designated as input parameters must be specified on
  42. C entry to SNSQ and are not changed on exit, while parameters
  43. C designated as output parameters need not be specified on entry
  44. C and are set to appropriate values on exit from SNSQ.
  45. C
  46. C FCN is the name of the user-supplied subroutine which calculates
  47. C the functions. FCN must be declared in an EXTERNAL statement
  48. C in the user calling program, and should be written as follows.
  49. C
  50. C SUBROUTINE FCN(N,X,FVEC,IFLAG)
  51. C INTEGER N,IFLAG
  52. C REAL X(N),FVEC(N)
  53. C ----------
  54. C Calculate the functions at X and
  55. C return this vector in FVEC.
  56. C ----------
  57. C RETURN
  58. C END
  59. C
  60. C The value of IFLAG should not be changed by FCN unless the
  61. C user wants to terminate execution of SNSQ. In this case, set
  62. C IFLAG to a negative integer.
  63. C
  64. C JAC is the name of the user-supplied subroutine which calculates
  65. C the Jacobian. If IOPT=1, then JAC must be declared in an
  66. C EXTERNAL statement in the user calling program, and should be
  67. C written as follows.
  68. C
  69. C SUBROUTINE JAC(N,X,FVEC,FJAC,LDFJAC,IFLAG)
  70. C INTEGER N,LDFJAC,IFLAG
  71. C REAL X(N),FVEC(N),FJAC(LDFJAC,N)
  72. C ----------
  73. C Calculate the Jacobian at X and return this
  74. C matrix in FJAC. FVEC contains the function
  75. C values at X and should not be altered.
  76. C ----------
  77. C RETURN
  78. C END
  79. C
  80. C The value of IFLAG should not be changed by JAC unless the
  81. C user wants to terminate execution of SNSQ. In this case, set
  82. C IFLAG to a negative integer.
  83. C
  84. C If IOPT=2, JAC can be ignored (treat it as a dummy argument).
  85. C
  86. C IOPT is an input variable which specifies how the Jacobian will
  87. C be calculated. If IOPT=1, then the user must supply the
  88. C Jacobian through the subroutine JAC. If IOPT=2, then the
  89. C code will approximate the Jacobian by forward-differencing.
  90. C
  91. C N is a positive integer input variable set to the number of
  92. C functions and variables.
  93. C
  94. C X is an array of length N. On input, X must contain an initial
  95. C estimate of the solution vector. On output, X contains the
  96. C final estimate of the solution vector.
  97. C
  98. C FVEC is an output array of length N which contains the functions
  99. C evaluated at the output X.
  100. C
  101. C FJAC is an output N by N array which contains the orthogonal
  102. C matrix Q produced by the QR factorization of the final approx-
  103. C imate Jacobian.
  104. C
  105. C LDFJAC is a positive integer input variable not less than N
  106. C which specifies the leading dimension of the array FJAC.
  107. C
  108. C XTOL is a non-negative input variable. Termination occurs when
  109. C the relative error between two consecutive iterates is at most
  110. C XTOL. Therefore, XTOL measures the relative error desired in
  111. C the approximate solution. Section 4 contains more details
  112. C about XTOL.
  113. C
  114. C MAXFEV is a positive integer input variable. Termination occurs
  115. C when the number of calls to FCN is at least MAXFEV by the end
  116. C of an iteration.
  117. C
  118. C ML is a non-negative integer input variable which specifies the
  119. C number of subdiagonals within the band of the Jacobian matrix.
  120. C If the Jacobian is not banded or IOPT=1, set ML to at
  121. C least N - 1.
  122. C
  123. C MU is a non-negative integer input variable which specifies the
  124. C number of superdiagonals within the band of the Jacobian
  125. C matrix. If the Jacobian is not banded or IOPT=1, set MU to at
  126. C least N - 1.
  127. C
  128. C EPSFCN is an input variable used in determining a suitable step
  129. C for the forward-difference approximation. This approximation
  130. C assumes that the relative errors in the functions are of the
  131. C order of EPSFCN. If EPSFCN is less than the machine preci-
  132. C sion, it is assumed that the relative errors in the functions
  133. C are of the order of the machine precision. If IOPT=1, then
  134. C EPSFCN can be ignored (treat it as a dummy 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 speci-
  143. C fied by the input DIAG. Other values of MODE are equivalent
  144. C to MODE = 1.
  145. C
  146. C FACTOR is a positive input variable used in determining the ini-
  147. C tial step bound. This bound is set to the product of FACTOR
  148. C and the Euclidean norm of DIAG*X if nonzero, or else to FACTOR
  149. C itself. In most cases FACTOR should lie in the interval
  150. C (.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 iteration 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 eval-
  178. C uations.
  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 SNSQ 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. SNSQ termi-
  209. C nates when the test is satisfied. If the convergence parameter
  210. C is less than the machine precision (as defined by the function
  211. C R1MACH(4)), then SNSQ 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 SNSQ may incorrectly indicate conver-
  219. C gence. The coding of the Jacobian can be checked by the
  220. C subroutine CHKDER. 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 SNSQ with a tighter tolerance.
  223. C
  224. C Convergence Test. If ENORM(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 guaran-
  227. C tee that
  228. C
  229. C ENORM(D*(X-XSOL)) .LE. XTOL*ENORM(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 compo-
  234. C nents of D*X may have large relative errors, but the fast rate
  235. C of convergence of SNSQ 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 SNSQ can be due to improper input
  243. C parameters, arithmetic interrupts, an excessive number of func-
  244. C tion 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 SNSQ. In this
  254. C case, it may be possible to remedy the situation by rerunning
  255. C SNSQ 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 SNSQ,
  264. C causing termination with INFO = 4 or INFO = 5.
  265. C
  266. C Lack of Good Progress. SNSQ 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 situ-
  270. C ation, the iteration eventually fails to make good progress.
  271. C In particular, this will happen if the system does not have a
  272. C zero. If the system has a zero, rerunning SNSQ from a dif-
  273. C ferent starting point may be helpful.
  274. C
  275. C
  276. C 6. Characteristics of the Algorithm.
  277. C
  278. C SNSQ 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 Broy-
  282. C den. The choice of the correction guarantees (under reasonable
  283. C conditions) global convergence for starting points far from the
  284. C solution and a fast rate of convergence. The Jacobian is
  285. C calculated at the starting point by either the user-supplied
  286. C subroutine or a forward-difference approximation, but it is not
  287. C recalculated until the rank-1 method fails to produce satis-
  288. C factory progress.
  289. C
  290. C Timing. The time required by SNSQ 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 SNSQ 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 SNSQ will be strongly influenced by the time
  298. C spent in FCN and JAC.
  299. C
  300. C Storage. SNSQ 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
  305. C 7. Example.
  306. C
  307. C The problem is to determine the values of X(1), X(2), ..., X(9),
  308. C which solve the system of tridiagonal equations
  309. C
  310. C (3-2*X(1))*X(1) -2*X(2) = -1
  311. C -X(I-1) + (3-2*X(I))*X(I) -2*X(I+1) = -1, I=2-8
  312. C -X(8) + (3-2*X(9))*X(9) = -1
  313. C C **********
  314. C
  315. C PROGRAM TEST
  316. C C
  317. C C Driver for SNSQ example.
  318. C C
  319. C INTEGER J,IOPT,N,MAXFEV,ML,MU,MODE,NPRINT,INFO,NFEV,LDFJAC,LR,
  320. C * NWRITE
  321. C REAL XTOL,EPSFCN,FACTOR,FNORM
  322. C REAL X(9),FVEC(9),DIAG(9),FJAC(9,9),R(45),QTF(9),
  323. C * WA1(9),WA2(9),WA3(9),WA4(9)
  324. C REAL ENORM,R1MACH
  325. C EXTERNAL FCN
  326. C DATA NWRITE /6/
  327. C C
  328. C IOPT = 2
  329. C N = 9
  330. C C
  331. C C The following starting values provide a rough solution.
  332. C C
  333. C DO 10 J = 1, 9
  334. C X(J) = -1.E0
  335. C 10 CONTINUE
  336. C C
  337. C LDFJAC = 9
  338. C LR = 45
  339. C C
  340. C C Set XTOL to the square root of the machine precision.
  341. C C Unless high precision solutions are required,
  342. C C this is the recommended setting.
  343. C C
  344. C XTOL = SQRT(R1MACH(4))
  345. C C
  346. C MAXFEV = 2000
  347. C ML = 1
  348. C MU = 1
  349. C EPSFCN = 0.E0
  350. C MODE = 2
  351. C DO 20 J = 1, 9
  352. C DIAG(J) = 1.E0
  353. C 20 CONTINUE
  354. C FACTOR = 1.E2
  355. C NPRINT = 0
  356. C C
  357. C CALL SNSQ(FCN,JAC,IOPT,N,X,FVEC,FJAC,LDFJAC,XTOL,MAXFEV,ML,MU,
  358. C * EPSFCN,DIAG,MODE,FACTOR,NPRINT,INFO,NFEV,NJEV,
  359. C * R,LR,QTF,WA1,WA2,WA3,WA4)
  360. C FNORM = ENORM(N,FVEC)
  361. C WRITE (NWRITE,1000) FNORM,NFEV,INFO,(X(J),J=1,N)
  362. C STOP
  363. C 1000 FORMAT (5X,' FINAL L2 NORM OF THE RESIDUALS',E15.7 //
  364. C * 5X,' NUMBER OF FUNCTION EVALUATIONS',I10 //
  365. C * 5X,' EXIT PARAMETER',16X,I10 //
  366. C * 5X,' FINAL APPROXIMATE SOLUTION' // (5X,3E15.7))
  367. C END
  368. C SUBROUTINE FCN(N,X,FVEC,IFLAG)
  369. C INTEGER N,IFLAG
  370. C REAL X(N),FVEC(N)
  371. C INTEGER K
  372. C REAL ONE,TEMP,TEMP1,TEMP2,THREE,TWO,ZERO
  373. C DATA ZERO,ONE,TWO,THREE /0.E0,1.E0,2.E0,3.E0/
  374. C C
  375. C IF (IFLAG .NE. 0) GO TO 5
  376. C C
  377. C C Insert print statements here when NPRINT is positive.
  378. C C
  379. C RETURN
  380. C 5 CONTINUE
  381. C DO 10 K = 1, N
  382. C TEMP = (THREE - TWO*X(K))*X(K)
  383. C TEMP1 = ZERO
  384. C IF (K .NE. 1) TEMP1 = X(K-1)
  385. C TEMP2 = ZERO
  386. C IF (K .NE. N) TEMP2 = X(K+1)
  387. C FVEC(K) = TEMP - TEMP1 - TWO*TEMP2 + ONE
  388. C 10 CONTINUE
  389. C RETURN
  390. C END
  391. C
  392. C Results obtained with different compilers or machines
  393. C may be slightly different.
  394. C
  395. C FINAL L2 NORM OF THE RESIDUALS 0.1192636E-07
  396. C
  397. C NUMBER OF FUNCTION EVALUATIONS 14
  398. C
  399. C EXIT PARAMETER 1
  400. C
  401. C FINAL APPROXIMATE SOLUTION
  402. C
  403. C -0.5706545E+00 -0.6816283E+00 -0.7017325E+00
  404. C -0.7042129E+00 -0.7013690E+00 -0.6918656E+00
  405. C -0.6657920E+00 -0.5960342E+00 -0.4164121E+00
  406. C
  407. C***REFERENCES M. J. D. Powell, A hybrid method for nonlinear equa-
  408. C tions. In Numerical Methods for Nonlinear Algebraic
  409. C Equations, P. Rabinowitz, Editor. Gordon and Breach,
  410. C 1988.
  411. C***ROUTINES CALLED DOGLEG, ENORM, FDJAC1, QFORM, QRFAC, R1MACH,
  412. C R1MPYQ, R1UPDT, XERMSG
  413. C***REVISION HISTORY (YYMMDD)
  414. C 800301 DATE WRITTEN
  415. C 890531 Changed all specific intrinsics to generic. (WRB)
  416. C 890831 Modified array declarations. (WRB)
  417. C 890831 REVISION DATE from Version 3.2
  418. C 891214 Prologue converted to Version 4.0 format. (BAB)
  419. C 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
  420. C 920501 Reformatted the REFERENCES section. (WRB)
  421. C***END PROLOGUE SNSQ
  422. INTEGER IOPT,N,MAXFEV,ML,MU,MODE,NPRINT,INFO,NFEV,LDFJAC,LR,NJEV
  423. REAL XTOL,EPSFCN,FACTOR
  424. REAL X(*),FVEC(*),DIAG(*),FJAC(LDFJAC,*),R(LR),QTF(*),WA1(*),
  425. 1 WA2(*),WA3(*),WA4(*)
  426. EXTERNAL FCN
  427. INTEGER I,IFLAG,ITER,J,JM1,L,NCFAIL,NCSUC,NSLOW1,NSLOW2
  428. INTEGER IWA(1)
  429. LOGICAL JEVAL,SING
  430. REAL ACTRED,DELTA,EPSMCH,FNORM,FNORM1,ONE,PNORM,PRERED,P1,P5,
  431. 1 P001,P0001,RATIO,SUM,TEMP,XNORM,ZERO
  432. REAL R1MACH,ENORM
  433. SAVE ONE, P1, P5, P001, P0001, ZERO
  434. DATA ONE,P1,P5,P001,P0001,ZERO
  435. 1 /1.0E0,1.0E-1,5.0E-1,1.0E-3,1.0E-4,0.0E0/
  436. C
  437. C***FIRST EXECUTABLE STATEMENT SNSQ
  438. EPSMCH = R1MACH(4)
  439. C
  440. INFO = 0
  441. IFLAG = 0
  442. NFEV = 0
  443. NJEV = 0
  444. C
  445. C CHECK THE INPUT PARAMETERS FOR ERRORS.
  446. C
  447. IF (IOPT .LT. 1 .OR. IOPT .GT. 2 .OR.
  448. 1 N .LE. 0 .OR. XTOL .LT. ZERO .OR. MAXFEV .LE. 0
  449. 2 .OR. ML .LT. 0 .OR. MU .LT. 0 .OR. FACTOR .LE. ZERO
  450. 3 .OR. LDFJAC .LT. N .OR. LR .LT. (N*(N + 1))/2) GO TO 300
  451. IF (MODE .NE. 2) GO TO 20
  452. DO 10 J = 1, N
  453. IF (DIAG(J) .LE. ZERO) GO TO 300
  454. 10 CONTINUE
  455. 20 CONTINUE
  456. C
  457. C EVALUATE THE FUNCTION AT THE STARTING POINT
  458. C AND CALCULATE ITS NORM.
  459. C
  460. IFLAG = 1
  461. CALL FCN(N,X,FVEC,IFLAG)
  462. NFEV = 1
  463. IF (IFLAG .LT. 0) GO TO 300
  464. FNORM = ENORM(N,FVEC)
  465. C
  466. C INITIALIZE ITERATION COUNTER AND MONITORS.
  467. C
  468. ITER = 1
  469. NCSUC = 0
  470. NCFAIL = 0
  471. NSLOW1 = 0
  472. NSLOW2 = 0
  473. C
  474. C BEGINNING OF THE OUTER LOOP.
  475. C
  476. 30 CONTINUE
  477. JEVAL = .TRUE.
  478. C
  479. C CALCULATE THE JACOBIAN MATRIX.
  480. C
  481. IF (IOPT .EQ. 2) GO TO 31
  482. C
  483. C USER SUPPLIES JACOBIAN
  484. C
  485. CALL JAC(N,X,FVEC,FJAC,LDFJAC,IFLAG)
  486. NJEV = NJEV+1
  487. GO TO 32
  488. C
  489. C CODE APPROXIMATES THE JACOBIAN
  490. C
  491. 31 IFLAG = 2
  492. CALL FDJAC1(FCN,N,X,FVEC,FJAC,LDFJAC,IFLAG,ML,MU,EPSFCN,WA1,
  493. 1 WA2)
  494. NFEV = NFEV + MIN(ML+MU+1,N)
  495. C
  496. 32 IF (IFLAG .LT. 0) GO TO 300
  497. C
  498. C COMPUTE THE QR FACTORIZATION OF THE JACOBIAN.
  499. C
  500. CALL QRFAC(N,N,FJAC,LDFJAC,.FALSE.,IWA,1,WA1,WA2,WA3)
  501. C
  502. C ON THE FIRST ITERATION AND IF MODE IS 1, SCALE ACCORDING
  503. C TO THE NORMS OF THE COLUMNS OF THE INITIAL JACOBIAN.
  504. C
  505. IF (ITER .NE. 1) GO TO 70
  506. IF (MODE .EQ. 2) GO TO 50
  507. DO 40 J = 1, N
  508. DIAG(J) = WA2(J)
  509. IF (WA2(J) .EQ. ZERO) DIAG(J) = ONE
  510. 40 CONTINUE
  511. 50 CONTINUE
  512. C
  513. C ON THE FIRST ITERATION, CALCULATE THE NORM OF THE SCALED X
  514. C AND INITIALIZE THE STEP BOUND DELTA.
  515. C
  516. DO 60 J = 1, N
  517. WA3(J) = DIAG(J)*X(J)
  518. 60 CONTINUE
  519. XNORM = ENORM(N,WA3)
  520. DELTA = FACTOR*XNORM
  521. IF (DELTA .EQ. ZERO) DELTA = FACTOR
  522. 70 CONTINUE
  523. C
  524. C FORM (Q TRANSPOSE)*FVEC AND STORE IN QTF.
  525. C
  526. DO 80 I = 1, N
  527. QTF(I) = FVEC(I)
  528. 80 CONTINUE
  529. DO 120 J = 1, N
  530. IF (FJAC(J,J) .EQ. ZERO) GO TO 110
  531. SUM = ZERO
  532. DO 90 I = J, N
  533. SUM = SUM + FJAC(I,J)*QTF(I)
  534. 90 CONTINUE
  535. TEMP = -SUM/FJAC(J,J)
  536. DO 100 I = J, N
  537. QTF(I) = QTF(I) + FJAC(I,J)*TEMP
  538. 100 CONTINUE
  539. 110 CONTINUE
  540. 120 CONTINUE
  541. C
  542. C COPY THE TRIANGULAR FACTOR OF THE QR FACTORIZATION INTO R.
  543. C
  544. SING = .FALSE.
  545. DO 150 J = 1, N
  546. L = J
  547. JM1 = J - 1
  548. IF (JM1 .LT. 1) GO TO 140
  549. DO 130 I = 1, JM1
  550. R(L) = FJAC(I,J)
  551. L = L + N - I
  552. 130 CONTINUE
  553. 140 CONTINUE
  554. R(L) = WA1(J)
  555. IF (WA1(J) .EQ. ZERO) SING = .TRUE.
  556. 150 CONTINUE
  557. C
  558. C ACCUMULATE THE ORTHOGONAL FACTOR IN FJAC.
  559. C
  560. CALL QFORM(N,N,FJAC,LDFJAC,WA1)
  561. C
  562. C RESCALE IF NECESSARY.
  563. C
  564. IF (MODE .EQ. 2) GO TO 170
  565. DO 160 J = 1, N
  566. DIAG(J) = MAX(DIAG(J),WA2(J))
  567. 160 CONTINUE
  568. 170 CONTINUE
  569. C
  570. C BEGINNING OF THE INNER LOOP.
  571. C
  572. 180 CONTINUE
  573. C
  574. C IF REQUESTED, CALL FCN TO ENABLE PRINTING OF ITERATES.
  575. C
  576. IF (NPRINT .LE. 0) GO TO 190
  577. IFLAG = 0
  578. IF (MOD(ITER-1,NPRINT) .EQ. 0) CALL FCN(N,X,FVEC,IFLAG)
  579. IF (IFLAG .LT. 0) GO TO 300
  580. 190 CONTINUE
  581. C
  582. C DETERMINE THE DIRECTION P.
  583. C
  584. CALL DOGLEG(N,R,LR,DIAG,QTF,DELTA,WA1,WA2,WA3)
  585. C
  586. C STORE THE DIRECTION P AND X + P. CALCULATE THE NORM OF P.
  587. C
  588. DO 200 J = 1, N
  589. WA1(J) = -WA1(J)
  590. WA2(J) = X(J) + WA1(J)
  591. WA3(J) = DIAG(J)*WA1(J)
  592. 200 CONTINUE
  593. PNORM = ENORM(N,WA3)
  594. C
  595. C ON THE FIRST ITERATION, ADJUST THE INITIAL STEP BOUND.
  596. C
  597. IF (ITER .EQ. 1) DELTA = MIN(DELTA,PNORM)
  598. C
  599. C EVALUATE THE FUNCTION AT X + P AND CALCULATE ITS NORM.
  600. C
  601. IFLAG = 1
  602. CALL FCN(N,WA2,WA4,IFLAG)
  603. NFEV = NFEV + 1
  604. IF (IFLAG .LT. 0) GO TO 300
  605. FNORM1 = ENORM(N,WA4)
  606. C
  607. C COMPUTE THE SCALED ACTUAL REDUCTION.
  608. C
  609. ACTRED = -ONE
  610. IF (FNORM1 .LT. FNORM) ACTRED = ONE - (FNORM1/FNORM)**2
  611. C
  612. C COMPUTE THE SCALED PREDICTED REDUCTION.
  613. C
  614. L = 1
  615. DO 220 I = 1, N
  616. SUM = ZERO
  617. DO 210 J = I, N
  618. SUM = SUM + R(L)*WA1(J)
  619. L = L + 1
  620. 210 CONTINUE
  621. WA3(I) = QTF(I) + SUM
  622. 220 CONTINUE
  623. TEMP = ENORM(N,WA3)
  624. PRERED = ZERO
  625. IF (TEMP .LT. FNORM) PRERED = ONE - (TEMP/FNORM)**2
  626. C
  627. C COMPUTE THE RATIO OF THE ACTUAL TO THE PREDICTED
  628. C REDUCTION.
  629. C
  630. RATIO = ZERO
  631. IF (PRERED .GT. ZERO) RATIO = ACTRED/PRERED
  632. C
  633. C UPDATE THE STEP BOUND.
  634. C
  635. IF (RATIO .GE. P1) GO TO 230
  636. NCSUC = 0
  637. NCFAIL = NCFAIL + 1
  638. DELTA = P5*DELTA
  639. GO TO 240
  640. 230 CONTINUE
  641. NCFAIL = 0
  642. NCSUC = NCSUC + 1
  643. IF (RATIO .GE. P5 .OR. NCSUC .GT. 1)
  644. 1 DELTA = MAX(DELTA,PNORM/P5)
  645. IF (ABS(RATIO-ONE) .LE. P1) DELTA = PNORM/P5
  646. 240 CONTINUE
  647. C
  648. C TEST FOR SUCCESSFUL ITERATION.
  649. C
  650. IF (RATIO .LT. P0001) GO TO 260
  651. C
  652. C SUCCESSFUL ITERATION. UPDATE X, FVEC, AND THEIR NORMS.
  653. C
  654. DO 250 J = 1, N
  655. X(J) = WA2(J)
  656. WA2(J) = DIAG(J)*X(J)
  657. FVEC(J) = WA4(J)
  658. 250 CONTINUE
  659. XNORM = ENORM(N,WA2)
  660. FNORM = FNORM1
  661. ITER = ITER + 1
  662. 260 CONTINUE
  663. C
  664. C DETERMINE THE PROGRESS OF THE ITERATION.
  665. C
  666. NSLOW1 = NSLOW1 + 1
  667. IF (ACTRED .GE. P001) NSLOW1 = 0
  668. IF (JEVAL) NSLOW2 = NSLOW2 + 1
  669. IF (ACTRED .GE. P1) NSLOW2 = 0
  670. C
  671. C TEST FOR CONVERGENCE.
  672. C
  673. IF (DELTA .LE. XTOL*XNORM .OR. FNORM .EQ. ZERO) INFO = 1
  674. IF (INFO .NE. 0) GO TO 300
  675. C
  676. C TESTS FOR TERMINATION AND STRINGENT TOLERANCES.
  677. C
  678. IF (NFEV .GE. MAXFEV) INFO = 2
  679. IF (P1*MAX(P1*DELTA,PNORM) .LE. EPSMCH*XNORM) INFO = 3
  680. IF (NSLOW2 .EQ. 5) INFO = 4
  681. IF (NSLOW1 .EQ. 10) INFO = 5
  682. IF (INFO .NE. 0) GO TO 300
  683. C
  684. C CRITERION FOR RECALCULATING JACOBIAN
  685. C
  686. IF (NCFAIL .EQ. 2) GO TO 290
  687. C
  688. C CALCULATE THE RANK ONE MODIFICATION TO THE JACOBIAN
  689. C AND UPDATE QTF IF NECESSARY.
  690. C
  691. DO 280 J = 1, N
  692. SUM = ZERO
  693. DO 270 I = 1, N
  694. SUM = SUM + FJAC(I,J)*WA4(I)
  695. 270 CONTINUE
  696. WA2(J) = (SUM - WA3(J))/PNORM
  697. WA1(J) = DIAG(J)*((DIAG(J)*WA1(J))/PNORM)
  698. IF (RATIO .GE. P0001) QTF(J) = SUM
  699. 280 CONTINUE
  700. C
  701. C COMPUTE THE QR FACTORIZATION OF THE UPDATED JACOBIAN.
  702. C
  703. CALL R1UPDT(N,N,R,LR,WA1,WA2,WA3,SING)
  704. CALL R1MPYQ(N,N,FJAC,LDFJAC,WA2,WA3)
  705. CALL R1MPYQ(1,N,QTF,1,WA2,WA3)
  706. C
  707. C END OF THE INNER LOOP.
  708. C
  709. JEVAL = .FALSE.
  710. GO TO 180
  711. 290 CONTINUE
  712. C
  713. C END OF THE OUTER LOOP.
  714. C
  715. GO TO 30
  716. 300 CONTINUE
  717. C
  718. C TERMINATION, EITHER NORMAL OR USER IMPOSED.
  719. C
  720. IF (IFLAG .LT. 0) INFO = IFLAG
  721. IFLAG = 0
  722. IF (NPRINT .GT. 0) CALL FCN(N,X,FVEC,IFLAG)
  723. IF (INFO .LT. 0) CALL XERMSG ('SLATEC', 'SNSQ',
  724. + 'EXECUTION TERMINATED BECAUSE USER SET IFLAG NEGATIVE.', 1, 1)
  725. IF (INFO .EQ. 0) CALL XERMSG ('SLATEC', 'SNSQ',
  726. + 'INVALID INPUT PARAMETER.', 2, 1)
  727. IF (INFO .EQ. 2) CALL XERMSG ('SLATEC', 'SNSQ',
  728. + 'TOO MANY FUNCTION EVALUATIONS.', 9, 1)
  729. IF (INFO .EQ. 3) CALL XERMSG ('SLATEC', 'SNSQ',
  730. + 'XTOL TOO SMALL. NO FURTHER IMPROVEMENT POSSIBLE.', 3, 1)
  731. IF (INFO .GT. 4) CALL XERMSG ('SLATEC', 'SNSQ',
  732. + 'ITERATION NOT MAKING GOOD PROGRESS.', 1, 1)
  733. RETURN
  734. C
  735. C LAST CARD OF SUBROUTINE SNSQ.
  736. C
  737. END