The POLY_FIT function performs a least-square polynomial fit with optional weighting and returns a vector of coefficients.

The POLY_FIT routine uses matrix inversion to determine the coefficients. A different version of this routine, SVDFIT, uses singular value decomposition (SVD). The SVD technique is more flexible and robust, but may be slower.

This routine is written in the IDL language. Its source code can be found in the file poly_fit.pro in the lib subdirectory of the IDL distribution.

Examples


In this example, we use X and Y data corresponding to the known polynomial f (x) = 0.25 - x + x2. Using POLY_FIT to compute a second degree polynomial fit returns the exact coefficients (to within machine accuracy).

; Define an 11-element vector of independent variable data:
X = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
; Define an 11-element vector of dependent variable data:
Y = [0.25, 0.16, 0.09, 0.04, 0.01, 0.00, 0.01, 0.04, 0.09, $
   0.16, 0.25]
; Define a vector of measurement errors:
measure_errors = REPLICATE(0.01, 11)
; Compute the second degree polynomial fit to the data:
result = POLY_FIT(X, Y, 2, MEASURE_ERRORS=measure_errors, $
   SIGMA=sigma)
; Print the coefficients:
PRINT, 'Coefficients: ', result
PRINT, 'Standard errors: ', sigma

IDL prints:

Coefficients:    0.250000    -1.00000    1.00000
Standard errors:    0.00761853    0.0354459    0.0341395

Syntax


Result = POLY_FIT( X, Y, Degree [, CHISQ=variable] [, COVAR=variable] [, /DOUBLE] [, MEASURE_ERRORS=vector] [, SIGMA=variable] [, STATUS=variable] [, YBAND=variable] [, YERROR=variable] [, YFIT=variable] )

Return Value


POLY_FIT returns a vector of coefficients of length Degree+1. If the DOUBLE keyword is set, or if X or Y are double precision, then the result will be double precision, otherwise the result will be single precision.

Arguments


X

An n-element vector containing the independent variable values. X may be of type integer, floating point, or double-precision floating-point.

Y

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

Degree

The degree of the polynomial to fit.

Keywords


CHISQ

Set this keyword to a named variable that will contain the value of the unreduced chi-square goodness-of-fit statistic.

COVAR

Set this keyword to a named variable that will contain the Covariance matrix of the coefficients.

Note: The COVAR matrix depends only upon the independent variable X and (optionally) the MEASURE_ERRORS. The values do not depend upon Y. See section 15.4 of Numerical Recipes in C (Second Edition) for details.

DOUBLE

Set this keyword to force computations to be done in double-precision arithmetic. All computations are performed using double-precision arithmetic.

MEASURE_ERRORS

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.

Note: For Gaussian errors (e.g., instrumental uncertainties), MEASURE_ERRORS should be set to the standard deviations of each point in Y. For Poisson or statistical weighting, MEASURE_ERRORS should be set to SQRT(Y).

SIGMA

Set this keyword to a named variable that will contain the 1-sigma uncertainty estimates for the returned parameters.

Note: If MEASURE_ERRORS is omitted, then you are assuming that a polynomial is the correct model for your data, and therefore, no independent goodness-of-fit test is possible. In this case, the values returned in SIGMA are multiplied by SQRT(CHISQ/(NM)), where N is the number of points in X, and M is the number of coefficients. See section 15.2 of Numerical Recipes in C (Second Edition) for details.

STATUS

Set this keyword to a named variable to receive the status of the operation. Possible status values are:

  • 0 = Successful completion.
  • 1 = Singular array (which indicates that the inversion is invalid). Result is NaN.
  • 2 = Warning that a small pivot element was used and that significant accuracy was probably lost.
  • 3 = Undefined (NaN) error estimate was encountered.

Note: If STATUS is not specified, any error messages will be output to the screen.

Tip: Status values of 2 or 3 can often be resolved by setting the DOUBLE keyword.

YBAND

Set this keyword to a named variable that will contain the 1 standard deviation error estimate for each point.

Note: If MEASURE_ERRORS is omitted, then you are assuming that a polynomial is the correct model for your data, and therefore, no independent goodness-of-fit test is possible. In this case, the values returned in YBAND are multiplied by YERROR (the standard error between YFIT and Y). See section 15.2 of Numerical Recipes in C (Second Edition) for details.

YERROR

Set this keyword to a named variable that will contain the standard error between YFIT and Y.

YFIT

Set this keyword to a named variable that will contain the vector of calculated Y values. These values have an error of + or – YBAND.

Version History


Original

Introduced

5.4

Deprecated the Yfit, Yband, Sigma, and Corrm arguments, added the MEASURE_ERRORS keyword.

See Also


COMFIT, CURVEFIT, GAUSSFIT, LINFIT, REGRESS, SFIT, SVDFIT