Polynomial Curve Fitting

polyfit finds the coefficients of a polynomial that fits a set of data in a least-squares sense:

p = polyfit(x,y,n)

x and y are vectors containing the x and y data to be fitted, and n is the degree of the polynomial to return. For example, consider the x-y test data

x = [1 2 3 4 5];
y = [5.5 43.1 128 290.7 498.4];

A third degree polynomial that approximately fits the data is

p = polyfit(x,y,3)
p =

   -0.1917   31.5821  -60.3262   35.3400

Compute the values of the polyfit estimate over a finer range, and plot the estimate over the real data values for comparison:

x2 = 1:.1:5;
y2 = polyval(p,x2);
plot(x,y,'o',x2,y2)
grid on

See Also

| |

Was this topic helpful?