The LMFIT function does a non-linear least squares fit to a function with an arbitrary number of parameters. LMFIT uses the Levenberg-Marquardt algorithm, which combines the steepest descent and inverse-Hessian function fitting methods. The function may be any non-linear function.

Iterations are performed until three consecutive iterations fail to change the chi square value by more than the specified tolerance amount, or until a maximum number of iterations have been performed. The LMFIT function returns a vector of calculated parameters, which are improvements upon the initial guesses.

The initial guess of the parameter values should be as close to the actual values as possible or the solution may not converge. Test the value of the variable specified by the CONVERGENCE keyword to determine whether the algorithm converged, failed to converge, or encountered a singular matrix.

This routine is written in the IDL language. Its source code can be found in the file ```
lmfit.pro
```

in the ```
lib
```

subdirectory of the IDL distribution. LMFIT is based on the routine ```
mrqmin
```

described in section 15.5 of *
Numerical Recipes in C: The Art of Scientific Computing*
(Second Edition), published by Cambridge University Press, and is used by permission.

Set this keyword equal to a named variable that will contain the value of the curvature matrix.

Set this keyword equal to a named variable that will indicate whether the LMFIT algorithm converged. The possible returned values are:

Set this keyword equal to a named variable that will contain the value of the covariance matrix.

Set this keyword equal to a vector, with as many elements as *
A*
, which contains a zero for each fixed parameter, and a non-zero value for elements of *
A*
to fit. If FITA is not specified, all parameters are taken to be non-fixed.

Use this keyword to specify the name of the function to fit. If this keyword is omitted, LMFIT assumes that the IDL procedure ```
LMFUNCT
```

is to be used. If ```
LMFUNCT
```

is not already compiled, IDL compiles the function from the file ```
lmfunct.pro
```

, located in the ```
lib
```

subdirectory of the IDL distribution.```
LMFUNCT
```

is designed to fit a quadratic equation.

The function to be fit must be written as an IDL procedure and compiled prior to calling LMFIT. The function must accept a vector *
X*
(the independent variables) and a vector *
A*
containing the fitted function's parameter values. It must return an *
A*
+1-element vector in which the first (zeroth) element is the evaluated function value and the remaining elements are the partial derivatives with respect to each parameter in *
A*
.

Set this keyword equal to a named variable that will contain the actual number of iterations which were performed

Set this keyword equal to a named variable that will contain a vector of standard deviations for the returned parameters.

The convergence tolerance. The routine returns when the relative decrease in chi-squared is less than TOL in an iteration. Default = 1.0 x10^{
-6}
.

Set this keyword equal to a vector of fitting weights for *
Y*
_{
i}
. This vector must be the same length as X and Y. For instrumental (Gaussian) weighting (when the measurement errors or standard deviations (s) of Y are known), set WEIGHTS to 1/(s^{
2}
). For statistical (Poisson) weighting, set WEIGHTS=1/Y. If WEIGHTS is not specified, WEIGHTS_{
i}
is assumed to be 1.0.

Suppose we wish to fit a function of the form:

f(x)=a[0] * exp(a[1]*x) + a[2] + a[3] * sin(x)

First, define a return function for LMFIT:

RETURN,[ [bx+A[2]+A[3]*SIN(X)], [EXP(A[1]*X)], [bx*X], $

Compute the fit to the function we have just defined. First, define the independent and dependent variables:

Y = 8.8 * EXP(-9.9 * X) + 11.11 + 4.9 * SIN(X)

A = [10.0, -0.1, 2.0, 4.0] *;
Provide an initial guess for the function's parameters.*

PLOTERR, X, Y, sig *;
Plot the initial data, with error bars.*

coefs = LMFIT(X, Y, A, WEIGHTS = (1/sig^2.0), FITA = fita, $