MATLAB polyfit function - polynomial fitting

1. Basic form

p = polyfit(x,y,n)

    This function uses an n-degree polynomial to fit a set of data points (x, y), and outputs the polynomial coefficients in the form of an array p. The coefficients in p are arranged in descending powers, and the array length is n+1.

p(x)=p_{1}x^{n}+p_{2}x^{n-1}+...+p_{n}x+p_{n+1}

    If you want to plot the fitted polynomial coefficients, you can use the polyval function:

y1 = polyval(p,x1)

    The function of this function is to calculate the corresponding y1 value through the polynomial coefficient array p for a given x1 value. Then you can easily use x1 and y1 to draw the polynomial curve. x1 is generally a more precise independent variable value defined using the linspace function.

For a more complete introduction to the polyfit function, see: Polynomial Curve Fitting

2. Example

    Fit the 10 sets of data of the flow area x and the mass flow rate y flowing through the valve port with a quadratic polynomial, and draw the data points and the quadratic polynomial into a graph:

x = [0.9181,1.1006,1.2803,1.4574,1.6317,1.8033,1.9721,2.1382,2.3016,2.4623];
y = [0.1108,0.1306,0.1529,0.1805,0.1972,0.2181,0.2412,0.2601,0.2864,0.3195];
p = polyfit(x,y,2);
xx = linspace(0.8,3);
yy = polyval(p,xx);
figure
plot(x,y,'ko',xx,yy,'LineWidth',2,'MarkerFaceColor','k');

 After running, the value of array p is [0.0155, 0.0784, 0.0271], indicating that the fitted quadratic polynomial is:

p(x)=0.0155x^{2}+0.0784x+0.0271

Guess you like

Origin blog.csdn.net/Ronko_G/article/details/130381406