一、简介
最小化二乘法是一种数学优化技术,是一种最简单的优化问题。
1 原理
2 方法
3 实例
二、源代码
clf;
set(gcf,'Unit','normalized','Position',[0.2,0.3,0.60,0.45]);
set(gcf,'Menubar','none','Name','最小二乘法曲线拟合','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',...
'输入x向量:','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',...
'输入y向量:','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',...
'输入xi向量:','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',...
'绘制原始数据曲线','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',...
'绘制拟合曲线','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',...
三、运行结果
四、备注
版本:2014a
完整代码或代写加1564658423