1, least squares principle
Resources:
1, http://blog.csdn.net/lotus___/article/details/20546259
2, http://blog.sina.com.cn/s/blog_5404ea4f0101afth.html
2, Matlab to achieve the least squares
By using the least squares fitting function of Matlab to fit the nonlinear function, the function is fitted specifically:
[Q r] = Lsqcurvefit (fun, Q_0, XData, Ydata);
Input parameters:
Fun: A function that needs to be fitted, assuming that there are n parameters to fit, q = [q1,q2,..., qn]
Q_0: Represents a given starting point for a user
XData: Arguments to functions
Ydata: The dependent variable of a function
Output parameters:
Q: The optimal parameters of the solution are obtained
R: The value of the target function that represents the least squares, that is, the residuals.
Implementation code
Closex= 1:1:100; a=-1.5; b=-10; y= A*log (x) +b;plot (x, y); Yrand= y + 0.5*rand (1,size (y,2) );p lot (X,yrand,'ro');%%least squares fitting XF=log (x); YF=yrand;f=inline ('A (1) +a (2). *x','a','x'); [Q,r]=lsqcurvefit (f,[1, 0],XF,YF) plot (X,yrand,'ro','linewidth', 2)%Draw the chart hold on;%%draw a fit curve yn= q (1) +q (2) *log (x); hold On;plot (X,yn,'b','linewidth', 2);%%Set Legendhleg= Legend (['primitive function (y='Num2str (b,3)'+'Num2str (a,3)'ln (x)' ') Data'],['fit Result: y='NUM2STR (q (1), 3)'+'NUM2STR (q (2), 3)'ln (x)'],' Location','Northeast');%the size of the font cannot be set by itself, set (Hleg,'FontSize', 15,'Fontangle','Italic','FontWeight','Bold',... 'TextColor', [. 6,.2,.1],'Color', [1,1,1]);%Color Sets the background colors for coordinates%%Set caption Title ('least Squares fitting: y=a+b*ln (x)','Color','k','FontSize', 20); %%Axis Title Settings Xlabel ('x','Color','k','FontSize', 15);%Horizontal axis title Ylabel ('y','Color','k','FontSize', 15);%Vertical Axis Title%%Save Image Set (1,'inverthardcopy','if');%set the background color is valid, if the graphic does not save the background color, maltab default is Onfilename='LNX';Print(1,'-djpeg', filename);% Other formats-djpeg,-dpng,-dbmp,-dtiff,-dgif
Fitting results
As shown
Matlab least squares fitting nonlinear function