The CURVEFIT function uses a gradient-expansion algorithm to compute a non-linear least squares fit to a user-supplied function with an arbitrary number of parameters. The user-supplied function may be any non-linear function where the partial derivatives are known or can be approximated. Iterations are performed until the chi square changes by a specified amount, or until a maximum number of iterations have been performed.
This routine is written in the IDL language. Its source code can be found in the file curvefit.pro in the lib subdirectory of the IDL distribution.
Result = CURVEFIT( X, Y, Weights, A [, Sigma] [, CHISQ=variable] [, /DOUBLE] [, FITA=vector] [, FUNCTION_NAME=string] [, ITER=variable] [, ITMAX=value] [, /NODERIVATIVE] [, STATUS={0 | 1 | 2}] [, TOL=value] [, YERROR=variable] )
Returns a vector of values for the dependent variables, as fitted by the function fit. If A is double-precision or if the DOUBLE keyword is set, calculations are performed in double-precision arithmetic, otherwise they are performed in single-precision arithmetic.
An n-element vector of independent variables.
A vector of dependent variables. Y must have the same number of elements as F returned by the user-defined function.
For instrumental (Gaussian) weighting, set Weightsi = 1.0/standard_deviation(Yi)2. For statistical (Poisson) weighting, Weightsi = 1.0/Yi. For no weighting, set Weightsi = 1.0. If Weights is set to an undefined variable then no weighting will be used.
A vector with as many elements as the number of terms in the user-supplied function, containing the initial estimate for each parameter. On return, the vector A contains the fitted model parameters.
A named variable that will contain a vector of standard deviations for the elements of the output vector A.
| Note |
Set this keyword to a named variable that will contain the value of the reduced chi-square goodness-of-fit statistic.
Set this keyword to force the computation to be done in double-precision arithmetic.
Set this keyword 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 not supplied, 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, CURVEFIT assumes that the IDL procedure FUNCT is to be used. If FUNCT is not already compiled, IDL compiles the function from the file funct.pro, located in the lib subdirectory of the IDL distribution. FUNCT evaluates the sum of a Gaussian and a second-order polynomial.
The function to be fit must be written as an IDL procedure and compiled prior to calling CURVEFIT. The procedure must accept values of X (the independent variable), and A (the fitted function's initial parameter values). It must return values for F (the function's value at X), and optionally PDER (a 2D array of partial derivatives).
The return value for F must have the same number of elements as Y. The return value for PDER (if supplied) must be a 2D array with dimensions
[N_ELEMENTS(Y), N_ELEMENTS(A)].
See the Example section below for an example function.
Set this keyword equal to a named variable that will contain the actual number of iterations performed.
Set this keyword to specify the maximum number of iterations. The default value is 20.
If this keyword is set, the routine specified by the FUNCTION_NAME keyword will not be requested to provide partial derivatives. The partial derivatives will be estimated by CURVEFIT using forward differences. If analytical derivatives are available they should always be used.
Set this keyword to a named variable that will contain an integer indicating the status of the computation. Possible return values are:
|
0
|
The computation was successful.
|
|
1
|
The computation failed. Chi-square was increasing without bounds.
|
|
2
|
The computation failed to converge in ITMAX iterations.
|
Use this keyword to specify the desired convergence tolerance. The routine returns when the relative decrease in chi-squared is less than TOL in one iteration. The default value is 1.0 x 10-3.
Set this keyword to a named variable that will contain the standard error between YFIT and Y.
Fit a function of the form F(x) = a * exp(b*x) + c to sample pairs contained in arrays X and Y. The partial derivatives are easily computed symbolically:
df/da = EXP(b*x) df/db = a * x * EXP(b*x) df/dc = 1.0
First, define a procedure to return F(x) and the partial derivatives, given X. Note that A is an array containing the values a, b, and c.
PRO gfunct, X, A, F, pder bx = EXP(A[1] * X) F = A[0] * bx + A[2] ;If the procedure is called with four parameters, calculate the ;partial derivatives. IF N_PARAMS() GE 4 THEN $ pder = [[bx], [A[0] * X * bx], [replicate(1.0, N_ELEMENTS(X))]] END
Compute the fit to the function we have just defined. First, define the independent and dependent variables:
X = FLOAT(INDGEN(10)) Y = [12.0, 11.0, 10.2, 9.4, 8.7, 8.1, 7.5, 6.9, 6.5, 6.1] ;Define a vector of weights. weights = 1.0/Y ;Provide an initial guess of the function's parameters. A = [10.0,-0.1,2.0] ;Compute the parameters. yfit = CURVEFIT(X, Y, weights, A, SIGMA, FUNCTION_NAME='gfunct') ;Print the parameters returned in A. PRINT, 'Function parameters: ', A
IDL prints:
Function parameters: 9.91120 -0.100883 2.07773
Thus, the function that best fits the data is:
f (x) = 9.91120(e-0.100883x) + 2.07773
Introduced: Original
YERROR keyword added: 5.6
FITA and STATUS keywords added: 6.0
COMFIT, GAUSS2DFIT, GAUSSFIT, LMFIT, POLY_FIT, REGRESS, SFIT, SVDFIT