1 subroutine hybrj
(fcn
,n
,x
,fvec
,fjac
,ldfjac
,xtol
,maxfev
,diag
,mode
,
2 * factor
,nprint
,info
,nfev
,njev
,r
,lr
,qtf
,wa1
,wa2
,
4 integer n
,ldfjac
,maxfev
,mode
,nprint
,info
,nfev
,njev
,lr
5 double precision xtol
,factor
6 double precision x
(n
),fvec
(n
),fjac
(ldfjac
,n
),diag
(n
),r
(lr
),
7 * qtf
(n
),wa1
(n
),wa2
(n
),wa3
(n
),wa4
(n
)
12 c the purpose of hybrj is to find a zero of a system of
13 c n nonlinear functions in n variables by a modification
14 c of the powell hybrid method. the user must provide a
15 c subroutine which calculates the functions and the jacobian.
17 c the subroutine statement is
19 c subroutine hybrj(fcn,n,x,fvec,fjac,ldfjac,xtol,maxfev,diag,
20 c mode,factor,nprint,info,nfev,njev,r,lr,qtf,
25 c fcn is the name of the user-supplied subroutine which
26 c calculates the functions and the jacobian. fcn must
27 c be declared in an external statement in the user
28 c calling program, and should be written as follows.
30 c subroutine fcn(n,x,fvec,fjac,ldfjac,iflag)
31 c integer n,ldfjac,iflag
32 c double precision x(n),fvec(n),fjac(ldfjac,n)
34 c if iflag = 1 calculate the functions at x and
35 c return this vector in fvec. do not alter fjac.
36 c if iflag = 2 calculate the jacobian at x and
37 c return this matrix in fjac. do not alter fvec.
42 c the value of iflag should not be changed by fcn unless
43 c the user wants to terminate execution of hybrj.
44 c in this case set iflag to a negative integer.
46 c n is a positive integer input variable set to the number
47 c of functions and variables.
49 c x is an array of length n. on input x must contain
50 c an initial estimate of the solution vector. on output x
51 c contains the final estimate of the solution vector.
53 c fvec is an output array of length n which contains
54 c the functions evaluated at the output x.
56 c fjac is an output n by n array which contains the
57 c orthogonal matrix q produced by the qr factorization
58 c of the final approximate jacobian.
60 c ldfjac is a positive integer input variable not less than n
61 c which specifies the leading dimension of the array fjac.
63 c xtol is a nonnegative input variable. termination
64 c occurs when the relative error between two consecutive
65 c iterates is at most xtol.
67 c maxfev is a positive integer input variable. termination
68 c occurs when the number of calls to fcn with iflag = 1
71 c diag is an array of length n. if mode = 1 (see
72 c below), diag is internally set. if mode = 2, diag
73 c must contain positive entries that serve as
74 c multiplicative scale factors for the variables.
76 c mode is an integer input variable. if mode = 1, the
77 c variables will be scaled internally. if mode = 2,
78 c the scaling is specified by the input diag. other
79 c values of mode are equivalent to mode = 1.
81 c factor is a positive input variable used in determining the
82 c initial step bound. this bound is set to the product of
83 c factor and the euclidean norm of diag*x if nonzero, or else
84 c to factor itself. in most cases factor should lie in the
85 c interval (.1,100.). 100. is a generally recommended value.
87 c nprint is an integer input variable that enables controlled
88 c printing of iterates if it is positive. in this case,
89 c fcn is called with iflag = 0 at the beginning of the first
90 c iteration and every nprint iterations thereafter and
91 c immediately prior to return, with x and fvec available
92 c for printing. fvec and fjac should not be altered.
93 c if nprint is not positive, no special calls of fcn
94 c with iflag = 0 are made.
96 c info is an integer output variable. if the user has
97 c terminated execution, info is set to the (negative)
98 c value of iflag. see description of fcn. otherwise,
99 c info is set as follows.
101 c info = 0 improper input parameters.
103 c info = 1 relative error between two consecutive iterates
106 c info = 2 number of calls to fcn with iflag = 1 has
109 c info = 3 xtol is too small. no further improvement in
110 c the approximate solution x is possible.
112 c info = 4 iteration is not making good progress, as
113 c measured by the improvement from the last
114 c five jacobian evaluations.
116 c info = 5 iteration is not making good progress, as
117 c measured by the improvement from the last
120 c nfev is an integer output variable set to the number of
121 c calls to fcn with iflag = 1.
123 c njev is an integer output variable set to the number of
124 c calls to fcn with iflag = 2.
126 c r is an output array of length lr which contains the
127 c upper triangular matrix produced by the qr factorization
128 c of the final approximate jacobian, stored rowwise.
130 c lr is a positive integer input variable not less than
133 c qtf is an output array of length n which contains
134 c the vector (q transpose)*fvec.
136 c wa1, wa2, wa3, and wa4 are work arrays of length n.
140 c user-supplied ...... fcn
142 c minpack-supplied ... dogleg,dpmpar,enorm,
143 c qform,qrfac,r1mpyq,r1updt
145 c fortran-supplied ... dabs,dmax1,dmin1,mod
147 c argonne national laboratory. minpack project. march 1980.
148 c burton s. garbow, kenneth e. hillstrom, jorge j. more
151 integer i
,iflag
,iter
,j
,jm1
,l
,ncfail
,ncsuc
,nslow1
,nslow2
154 double precision actred
,delta
,epsmch
,fnorm
,fnorm1
,one
,pnorm
,
155 * prered
,p1
,p5
,p001
,p0001
,ratio
,sum
,temp
,xnorm
,
157 double precision dpmpar
,enorm
158 data one
,p1
,p5
,p001
,p0001
,zero
159 * /1.0d0
,1.0d
-1,5.0d
-1,1.0d
-3,1.0d
-4,0.0d0
/
161 c epsmch is the machine precision.
170 c check the input parameters for errors.
172 if (n
.le
. 0 .or
. ldfjac
.lt
. n
.or
. xtol
.lt
. zero
173 * .or
. maxfev
.le
. 0 .or
. factor
.le
. zero
174 * .or
. lr
.lt
. (n*
(n
+ 1))/2) go to 300
175 if (mode
.ne
. 2) go to 20
177 if (diag
(j
) .le
. zero
) go to 300
181 c evaluate the function at the starting point
182 c and calculate its norm.
185 call fcn
(n
,x
,fvec
,fjac
,ldfjac
,iflag
)
187 if (iflag
.lt
. 0) go to 300
188 fnorm
= enorm
(n
,fvec
)
190 c initialize iteration counter and monitors.
198 c beginning of the outer loop.
203 c calculate the jacobian matrix.
206 call fcn
(n
,x
,fvec
,fjac
,ldfjac
,iflag
)
208 if (iflag
.lt
. 0) go to 300
210 c compute the qr factorization of the jacobian.
212 call qrfac
(n
,n
,fjac
,ldfjac
,.false
.,iwa
,1,wa1
,wa2
,wa3
)
214 c on the first iteration and if mode is 1, scale according
215 c to the norms of the columns of the initial jacobian.
217 if (iter
.ne
. 1) go to 70
218 if (mode
.eq
. 2) go to 50
221 if (wa2
(j
) .eq
. zero
) diag
(j
) = one
225 c on the first iteration, calculate the norm of the scaled x
226 c and initialize the step bound delta.
229 wa3
(j
) = diag
(j
)*x
(j
)
233 if (delta
.eq
. zero
) delta
= factor
236 c form (q transpose)*fvec and store in qtf.
242 if (fjac
(j
,j
) .eq
. zero
) go to 110
245 sum
= sum
+ fjac
(i
,j
)*qtf
(i
)
247 temp
= -sum
/fjac
(j
,j
)
249 qtf
(i
) = qtf
(i
) + fjac
(i
,j
)*temp
254 c copy the triangular factor of the qr factorization into r.
260 if (jm1
.lt
. 1) go to 140
267 if (wa1
(j
) .eq
. zero
) sing
= .true
.
270 c accumulate the orthogonal factor in fjac.
272 call qform
(n
,n
,fjac
,ldfjac
,wa1
)
274 c rescale if necessary.
276 if (mode
.eq
. 2) go to 170
278 diag
(j
) = dmax1
(diag
(j
),wa2
(j
))
282 c beginning of the inner loop.
286 c if requested, call fcn to enable printing of iterates.
288 if (nprint
.le
. 0) go to 190
290 if (mod
(iter
-1,nprint
) .eq
. 0)
291 * call fcn
(n
,x
,fvec
,fjac
,ldfjac
,iflag
)
292 if (iflag
.lt
. 0) go to 300
295 c determine the direction p.
297 call dogleg
(n
,r
,lr
,diag
,qtf
,delta
,wa1
,wa2
,wa3
)
299 c store the direction p and x + p. calculate the norm of p.
303 wa2
(j
) = x
(j
) + wa1
(j
)
304 wa3
(j
) = diag
(j
)*wa1
(j
)
308 c on the first iteration, adjust the initial step bound.
310 if (iter
.eq
. 1) delta
= dmin1
(delta
,pnorm
)
312 c evaluate the function at x + p and calculate its norm.
315 call fcn
(n
,wa2
,wa4
,fjac
,ldfjac
,iflag
)
317 if (iflag
.lt
. 0) go to 300
318 fnorm1
= enorm
(n
,wa4
)
320 c compute the scaled actual reduction.
323 if (fnorm1
.lt
. fnorm
) actred
= one
- (fnorm1
/fnorm
)**2
325 c compute the scaled predicted reduction.
331 sum
= sum
+ r
(l
)*wa1
(j
)
334 wa3
(i
) = qtf
(i
) + sum
338 if (temp
.lt
. fnorm
) prered
= one
- (temp
/fnorm
)**2
340 c compute the ratio of the actual to the predicted
344 if (prered
.gt
. zero
) ratio
= actred
/prered
346 c update the step bound.
348 if (ratio
.ge
. p1
) go to 230
356 if (ratio
.ge
. p5
.or
. ncsuc
.gt
. 1)
357 * delta
= dmax1
(delta
,pnorm
/p5
)
358 if (dabs
(ratio
-one
) .le
. p1
) delta
= pnorm
/p5
361 c test for successful iteration.
363 if (ratio
.lt
. p0001
) go to 260
365 c successful iteration. update x, fvec, and their norms.
369 wa2
(j
) = diag
(j
)*x
(j
)
377 c determine the progress of the iteration.
380 if (actred
.ge
. p001
) nslow1
= 0
381 if (jeval
) nslow2
= nslow2
+ 1
382 if (actred
.ge
. p1
) nslow2
= 0
384 c test for convergence.
386 if (delta
.le
. xtol*xnorm
.or
. fnorm
.eq
. zero
) info
= 1
387 if (info
.ne
. 0) go to 300
389 c tests for termination and stringent tolerances.
391 if (nfev
.ge
. maxfev
) info
= 2
392 if (p1*dmax1
(p1*delta
,pnorm
) .le
. epsmch*xnorm
) info
= 3
393 if (nslow2
.eq
. 5) info
= 4
394 if (nslow1
.eq
. 10) info
= 5
395 if (info
.ne
. 0) go to 300
397 c criterion for recalculating jacobian.
399 if (ncfail
.eq
. 2) go to 290
401 c calculate the rank one modification to the jacobian
402 c and update qtf if necessary.
407 sum
= sum
+ fjac
(i
,j
)*wa4
(i
)
409 wa2
(j
) = (sum
- wa3
(j
))/pnorm
410 wa1
(j
) = diag
(j
)*((diag
(j
)*wa1
(j
))/pnorm
)
411 if (ratio
.ge
. p0001
) qtf
(j
) = sum
414 c compute the qr factorization of the updated jacobian.
416 call r1updt
(n
,n
,r
,lr
,wa1
,wa2
,wa3
,sing
)
417 call r1mpyq
(n
,n
,fjac
,ldfjac
,wa2
,wa3
)
418 call r1mpyq
(1,n
,qtf
,1,wa2
,wa3
)
420 c end of the inner loop.
426 c end of the outer loop.
431 c termination, either normal or user imposed.
433 if (iflag
.lt
. 0) info
= iflag
435 if (nprint
.gt
. 0) call fcn
(n
,x
,fvec
,fjac
,ldfjac
,iflag
)
438 c last card of subroutine hybrj.