```
subroutine lmdif1(fcn,m,n,x,fvec,tol,info,iwa,wa,lwa)
C-----------------------------------------------------------------------
c     subroutine lmdif1
c
c     PURPOSE: minimize the sum of the squares of m nonlinear
c     functions in n variables by a modification of the
c     levenberg-marquardt algorithm.
c
c     this is done by using the more general least-squares solver
c     lmdif. the user must provide a subroutine which calculates
c     the functions. the jacobian is then calculated by a
c     forward-difference approximation.
c
c     the subroutine statement is
c
c       subroutine lmdif1(fcn,m,n,x,fvec,tol,info,iwa,wa,lwa)
c
c     where
c
c       fcn is the name of the user-supplied subroutine which
c         calculates the functions. fcn must be declared
c         in an external statement in the user calling
c         program, and should be written as follows.
c
c         subroutine fcn(m,n,x,fvec,iflag)
c         integer m,n,iflag
c         double precision x(n),fvec(m)
c         ----------
c         calculate the functions at x and
c         return this vector in fvec.
c         ----------
c         return
c         end
c
c         the value of iflag should not be changed by fcn unless
c         the user wants to terminate execution of lmdif1.
c         in this case set iflag to a negative integer.
c
c       m is a positive integer input variable set to the number
c         of functions.
c
c       n is a positive integer input variable set to the number
c         of variables. n must not exceed m.
c
c       x is an array of length n. on input x must contain
c         an initial estimate of the solution vector. on output x
c         contains the final estimate of the solution vector.
c
c       fvec is an output array of length m which contains
c         the functions evaluated at the output x.
c
c       tol is a nonnegative input variable. termination occurs
c         when the algorithm estimates either that the relative
c         error in the sum of squares is at most tol or that
c         the relative error between x and the solution is at
c         most tol.
c
c       info is an integer output variable. if the user has
c         terminated execution, info is set to the (negative)
c         value of iflag. see description of fcn. otherwise,
c         info is set as follows.
c
c         info = 0  improper input parameters.
c
c         info = 1  algorithm estimates that the relative error
c                   in the sum of squares is at most tol.
c
c         info = 2  algorithm estimates that the relative error
c                   between x and the solution is at most tol.
c
c         info = 3  conditions for info = 1 and info = 2 both hold.
c
c         info = 4  fvec is orthogonal to the columns of the
c                   jacobian to machine precision.
c
c         info = 5  number of calls to fcn has reached or
c                   exceeded 200*(n+1).
c
c         info = 6  tol is too small. no further reduction in
c                   the sum of squares is possible.
c
c         info = 7  tol is too small. no further improvement in
c                   the approximate solution x is possible.
c
c       iwa is an integer work array of length n.
c
c       wa is a work array of length lwa.
c
c       lwa is a positive integer input variable not less than
c         m*n+5*n+m.
c
c     subprograms called
c
c       user-supplied ...... fcn
c
c       minpack-supplied ... lmdif
c
c     argonne national laboratory. minpack project. march 1980.
c     burton s. garbow, kenneth e. hillstrom, jorge j. more
c
C	   WILLIAM OSBORN, TESSELLA SUPPORT SERVICES PLC.
C
C DATE:    25TH APRIL 1996
C
C VERSION: 1.1				DATE: 25-04-96
C MODIFIED: WILLIAM OSBORN
C	    - FOUND AT WWW.NETLIB.ORG/MINPACK/ .
C             REPLACES NAG ROUTINE E04FDF .
C
C VERSION: 1.2				DATE: 21-05-96
C MODIFIED: WILLIAM OSBORN
C           - CHANGED TOLERANCE CALCULATION IN ORDER TO USE SMALLER
C             VALUES
C
C VERSION: 1.3				DATE: 22-09-99
C MODIFIED: RICHARD MARTIN
C 		- RENAMED FROM lmdif1_all.f to lmdif1_all.for
C
C VERSION: 1.4				DATE: 16-05-07
C MODIFIED: Allan Whiteford
C	    - Updated comments as part of subroutine documentation
C             procedure.
C
C-------------------------------------------------------------------
DOUBLE PRECISION    FVEC(M),     TOL,         WA(LWA),     X(N)
INTEGER             INFO,        IWA(N),      LWA,         M
INTEGER             N
DOUBLE PRECISION    X(N)
INTEGER             N
DOUBLE PRECISION    EPSFCN,      FJAC(LDFJAC,N),           FVEC(M)
DOUBLE PRECISION    WA(M),       X(N)
INTEGER             IFLAG,       LDFJAC,      M,           N
DOUBLE PRECISION    DIAG(N),     EPSFCN,      FACTOR
DOUBLE PRECISION    FJAC(LDFJAC,N),           FTOL,        FVEC(M)
DOUBLE PRECISION    GTOL,        QTF(N),      WA1(N),      WA2(N)
DOUBLE PRECISION    WA3(N),      WA4(M),      X(N),        XTOL
INTEGER             INFO,        IPVT(N),     LDFJAC,      M
INTEGER             MAXFEV,      MODE,        N,           NFEV
INTEGER             NPRINT
DOUBLE PRECISION    DELTA,       DIAG(N),     PAR,         QTB(N)
DOUBLE PRECISION    R(LDR,N),    SDIAG(N),    WA1(N),      WA2(N)
DOUBLE PRECISION    X(N)
INTEGER             IPVT(N),     LDR,         N
DOUBLE PRECISION    A(LDA,N),    ACNORM(N),   RDIAG(N),    WA(N)
INTEGER             IPVT(LIPVT), LDA,         LIPVT,       M
INTEGER             N
LOGICAL             PIVOT
DOUBLE PRECISION    DIAG(N),     QTB(N),      R(LDR,N)
DOUBLE PRECISION    SDIAG(N),    WA(N),       X(N)
INTEGER             IPVT(N),     LDR,         N
```