POLYFITW Function
Standard Library function that fits an n-degree polynomial curve through a set of data points using the least-squares method.
Usage
result = POLYFITW(x, y, wt, deg[, yft, ybd, sig, mat])
Input Parameters
x — The vector containing the independent x-coordinates of the data.
y — The vector containing the dependent y-coordinates of the data. Must have the same number of elements as x.
wt — The vector of weighting factors for determining the weighting of the least-squares fit. Must have the same number of elements as x. Normalize this parameter for best results.
deg — The degree of the polynomial to be fitted to the data.
Output Parameters
yft — (optional) The vector containing the calculated y values.
ybd — (optional) The vector containing the error estimate of each point. (The error estimate is equal to one standard deviation.)
sig — (optional) The standard deviation of the function, expressed in the units of the y direction.
mat — (optional) The correlation matrix of the coefficients.
Returned Value
result — The vector containing the coefficients of the polynomial equation that best approximates the data. It has a length of deg + 1.
Keywords
None.
Discussion
POLYFITW is similar to the POLY_FIT function, except that it permits the weighting of data points. Weighting is useful when you want to correct for potential errors in the data you are fitting to a curve. The weighting factor, wt, adjusts the parameters of the curve so that the error at each point of the curve is minimized. For more information, see the section "Weighting Factor" on page 272 of this Reference.
Example
; Create the data.
x = FINDGEN(9)
y = [5., 4., 3., 2., 2., 3., 5., 6., 7.]
TEK_COLOR
; Load a color table and plot the original data.
PLOT, x, y, Title='POLYFITW EXAMPLE'
wt = FLTARR(9) + 1.0
; Fit with a first-order polynomial, without weighting.
coeff_no_wt = POLYFITW(x, y, wt, 1, yfit)
; Overplot the calculated values on the original plot.
OPLOT, x, yfit, Color=WoColorConvert(3)
wt = 1.0/y
; Fit with statistical weighting. 
coeff_stat_wt = POLYFITW(x, y, wt, 1, yfit)
; Overplot the calculated values on the original plot.
OPLOT, x, yfit, Color=WoColorConvert(2)
labels = ['Original data', 'Fit with no weighting', $
   'Fit with statistical weighting']
; Put a legend on the plot.
LEGEND, labels, WoColorConvert([255, 3, 2]), [0, 0, 0], $
   [0, 0, 0], 4., 1.5, .3
See Also