The POLY_FIT function performs a least-square polynomial fit with optional error estimates and returns a vector of coefficients with a length of NDegree +1.
The POLY_FIT routine uses matrix inversion. A newer version of this routine, SVDFIT, uses Singular Value Decomposition. The SVD technique is more flexible, but slower. Another version of this routine, POLYFITW, performs a weighted least square fit.
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.
In this example, we use X and Y data corresponding to the known polynomial f ( x ) = 0.25 - x + x 2 . Using POLY_FIT to compute a second degree polynomial fit returns the exact coefficients (to within machine accuracy).
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 independent 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 an 11-element vector of dependent variable data.
result = POLY_FIT(X, Y, 2) ; Compute the second degree polynomial fit to the data.