Upload
zheepirate
View
68
Download
0
Embed Size (px)
Citation preview
polyfit :: Functions (MATLAB)
text://11
MATLAB
Provide feedback about this page
polyfitPolynomial curve fitting
Syntaxp = polyfit(x,y,n) [p,S] = polyfit(x,y,n) [p,S,mu] = polyfit(x,y,n)
Descriptionp = polyfit(x,y,n) finds the coefficients of a polynomial p(x) of degree n that fits the data, p(x(i)) to y(i), in a least squares sense. The result p is a row vector of length n+1 containing the polynomial coefficients in descending powers
[p,S] = polyfit(x,y,n) returns the polynomial coefficients p and a structure S for use with polyval to obtain error estimates or predictions. Structure S contains fields R, df, and normr, for the triangular factor from a QR decomposition of the Vandermonde matrix of X, the degrees of freedom, and the norm of the residuals, respectively. If the data Y are random, an estimate of the covariance matrix of P is (Rinv*Rinv')*normr^2/df, where Rinv is the inverse of R. If the errors in the data y are independent normal with constant variance, polyval produces error bounds that contain at least 50% of the predictions. [p,S,mu] = polyfit(x,y,n) finds the coefficients of a polynomial in
where and . mu is the two-element vector . This centering and scaling transformation improves the numerical properties of both the polynomial and the fitting algorithm.
ExamplesThis example involves fitting the error function, erf(x), by a polynomial in x. This is a risky project because erf(x) is a bounded function, while polynomials are unbounded, so the fit might not be very good. First generate a vector of x points, equally spaced in the interval x = (0: 0.1: 2.5)'; y = erf(x); The coefficients in the approximating polynomial of degree 6 are p = polyfit(x,y,6) p = 0.0084 -0.0983 0.4217 -0.7435 0.1471 1.1064 0.0004 ; then evaluate erf(x) at those points.
There are seven coefficients and the polynomial is
To see how good the fit is, evaluate the polynomial at the data points with f = polyval(p,x); A table showing the data, fit, and error is
1 of 3
11/1/2011 9:50 AM
polyfit :: Functions (MATLAB)
text://11
table = [x y f y-f] table = 0 0.1000 0.2000 0.3000 0.4000 ... 2.1000 2.2000 2.3000 2.4000 2.5000 0 0.1125 0.2227 0.3286 0.4284 0.9970 0.9981 0.9989 0.9993 0.9996 0.0004 0.1119 0.2223 0.3287 0.4288 0.9969 0.9982 0.9991 0.9995 0.9994 -0.0004 0.0006 0.0004 -0.0001 -0.0004 0.0001 -0.0001 -0.0003 -0.0002 0.0002
So, on this interval, the fit is good to between three and four digits. Beyond this interval the graph shows that the polynomial behavior takes over and the approximation quickly deteriorates. x = (0: 0.1: 5)'; y = erf(x); f = polyval(p,x); plot(x,y,'o',x,f,'-') axis([0 5 0 2])
AlgorithmThe polyfit M-file forms the Vandermonde matrix, , whose elements are powers of .
It then uses the backslash operator, \, to solve the least squares problem
You can modify the M-file to use other functions of
as the basis functions.
See Alsopoly, polyval, roots, lscovProvide feedback about this page
2 of 3
11/1/2011 9:50 AM
polyfit :: Functions (MATLAB)
text://11
polyeig
polyint
1984-2009 The MathWorks, Inc. Terms of Use Patents Trademarks Acknowledgments
3 of 3
11/1/2011 9:50 AM