1      recursive
2     *subroutine hybrd1(fcn,n,x,fvec,tol,info,wa,lwa)
3      integer n,info,lwa
4      double precision tol
5      double precision x(n),fvec(n),wa(lwa)
6      external fcn
7c     **********
8c
9c     subroutine hybrd1
10c
11c     the purpose of hybrd1 is to find a zero of a system of
12c     n nonlinear functions in n variables by a modification
13c     of the powell hybrid method. this is done by using the
14c     more general nonlinear equation solver hybrd. the user
15c     must provide a subroutine which calculates the functions.
16c     the jacobian is then calculated by a forward-difference
17c     approximation.
18c
19c     the subroutine statement is
20c
21c       subroutine hybrd1(fcn,n,x,fvec,tol,info,wa,lwa)
22c
23c     where
24c
25c       fcn is the name of the user-supplied subroutine which
26c         calculates the functions. fcn must be declared
27c         in an external statement in the user calling
28c         program, and should be written as follows.
29c
30c         subroutine fcn(n,x,fvec,iflag)
31c         integer n,iflag
32c         double precision x(n),fvec(n)
33c         ----------
34c         calculate the functions at x and
35c         return this vector in fvec.
36c         ---------
37c         return
38c         end
39c
40c         the value of iflag should not be changed by fcn unless
41c         the user wants to terminate execution of hybrd1.
42c         in this case set iflag to a negative integer.
43c
44c       n is a positive integer input variable set to the number
45c         of functions and variables.
46c
47c       x is an array of length n. on input x must contain
48c         an initial estimate of the solution vector. on output x
49c         contains the final estimate of the solution vector.
50c
51c       fvec is an output array of length n which contains
52c         the functions evaluated at the output x.
53c
54c       tol is a nonnegative input variable. termination occurs
55c         when the algorithm estimates that the relative error
56c         between x and the solution is at most tol.
57c
58c       info is an integer output variable. if the user has
59c         terminated execution, info is set to the (negative)
60c         value of iflag. see description of fcn. otherwise,
61c         info is set as follows.
62c
63c         info = 0   improper input parameters.
64c
65c         info = 1   algorithm estimates that the relative error
66c                    between x and the solution is at most tol.
67c
68c         info = 2   number of calls to fcn has reached or exceeded
69c                    200*(n+1).
70c
71c         info = 3   tol is too small. no further improvement in
72c                    the approximate solution x is possible.
73c
74c         info = 4   iteration is not making good progress.
75c
76c       wa is a work array of length lwa.
77c
78c       lwa is a positive integer input variable not less than
79c         (n*(3*n+13))/2.
80c
81c     subprograms called
82c
83c       user-supplied ...... fcn
84c
85c       minpack-supplied ... hybrd
86c
87c     argonne national laboratory. minpack project. march 1980.
88c     burton s. garbow, kenneth e. hillstrom, jorge j. more
89c
90c     **********
91      integer index,j,lr,maxfev,ml,mode,mu,nfev,nprint
92      double precision epsfcn,factor,one,xtol,zero
93      data factor,one,zero /1.0d2,1.0d0,0.0d0/
94      info = 0
95c
96c     check the input parameters for errors.
97c
98      if (n .le. 0 .or. tol .lt. zero .or. lwa .lt. (n*(3*n + 13))/2)
99     *   go to 20
100c
101c     call hybrd.
102c
103      maxfev = 200*(n + 1)
104      xtol = tol
105      ml = n - 1
106      mu = n - 1
107      epsfcn = zero
108      mode = 2
109      do 10 j = 1, n
110         wa(j) = one
111   10    continue
112      nprint = 0
113      lr = (n*(n + 1))/2
114      index = 6*n + lr
115      call hybrd(fcn,n,x,fvec,xtol,maxfev,ml,mu,epsfcn,wa(1),mode,
116     *           factor,nprint,info,nfev,wa(index+1),n,wa(6*n+1),lr,
117     *           wa(n+1),wa(2*n+1),wa(3*n+1),wa(4*n+1),wa(5*n+1))
118      if (info .eq. 5) info = 4
119   20 continue
120      return
121c
122c     last card of subroutine hybrd1.
123c
124      end
125