scov.f 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. *DECK SCOV
  2. SUBROUTINE SCOV (FCN, IOPT, M, N, X, FVEC, R, LDR, INFO, WA1, WA2,
  3. + WA3, WA4)
  4. C***BEGIN PROLOGUE SCOV
  5. C***PURPOSE Calculate the covariance matrix for a nonlinear data
  6. C fitting problem. It is intended to be used after a
  7. C successful return from either SNLS1 or SNLS1E.
  8. C***LIBRARY SLATEC
  9. C***CATEGORY K1B1
  10. C***TYPE SINGLE PRECISION (SCOV-S, DCOV-D)
  11. C***KEYWORDS COVARIANCE MATRIX, NONLINEAR DATA FITTING,
  12. C NONLINEAR LEAST SQUARES
  13. C***AUTHOR Hiebert, K. L., (SNLA)
  14. C***DESCRIPTION
  15. C
  16. C 1. Purpose.
  17. C
  18. C SCOV calculates the covariance matrix for a nonlinear data
  19. C fitting problem. It is intended to be used after a
  20. C successful return from either SNLS1 or SNLS1E. SCOV
  21. C and SNLS1 (and SNLS1E) have compatible parameters. The
  22. C required external subroutine, FCN, is the same
  23. C for all three codes, SCOV, SNLS1, and SNLS1E.
  24. C
  25. C 2. Subroutine and Type Statements.
  26. C
  27. C SUBROUTINE SCOV(FCN,IOPT,M,N,X,FVEC,R,LDR,INFO,
  28. C WA1,WA2,WA3,WA4)
  29. C INTEGER IOPT,M,N,LDR,INFO
  30. C REAL X(N),FVEC(M),R(LDR,N),WA1(N),WA2(N),WA3(N),WA4(M)
  31. C EXTERNAL FCN
  32. C
  33. C 3. Parameters.
  34. C
  35. C FCN is the name of the user-supplied subroutine which calculates
  36. C the functions. If the user wants to supply the Jacobian
  37. C (IOPT=2 or 3), then FCN must be written to calculate the
  38. C Jacobian, as well as the functions. See the explanation
  39. C of the IOPT argument below. FCN must be declared in an
  40. C EXTERNAL statement in the calling program and should be
  41. C written as follows.
  42. C
  43. C SUBROUTINE FCN(IFLAG,M,N,X,FVEC,FJAC,LDFJAC)
  44. C INTEGER IFLAG,LDFJAC,M,N
  45. C REAL X(N),FVEC(M)
  46. C ----------
  47. C FJAC and LDFJAC may be ignored , if IOPT=1.
  48. C REAL FJAC(LDFJAC,N) , if IOPT=2.
  49. C REAL FJAC(N) , if IOPT=3.
  50. C ----------
  51. C IFLAG will never be zero when FCN is called by SCOV.
  52. C RETURN
  53. C ----------
  54. C If IFLAG=1, calculate the functions at X and return
  55. C this vector in FVEC.
  56. C RETURN
  57. C ----------
  58. C If IFLAG=2, calculate the full Jacobian at X and return
  59. C this matrix in FJAC. Note that IFLAG will never be 2 unless
  60. C IOPT=2. FVEC contains the function values at X and must
  61. C not be altered. FJAC(I,J) must be set to the derivative
  62. C of FVEC(I) with respect to X(J).
  63. C RETURN
  64. C ----------
  65. C If IFLAG=3, calculate the LDFJAC-th row of the Jacobian
  66. C and return this vector in FJAC. Note that IFLAG will
  67. C never be 3 unless IOPT=3. FJAC(J) must be set to
  68. C the derivative of FVEC(LDFJAC) with respect to X(J).
  69. C RETURN
  70. C ----------
  71. C END
  72. C
  73. C
  74. C The value of IFLAG should not be changed by FCN unless the
  75. C user wants to terminate execution of SCOV. In this case, set
  76. C IFLAG to a negative integer.
  77. C
  78. C
  79. C IOPT is an input variable which specifies how the Jacobian will
  80. C be calculated. If IOPT=2 or 3, then the user must supply the
  81. C Jacobian, as well as the function values, through the
  82. C subroutine FCN. If IOPT=2, the user supplies the full
  83. C Jacobian with one call to FCN. If IOPT=3, the user supplies
  84. C one row of the Jacobian with each call. (In this manner,
  85. C storage can be saved because the full Jacobian is not stored.)
  86. C If IOPT=1, the code will approximate the Jacobian by forward
  87. C differencing.
  88. C
  89. C M is a positive integer input variable set to the number of
  90. C functions.
  91. C
  92. C N is a positive integer input variable set to the number of
  93. C variables. N must not exceed M.
  94. C
  95. C X is an array of length N. On input X must contain the value
  96. C at which the covariance matrix is to be evaluated. This is
  97. C usually the value for X returned from a successful run of
  98. C SNLS1 (or SNLS1E). The value of X will not be changed.
  99. C
  100. C FVEC is an output array of length M which contains the functions
  101. C evaluated at X.
  102. C
  103. C R is an output array. For IOPT=1 and 2, R is an M by N array.
  104. C For IOPT=3, R is an N by N array. On output, if INFO=1,
  105. C the upper N by N submatrix of R contains the covariance
  106. C matrix evaluated at X.
  107. C
  108. C LDR is a positive integer input variable which specifies
  109. C the leading dimension of the array R. For IOPT=1 and 2,
  110. C LDR must not be less than M. For IOPT=3, LDR must not
  111. C be less than N.
  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. Otherwise, INFO is set as follows.
  116. C
  117. C INFO = 0 Improper input parameters (M.LE.0 or N.LE.0).
  118. C
  119. C INFO = 1 Successful return. The covariance matrix has been
  120. C calculated and stored in the upper N by N
  121. C submatrix of R.
  122. C
  123. C INFO = 2 The Jacobian matrix is singular for the input value
  124. C of X. The covariance matrix cannot be calculated.
  125. C The upper N by N submatrix of R contains the QR
  126. C factorization of the Jacobian (probably not of
  127. C interest to the user).
  128. C
  129. C WA1 is a work array of length N.
  130. C WA2 is a work array of length N.
  131. C WA3 is a work array of length N.
  132. C WA4 is a work array of length M.
  133. C
  134. C***REFERENCES (NONE)
  135. C***ROUTINES CALLED ENORM, FDJAC3, QRFAC, RWUPDT, XERMSG
  136. C***REVISION HISTORY (YYMMDD)
  137. C 810522 DATE WRITTEN
  138. C 890505 REVISION DATE from Version 3.2
  139. C 891214 Prologue converted to Version 4.0 format. (BAB)
  140. C 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
  141. C 900510 Fixed an error message. (RWC)
  142. C***END PROLOGUE SCOV
  143. C
  144. C REVISED 820707-1100
  145. C REVISED YYMMDD HHMM
  146. C
  147. INTEGER I,IDUM,IFLAG,INFO,IOPT,J,K,KP1,LDR,M,N,NM1,NROW
  148. REAL X(*),R(LDR,*),FVEC(*),WA1(*),WA2(*),WA3(*),WA4(*)
  149. EXTERNAL FCN
  150. REAL ONE,SIGMA,TEMP,ZERO
  151. LOGICAL SING
  152. SAVE ZERO, ONE
  153. DATA ZERO/0.E0/,ONE/1.E0/
  154. C***FIRST EXECUTABLE STATEMENT SCOV
  155. SING=.FALSE.
  156. IFLAG=0
  157. IF (M.LE.0 .OR. N.LE.0) GO TO 300
  158. C
  159. C CALCULATE SIGMA = (SUM OF THE SQUARED RESIDUALS) / (M-N)
  160. IFLAG=1
  161. CALL FCN(IFLAG,M,N,X,FVEC,R,LDR)
  162. IF (IFLAG.LT.0) GO TO 300
  163. TEMP=ENORM(M,FVEC)
  164. SIGMA=ONE
  165. IF (M.NE.N) SIGMA=TEMP*TEMP/(M-N)
  166. C
  167. C CALCULATE THE JACOBIAN
  168. IF (IOPT.EQ.3) GO TO 200
  169. C
  170. C STORE THE FULL JACOBIAN USING M*N STORAGE
  171. IF (IOPT.EQ.1) GO TO 100
  172. C
  173. C USER SUPPLIES THE JACOBIAN
  174. IFLAG=2
  175. CALL FCN(IFLAG,M,N,X,FVEC,R,LDR)
  176. GO TO 110
  177. C
  178. C CODE APPROXIMATES THE JACOBIAN
  179. 100 CALL FDJAC3(FCN,M,N,X,FVEC,R,LDR,IFLAG,ZERO,WA4)
  180. 110 IF (IFLAG.LT.0) GO TO 300
  181. C
  182. C COMPUTE THE QR DECOMPOSITION
  183. CALL QRFAC(M,N,R,LDR,.FALSE.,IDUM,1,WA1,WA1,WA1)
  184. DO 120 I=1,N
  185. 120 R(I,I)=WA1(I)
  186. GO TO 225
  187. C
  188. C COMPUTE THE QR FACTORIZATION OF THE JACOBIAN MATRIX CALCULATED ONE
  189. C ROW AT A TIME AND STORED IN THE UPPER TRIANGLE OF R.
  190. C ( (Q TRANSPOSE)*FVEC IS ALSO CALCULATED BUT NOT USED.)
  191. 200 CONTINUE
  192. DO 210 J=1,N
  193. WA2(J)=ZERO
  194. DO 205 I=1,N
  195. R(I,J)=ZERO
  196. 205 CONTINUE
  197. 210 CONTINUE
  198. IFLAG=3
  199. DO 220 I=1,M
  200. NROW = I
  201. CALL FCN(IFLAG,M,N,X,FVEC,WA1,NROW)
  202. IF (IFLAG.LT.0) GO TO 300
  203. TEMP=FVEC(I)
  204. CALL RWUPDT(N,R,LDR,WA1,WA2,TEMP,WA3,WA4)
  205. 220 CONTINUE
  206. C
  207. C CHECK IF R IS SINGULAR.
  208. 225 CONTINUE
  209. DO 230 I=1,N
  210. IF (R(I,I).EQ.ZERO) SING=.TRUE.
  211. 230 CONTINUE
  212. IF (SING) GO TO 300
  213. C
  214. C R IS UPPER TRIANGULAR. CALCULATE (R TRANSPOSE) INVERSE AND STORE
  215. C IN THE UPPER TRIANGLE OF R.
  216. IF (N.EQ.1) GO TO 275
  217. NM1=N-1
  218. DO 270 K=1,NM1
  219. C
  220. C INITIALIZE THE RIGHT-HAND SIDE (WA1(*)) AS THE K-TH COLUMN OF THE
  221. C IDENTITY MATRIX.
  222. DO 240 I=1,N
  223. WA1(I)=ZERO
  224. 240 CONTINUE
  225. WA1(K)=ONE
  226. C
  227. R(K,K)=WA1(K)/R(K,K)
  228. KP1=K+1
  229. DO 260 I=KP1,N
  230. C
  231. C SUBTRACT R(K,I-1)*R(I-1,*) FROM THE RIGHT-HAND SIDE, WA1(*).
  232. DO 250 J=I,N
  233. WA1(J)=WA1(J)-R(K,I-1)*R(I-1,J)
  234. 250 CONTINUE
  235. R(K,I)=WA1(I)/R(I,I)
  236. 260 CONTINUE
  237. 270 CONTINUE
  238. 275 R(N,N)=ONE/R(N,N)
  239. C
  240. C CALCULATE R-INVERSE * (R TRANSPOSE) INVERSE AND STORE IN THE UPPER
  241. C TRIANGLE OF R.
  242. DO 290 I=1,N
  243. DO 290 J=I,N
  244. TEMP=ZERO
  245. DO 280 K=J,N
  246. TEMP=TEMP+R(I,K)*R(J,K)
  247. 280 CONTINUE
  248. R(I,J)=TEMP*SIGMA
  249. 290 CONTINUE
  250. INFO=1
  251. C
  252. 300 CONTINUE
  253. IF (M.LE.0 .OR. N.LE.0) INFO=0
  254. IF (IFLAG.LT.0) INFO=IFLAG
  255. IF (SING) INFO=2
  256. IF (INFO .LT. 0) CALL XERMSG ('SLATEC', 'SCOV',
  257. + 'EXECUTION TERMINATED BECAUSE USER SET IFLAG NEGATIVE.', 1, 1)
  258. IF (INFO .EQ. 0) CALL XERMSG ('SLATEC', 'SCOV',
  259. + 'INVALID INPUT PARAMETER.', 2, 1)
  260. IF (INFO .EQ. 2) CALL XERMSG ('SLATEC', 'SCOV',
  261. + 'SINGULAR JACOBIAN MATRIX, COVARIANCE MATRIX CANNOT BE ' //
  262. + 'CALCULATED.', 1, 1)
  263. RETURN
  264. END