SVDFIT

The SVDFIT function performs a general least squares fit with optional error estimates and returns a vector of coefficients. 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.

Calling Sequence

Result = SVDFIT( X, Y [, M] )

Arguments

X

An n-element vector of independent variables.

Y

A vector of dependent variables, the same length as X .

M

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.

Keywords

A

Set this keyword equal to a vector of initial estimates for the fitted function parameters. SVDFIT returns 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.

CHISQ

Set this keyword equal to a named variable that will contain the sum of squared errors multiplied by weights if weights are specified.

COVAR

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

DOUBLE

Set this keyword to force the computation to be done in double-precision arithmetic.

FUNCTION_NAME

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 Example section below for an example function.

LEGENDRE

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.

SIGMA

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

SINGULAR

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.

VARIANCE

Set this keyword equal to a named variable that will contain the variance (sigma squared) of each coefficient M .

WEIGHTS

Set this keyword equal to a vector of weights for Y i . This vector should be the same length as X and Y . The error for each term is weighted by WEIGHTS i when computing the fit. Frequently, WEIGHTS i  = 1.0/s 2 i , where s is the measurement error or standard deviation of Y i (Gaussian or instrumental weighting), or WEIGHTS = 1/Y (Poisson or statistical weighting). 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 vector of calculated Y values.

Example

To fit a function of the following form:

first, create the function in IDL, then create a procedure to perform the fit. Enter the following IDL commands into a file called example_svdfit.pro :

FUNCTION myfunct, X ,M

    return,[ [1.0], [SIN(2*X)/X], [COS(4.*X)^2.] ]

END

PRO example_svdfit

C = [7.77, 8.88, -9.99] ; Provide an array of coefficients.

X = FINDGEN(100)/15.0 + 0.1

Y = C[0] + C[1] * SIN(2*X)/X + C[2] * COS(4.*X)^2.

sig = 0.05 * Y ; Set uncertainties to 5%

A=[1,1,1] ; Provide an initial guess

result_a = SVDFIT(X, Y, A=A, WEIGHTS=(1/SIG^2.), $

    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

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 plot, IDL prints:

result_a [ 0 ] = 7.7700 +- 0.1757 VS. 7.7700

result_a [ 1 ] = 8.8800 +- 0.1631 VS. 8.8800

result_a [ 2 ] = -9.9900 +- 0.2833 VS. -9.9900

See Also

CURVEFIT , GAUSSFIT , LMFIT , POLY_FIT , POLYFITW , REGRESS , SFIT