## LMFIT

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.

### Calling Sequence

Result = LMFIT( X , Y , A )

### Arguments

#### X

A row vector of independent variables. LMFIT does not manipulate or use values in X , it simply passes X to the user-written function.

#### Y

A row vector containing the dependent variables.

#### A

A vector that contains the initial estimate for each parameter.

### Keywords

#### ALPHA

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

#### CHISQ

Set this keyword equal to a named variable that will contain the value of chi-squared.

#### CONVERGENCE

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

•  1  The algorithm converged.
•  0  The algorithm did not converge.
• -1  The algorithm encountered a singular matrix and did not converge.

#### COVAR

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

#### DOUBLE

Set this keyword to force the computations to be performed in double precision.

#### FITA

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.

#### FUNCTION_NAME

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 .

NOTE: The returned value must be of the same data type as the input X value.

#### ITER

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

#### ITMAX

Set this keyword equal to the maximum number of iterations. The default is 20.

#### SIGMA

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

#### TOL

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 .

#### WEIGHTS

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.

#### YFIT

Set this keyword equal to a named variable that will contain the fitted function evaluated at the input X values.

### Example

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:

FUNCTION myfunct, X, A

bx = A[0]*EXP(A[1]*X)

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

[1.0] ,[SIN(X)] ]

END

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

X = FINDGEN(40)/20.0

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

sig = 0.05 * Y

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

fita = [1,1,1,1]

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

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

FUNCTION_NAME = 'myfunct')

OPLOT, X, coefs ; Overplot the fitted data.