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.
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.
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.
Set this keyword equal to a named variable that will contain the sum of squared errors multiplied by weights if weights are specified.
Set this keyword equal to a named variable that will contain the covariance matrix of the fitted coefficients.
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.
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 equal to a named variable that will contain the vector of standard deviations for the returned coefficients.
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 equal to a named variable that will contain the variance (sigma squared) of each coefficient M .
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.
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
:
return,[ [1.0], [SIN(2*X)/X], [COS(4.*X)^2.] ]
C = [7.77, 8.88, -9.99] ; Provide an array of coefficients.
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)
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)'
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