%N lsqfit %F $MIR/src/subs/lsqfit.for
%D Non-linear least squares fitter.
%P rjs
%: least-squares, fitting
%B
        subroutine lsqfit(FCN,m,n,x,covar,rms,ifail1,ifail2)

        implicit none
        external FCN
        integer m,n,ifail1,ifail2
        real x(n),covar(n,n),rms

  Lsqfit minimises the sum of squares of M non-linear equations in N
  unknown variables "x". The caller provides a subroutine which calculats
  the non-linear equations.

  Input:
   FCN      This is the name of the user-supplied subroutine which
            calculates the functions. fcn must be declared
            in an external statement in the user calling
            routine, and should be written as follows.

              subroutine fcn(m,n,x,fvec,iflag)
              integer m,n,iflag
              real x(n),fvec(m)
                ---------- 
                calculate the functions at x and
                return this vector in fvec.
                ----------
             return  
             end   

           the value of iflag should not be changed by fcn unless
           the user wants to terminate execution.
           In this case set iflag to a negative integer.

    n      Number of unknowns being solved for.
    m      Number of equations.
  Input/Output:
    x      On input, this should give an initial estimate of the solution
           vector. On output, this gives the final solution vector estimate.
  Output:
    covar  The covariance matrix.
    rms    The rms error at the end of the fitting process.
    ifail1 This gives the status of the fitting process. 0 indicates
           success.
    ifail2 This gives the status of the determination of the covariance
           matrix. 0 indicates success.
