A list,

The least squares method is a mathematical optimization technique, and it is the simplest optimization problem.

1 the principle





2 methods





Three examples



Ii. Source code

clf;
set(gcf,'Unit'.'normalized'.'Position'[0.2.0.3.0.60.0.45]);
set(gcf,'Menubar'.'none'.'Name'.'Least square curve Fitting'.'NumberTitle'.'off');
axes('Position'[0.05.0.1.0.55.0.85]);
uicontrol(gcf,'Style'.'text'.'Unit'.'normalized'.'Posi'[0.63.0.875.0.15.0.05].'String'.Enter x vector:.'FontSize'.11.'Horizontal'.'center'.'BackgroundColor'[0.8 0.8 0.8]);
hedit1=uicontrol(gcf,'Style'.'edit'.'Unit'.'normalized'.'Posi'[0.63.0.8.0.35.0.075].'Max'.1);   
uicontrol(gcf,'Style'.'text'.'Unit'.'normalized'.'Posi'[0.63.0.7.0.15.0.05].'String'.'Input y vector:'.'FontSize'.11.'Horizontal'.'center'.'BackgroundColor'[0.8 0.8 0.8]);
hedit2=uicontrol(gcf,'Style'.'edit'.'Unit'.'normalized'.'Posi'[0.63.0.625.0.35.0.075].'Max'.1);  
uicontrol(gcf,'Style'.'text'.'Unit'.'normalized'.'Posi'[0.63.0.525.0.15.0.05].'String'.'Input xi vector:'.'FontSize'.11.'Horizontal'.'center'.'BackgroundColor'[0.8 0.8 0.8]);
hedit3=uicontrol(gcf,'Style'.'edit'.'Unit'.'normalized'.'Posi'[0.63.0.45.0.35.0.075].'Max'.1);  
hpush1=uicontrol(gcf,'Style'.'push'.'Unit'.'normalized'.'Posi'[0.63.0.3.0.25.0.1].'String'.'Draw the raw data curve'.'FontSize'.11.'Call'.'x=str2num(get(hedit1,''String'')); y=str2num(get(hedit2,''String'')); plot(x,y,'':o''); ');
hpush2=uicontrol(gcf,'Style'.'push'.'Unit'.'normalized'.'Posi'[0.63.0.175.0.2.0.1].'String'.'Draw the fit curve'.'FontSize'.11.'Call'.'hold on; x=str2num(get(hedit1,''String'')); y=str2num(get(hedit2,''String'')); xi=str2num(get(hedit3,''String'')); p=polyfit(x,y,2); yi=polyval(p,xi); plot(xi,yi,''r-*''); hold off');
hpush3=uicontrol(gcf,'Style'.'push'.'Unit'.'normalized'.Copy the code

3. Operation results

Fourth, note

Version: 2014 a