POLY_FIT Function
Standard Library function that fits an n-degree polynomial curve through a set of data points using the least-squares method.
Usage
result = POLY_FIT(x, y, 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.
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 which best approximates the data.
Keywords
None.
Discussion
POLY_FIT uses a least-squares method for determining the equation of the curve, which minimizes the error at each point of the curve. This function is useful for showing the relationship between two variables.
POLY_FIT returns a vector of length deg + 1. For example, if you request a polynomial of degree 3, the fitted curve would have the equation:
f(x) = result(3)x3 + result(2)x2 + result(1)x + result(0)
The yft parameter is in a format that can be displayed as a curve alongside the input curve, thereby allowing you to compare the two curves.
Example
; Create the data and load the color table.
x = FINDGEN(9)
y = [5., 4., 3., 2., 2., 3., 5., 6., 7.]
TEK_COLOR
; Plot the data and fit with first-order polynomial.
PLOT, x, y, Title='POLY_FIT EXAMPLE'
coeff_1_deg = POLY_FIT(x, y, 1, yfit)
; Overplot the calculated values on the original plot.
OPLOT, x, yfit, Color=WoColorConvert(3)
; Fit with a third-order polynomial.
coeff_3_deg = POLY_FIT(x, y, 3, yfit)
; Overplot the calculated values on the original plot.
OPLOT, x, yfit, Color=WoColorConvert(11)
; Fit with a fifth-order polynomial.
coeff_5_deg = POLY_FIT(x, y, 5, yfit)
; Overplot the calculated values on the original plot.
OPLOT, x, yfit, Color=WoColorConvert(6)
labels = ['Original data', 'Fit with first order polynomial', $
'Fit with third order polynomial', $
'Fit with fifth order polynomial']
; Put a legend on the plot.
LEGEND, labels, WoColorConvert([255, 3, 11, 6]), $
[0, 0, 0, 0], [0, 0, 0, 0], 4., 1.5, .3
See Also