Function File: `p` = **polyfit** (`x, y, n`)

Function File: [

p,s] =polyfit(x, y, n)

Function File: [

p,s,mu] =polyfit(x, y, n)

Return the coefficients of a polynomial

p(x) of degreenthat minimizes the least-squares-error of the fit to the points`[`

x`,`

y`]`

. Ifnis a logical vector, it is used as a mask to selectively force the corresponding polynomial coefficients to be used or ignored.The polynomial coefficients are returned in a row vector.

The optional output

sis a structure containing the following fields:

- ‘
R’- Triangular factor R from the QR decomposition.
- ‘
X’- The Vandermonde matrix used to compute the polynomial coefficients.
- ‘
C’- The unscaled covariance matrix, formally equal to the inverse of
x'*x, but computed in a way minimizing roundoff error propagation.- ‘
df’- The degrees of freedom.
- ‘
normr’- The norm of the residuals.
- ‘
yf’- The values of the polynomial for each value of
x.The second output may be used by

`polyval`

to calculate the statistical error limits of the predicted values. In particular, the standard deviation ofpcoefficients is given by

`sqrt (diag (s.C)/s.df)*s.normr`

.When the third output,

mu, is present the coefficients,p, are associated with a polynomial inxhat= (x-mu(1))/mu(2). Wheremu(1) = mean (x), andmu(2) = std (x). This linear transformation ofximproves the numerical stability of the fit.See also:polyval, polyaffine, roots, vander, zscore.

Package: octave