1. Polynomial fitting:
f=polyfit(x,y,n), fit the data x and y n times, and x and y must have the same dimension.
polyval(f,xi)%xi is a set of arrays after the interpolation has become dense. It is generally used for drawing to calculate the value of the polynomial
example:
x = linspace (0.4 * pi, 10); y = sin (x);
Use polyfit
to fit a 7th-degree polynomial to the points.
p = polyfit(x,y,7);
Evaluate the polynomial on a finer grid and plot the results.
x1 = linspace(0,4*pi); y1 = polyval(p,x1); figure plot(x,y,'o') hold on plot(x1,y1) hold off
The final drawing effect:
2. Specify function fitting
(1): fit() and fittype()% see the help file for more details
g = fittype( @(a, b, c, d, x, y) a*x.^2+b*x+c*exp(... -(y-d).^2 ), 'independent', { 'x', 'y'},... 'dependent', 'z' );
myfittype = fittype( 'a*x.^2+b*x+c*exp(-(yd).^2)' , ... 'dependent' ,{ 'x,y' }, 'independent' ,{ 'x' }, ... 'coefficients' ,{ 'a' , 'b' })% quotation marks are used to change linesNote the difference between the two definition functions. What is written after'independent' is to define who is the variable, and what is after'coefficient' is who is the unknown coefficient.
(2): lsqcurvefit fitting
x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub), fun is a function, if it is a function handle @definition, fill in the function fun directly, if it is a .m file definition, put quotation marks around the file name. x0 is the defined search initial value, xdata, ydata are the data to be fitted, lb, ub are the limits of the solution x, and the final answer x will only be between lb and ub, that is, lb<=x<= ub. If it is not limited, leave it blank, or write [],[].
annotation:
The wording of fun. For example, for f=a*x^2+sin(b*x)-exp(-c*x); fitting, because fitting is the process of determining the function coefficients a, b, c, a unified Variables to express the coefficient, f=@(a,x)a(1)*x^2+sin(a(2)*x)-exp(-a(3)*x). Use a(1),a (2), a(3) to uniformly express the coefficients, the same applies when using file definitions.