Curve Fitting in MATLAB
In Matlab, The polyfit function can be used for fitting curves. This method is called polynomial fitting:
Polynomial fitting:
X = 0: 0.; y = [-0.447 1.978 3.28 6.16 7.08 7.34 7.66 9.56 9.48 9.30]; A = polyfit (X, Y, 2 ); % use quadratic polynomial curves to fit z = polyval (A, x); plot (X, Y, 'r * ', X, Z,' B ');
Note:
(X, y) In A = polyfit (X, Y, n) indicates the data coordinate to be fitted, N indicates the degree of the fit polynomial (when n is greater than or equal to the number of data to be fitted, it will always be completely fit), and the returned a indicates the coefficient of the fit polynomial, that is, a =, the fitted polynomial f (x) satisfies the following requirements: Therefore, the number of elements of a is equal to n + 1.
Z = polyval (A, x) indicates that the Y value corresponding to X is calculated based on the fitted polynomial f (x) of the coefficient matrix.
The result is as follows:
MATLAB also provides a graphic window-based fitting operation, which is very intuitive and convenient:
1. Enter the following statement:
x=0:0.1:1;y=[-0.447 1.978 3.28 6.16 7.08 7.34 7.66 9.56 9.48 9.30 11.2];plot(x, y, 'r*');
2. Open tools --> basic fitting in the figure graph window. You can set the fitting curve in the pop-up window to display the fitting curve equation, calculation of residual and other information.
3. Press the right arrow to see the equation of the fitting curve, and then press the right arrow to enter X to calculate the Y value corresponding to the fitting curve, or save these values to the workspace.
Note: residual = exact value-fitting value