The SVDFIT function performs a least squares fit with optional error estimates. Either a user-supplied function written in the IDL language or a built-in polynomial can be used to fit the data.
SVDFIT is based on the routine
svdfit described in section 15.4 of Numerical Recipes in C: The Art of Scientific Computing (Second Edition), published by Cambridge University Press, and is used by permission.
Result = SVDFIT( X, Y [, M] [, A=vector] [, CHISQ=variable] [, COVAR=variable] [, /DOUBLE] [, FUNCTION_NAME=string] [, /LEGENDRE] [, MEASURE_ERRORS=vector] [, SIGMA=variable] [, SING_VALUES=variable] [, SINGULAR=variable] [, STATUS=variable] [, TOL=value] [, VARIANCE=variable] [, YFIT=variable] )
Returns a vector of coefficients.
An n-element vector of independent variables.
A vector of dependent variables, the same length as X.
The number of coefficients in the fitting function. For polynomials, M is equal to the degree of the polynomial + 1. If the M argument is not specified, you must supply initial coefficient estimates using the A keyword. In this case, M is set equal to the number of elements of the array specified by the A keyword.
This keyword is both an input and output keyword. Set this keyword equal to a variable containing a vector of initial estimates for the fitted function parameters. On exit, SVDFIT returns in this variable a vector of coefficients that are improvements of the initial estimates. If A is supplied, the M argument will be set equal to the number of elements in the vector specified by A.
Set this keyword equal to a named variable that will contain the value of the unreduced chi-square goodness-of-fit.
Set this keyword equal to a named variable that will contain the covariance matrix of the fitted coefficients.
Set this keyword to force the computation to be done in double-precision arithmetic.
Set this keyword equal to a string containing the name of a user-supplied IDL basis function with M coefficients. If this keyword is omitted, and the LEGENDRE keyword is not set, IDL assumes that the IDL procedure SVDFUNCT, found in the file
svdfunct.pro, located in the
lib subdirectory of the IDL distribution, is to be used. SVDFUNCT uses the basis functions for the fitting polynomial:
The function to be fit must be written as an IDL function and compiled prior to calling SVDFIT. The function must accept values of X (a scalar), and M (a scalar). It must return an M-element vector containing the basis functions.
See the Examples section below for an example function.
Set this keyword to use Legendre polynomials instead of the function specified by the FUNCTION_NAME keyword. If the LEGENDRE keyword is set, the IDL uses the function SVDLEG found in the file
svdleg.pro, located in the
lib subdirectory of the IDL distribution.
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 a named variable in which to return the singular values from the SVD. Singular values which have been removed will be set to zero.
Set this keyword equal to a named variable that will contain the number of singular values returned. This value should be 0. If not, the basis functions do not accurately characterize the data.
Set this keyword to a named variable that will contain the status of the computation. Possible values are:
Set this keyword to the tolerance used when removing singular values. The default is 10-5 for single precision, and 2x10-12 for double precision (these defaults are approximately 100 and 10000 times the machine precisions for single and double precision, respectively).
Setting TOL to a larger value may remove coefficients that do not contribute to the solution, which may reduce the errors on the remaining coefficients.
Set this keyword equal to a named variable that will contain the variance (sigma squared) of each coefficient M.
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, 1/WEIGHTS[i] represents the measurement error for each point Y[i]. Using the MEASURE_ERRORS keyword, the measurement error is represented as simply MEASURE_ERRORS[i].
Set this keyword equal to a named variable that will contain the vector of calculated Y values.
This example fits a function of the following form:
First, create the function in IDL, then create a procedure to perform the fit. Create the following file called
PRO example_svdfit ; Provide an array of coefficients: C = [7.77, 8.88, -9.99] X = FINDGEN(100)/15.0 + 0.1 Y = C + C * SIN(2*X)/X + C * COS(4.*X)^2. ; Set uncertainties to 5%: measure_errors = 0.05 * Y ; Provide an initial guess: A=[1,1,1] result_a = SVDFIT(X, Y, A=A, MEASURE_ERRORS=measure_errors, $ FUNCTION_NAME='myfunct', SIGMA=SIGMA, YFIT=YFIT) ; Plot the results: PLOT, X, YFIT FOR I = 0, N_ELEMENTS(A)-1 DO $ PRINT, I, result_a[I], SIGMA[I], C[I],$ FORMAT = $ '(" result_a ( ",I1," ) = ",F7.4," +- ",F7.4," VS. ",F7.4)' END FUNCTION myfunct, X ,M RETURN,[ [1.0], [SIN(2*X)/X], [COS(4.*X)^2.] ] END
Place the file
example_svdfit.pro in a directory in the IDL search path, and enter
example_svdfit at the command prompt to create the plot.
In addition to creating the above plot, IDL prints:
result_a ( 0 ) = 7.7700 +- 0.0390 VS. 7.7700 result_a ( 1 ) = 8.8800 +- 0.0468 VS. 8.8800 result_a ( 2 ) = -9.9900 +- 0.0506 VS. -9.9900
SING_VALUES, STATUS, and TOL keywords added: 5.6
CURVEFIT, GAUSSFIT, LINFIT, LMFIT, POLY_FIT, REGRESS, SFIT