Documentation Center

  • Trial Software
  • Product Updates

polyfit

Polynomial curve fitting

Syntax

p = polyfit(x,y,n)
[p,S] = polyfit(x,y,n)
[p,S,mu] = polyfit(x,y,n)

Description

p = 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 [μ12]. This centering and scaling transformation improves the numerical properties of both the polynomial and the fitting algorithm.

Examples

expand all

Fit Polynomial to Error Function

This 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 [0, 2.5], then evaluate erf(x) at those points.

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

There are seven coefficients, and the polynomial is

$$ 0.0084x^6 - 0.0983x^5 + 0.4217x^4 - 0.7435x^3 +0.1471x^2 +1.1064x +
0.0004 $$

To see how good the fit is, evaluate the polynomial at the data points.

f = polyval(p,x);

A table showing the data, fit, and error is

table = [x y f y-f]
table =

         0         0    0.0004   -0.0004
    0.1000    0.1125    0.1119    0.0006
    0.2000    0.2227    0.2223    0.0004
    0.3000    0.3286    0.3287   -0.0001
    0.4000    0.4284    0.4288   -0.0004
    0.5000    0.5205    0.5209   -0.0004
    0.6000    0.6039    0.6041   -0.0002
    0.7000    0.6778    0.6778    0.0000
    0.8000    0.7421    0.7418    0.0003
    0.9000    0.7969    0.7965    0.0004
    1.0000    0.8427    0.8424    0.0003
    1.1000    0.8802    0.8800    0.0002
    1.2000    0.9103    0.9104   -0.0000
    1.3000    0.9340    0.9342   -0.0002
    1.4000    0.9523    0.9526   -0.0003
    1.5000    0.9661    0.9664   -0.0003
    1.6000    0.9763    0.9765   -0.0002
    1.7000    0.9838    0.9838    0.0000
    1.8000    0.9891    0.9889    0.0002
    1.9000    0.9928    0.9925    0.0003
    2.0000    0.9953    0.9951    0.0002
    2.1000    0.9970    0.9969    0.0001
    2.2000    0.9981    0.9982   -0.0001
    2.3000    0.9989    0.9991   -0.0003
    2.4000    0.9993    0.9995   -0.0002
    2.5000    0.9996    0.9994    0.0002

On this interval, the interpolated values and the actual values agree fairly closely. Outside this interval, the extrapolated values quickly diverge from the actual data values, as the following plot demonstrates.

x = (0: 0.1: 5)';
y = erf(x);
f = polyval(p,x);
plot(x,y,'o',x,f,'-')
axis([0  5  0  2])

More About

expand all

Algorithms

The polyfit MATLAB® file forms the Vandermonde matrix, V, whose elements are powers of x.

It then uses the backslash operator, \, to solve the least squares problem Vpy.

See Programmatic Fitting for information about fitting different functions of x.

See Also

| | | |

Was this topic helpful?