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 values for the dependent variables, as fitted by the function fit.
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.
Result = LMFIT( X, Y, A [, ALPHA=variable] [, CHISQ=variable] [, CONVERGENCE=variable] [, COVAR=variable] [, /DOUBLE] [, FITA=vector] [, FUNCTION_NAME=string] [, ITER=variable] [, ITMAX=value] [, ITMIN=value] [, MEASURE_ERRORS=vector] [, SIGMA=variable] [, TOL=value] )
Returns a vector of values for the dependent variable, resulting from the function fit.
A row vector of independent variables. LMFIT does not manipulate or use values in X, it simply passes X to the user-written function.
A row vector containing the dependent variables.
A vector that contains the initial estimate for each coefficient. Upon return, A will contain the final estimates for the coefficients.
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 contain the value of the unreduced chi-square goodness-of-fit statistic.
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 to force the computations to be performed in double precision.
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 routine 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 function 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 N_ELEMENTS(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 the maximum number of iterations. The default is 50.
Set this keyword equal to the minimum number of iterations. The default is 5.
Set this keyword to a vector containing standard measurement errors for each point Y[i]. This vector must be the same length as X and Y.
Set this keyword to a named variable that will contain the 1-sigma uncertainty estimates for the returned parameters
Set this keyword to the convergence tolerance. The routine returns when the relative decrease in chi-squared is less than TOL in an iteration. The default is 1.0 x 10-6 for single-precision, and 1.0 x 10-12 for double-precision.
The WEIGHTS keyword is obsolete and has been replaced by the MEASURE_ERRORS keyword. Code that uses the WEIGHTS keyword will continue to work as before, but new code should use the MEASURE_ERRORS keyword. Note that the definition of the MEASURE_ERRORS keyword is not the same as the WEIGHTS keyword. Using the WEIGHTS keyword, SQRT(1/WEIGHTS[i]) represents the measurement error for each point Y[i]. Using the MEASURE_ERRORS keyword, the measurement error for each point is represented as simply MEASURE_ERRORS[i].
In this example, we fit a function of the form:
f(x)=a * exp(a*x) + a + a * sin(x)
; First, define a return function for LMFIT: FUNCTION myfunct, X, A bx = A*EXP(A*X) RETURN,[ [bx+A+A*SIN(X)], [EXP(A*X)], [bx*X], $ [1.0] ,[SIN(X)] ] END PRO lmfit_example ; 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) measure_errors = 0.05 * Y ; Provide an initial guess for the function's parameters: A = [10.0, -0.1, 2.0, 4.0] fita = [1,1,1,1] ; Plot the initial data, with error bars: PLOTERR, X, Y, measure_errors coefs = LMFIT(X, Y, A, MEASURE_ERRORS=measure_errors, /DOUBLE, $ FITA = fita, FUNCTION_NAME = 'myfunct') ; Overplot the fitted data: OPLOT, X, coefs END
CURVEFIT, GAUSSFIT, LINFIT, POLY_FIT, REGRESS, SFIT, SVDFIT