多项式拟合:用到的函数主要是polyfit和polyval
clear
x=1:1:10; % 原始数据
y=-0.9*x.^2+10*x+20+rand(1,10).*5;
plot(x,y,'o') % 绘图并标出原始数据点
p=polyfit(x,y,2) % 根据原始数据生成系数,2表示多项式的次数,与拟合的精度正相关,但并不是越高越好,可能会导致过拟合
xi=1:0.5:10; % 新数据
yi=polyval(p,xi); % 计算拟合的结果
hold on
plot(xi,yi); % 绘制拟合结果图
hold off
1,得到的p值和原始数据很相近
2,得到的p值和原始数据有差别,这是随机数造成的误差
3,在实际中是否可以消除这种误差,不可以期望通过算法来消除误差,要通过减少原始测量误差来实现
注意:如果要进行预测,看之后某些点的走向时(也就是新数据的范围比原始数据大),若是新数据并不符合多项式的规律,那么在原数据范围内的数据预测会更准确,而在原数据范围之外的数据则会出现较大误差,且多项式的次数越高,这种趋势将会越大(过拟合)
更为强大的fit函数:
一维多项式拟合(曲线)
clear
x = linspace(0,4*pi,10)'; % 原数据,注意必须要转置
y = sin(x);
f=fit(x,y,'poly7') % 使用多项式拟合且次数为7,最大可到poly9
figure
plot(f,x,y) % f不可错位
% 使用fit将会自动标出原数据,然后根据原数据生成相应的系数和新数据,通过plot可以直接把图画好
二维多项式拟合(曲面)
load franke % matlab的一组内置数据
sf = fit([x, y],z,'poly23') % 最大可到poly55
plot(sf,[x,y],z)
指定拟合参数和类型
clear
load census % 一组内置数据
plot(cdate,pop,'o')
fo = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[0,0],...
'Upper',[Inf,max(cdate)],...
'StartPoint',[1 1]);
% 指定拟合的参数,分别是方法,下限,上限,起始点
ft = fittype('a*(x-b)^n','problem','n','options',fo);
% 指定拟合的类型,第一个是多项式拟合,后面是多项式系数,然后是拟合的参数
[curve2,gof2] = fit(cdate,pop,ft,'problem',2) % 在这里指定n
[curve3,gof3] = fit(cdate,pop,ft,'problem',3)
% gof2保存的是一些统计数据
hold on
plot(curve2,'m')
plot(curve3,'c') % m和c是颜色参数
legend('Data','n=2','n=3')
hold off
定义函数,根据指定函数文件进行拟合:也就是自定义拟合的方法
% function y = piecewiseLine(x,a,b,c,d,k)
% % PIECEWISELINE A line made of two pieces
% % that is not continuous.
%
% y = zeros(size(x));
%
% % This example includes a for-loop and if statement
% % purely for example purposes.
% for i = 1:length(x)
% if x(i) < k,
% y(i) = a + b.* x(i);
% else
% y(i) = c + d.* x(i);
% end
% end
% end
x = [0.81;0.91;0.13;0.91;0.63;0.098;0.28;0.55;...
0.96;0.96;0.16;0.97;0.96];
y = [0.17;0.12;0.16;0.0035;0.37;0.082;0.34;0.56;...
0.15;-0.046;0.17;-0.091;-0.071];
ft = fittype( 'piecewiseLine( x, a, b, c, d, k )' )
f = fit( x, y, ft, 'StartPoint', [1, 0, 1, 0, 0.5] )
plot( f, x, y )
排除个别点之后进行拟合
clear
[x, y] = titanium;
gaussEqn = 'a*exp(-((x-b)/c)^2)+d' % 高斯拟合
startPoints = [1.5 900 10 0.6]
f1 = fit(x',y',gaussEqn,'Start', startPoints, 'Exclude', [1 10 25]) % Exclude是剔除数据的参数,表示剔除第1,10,25个
f2 = fit(x',y',gaussEqn,'Start', startPoints, 'Exclude', x < 800) % 剔除x小于800的数据
% f2 = fit(x',y',gaussEqn,'Start', startPoints, 'Exclude',25:35) % 提出第25到35个数据
% 剔除部分峰值,可以看出结果的变化
plot(f1,x,y)
title('Fit with data points 1, 10, and 25 excluded')
figure
plot(f2,x,y)
title('Fit with data points excluded such that x < 800')
曲面情况下剔除部分点,并在图中标记:自动标记
clear
load franke % 内置数据
f1 = fit([x y],z,'poly23', 'Exclude', [1 10 25]);
f2 = fit([x y],z,'poly23', 'Exclude', z > 1);
figure
plot(f1, [x y], z, 'Exclude', [1 10 25]);
title('Fit with data points 1, 10, and 25 excluded')
figure
plot(f2, [x y], z, 'Exclude', z > 1);
title('Fit with data points excluded such that z > 1')
也可以用app进行拟合
平滑:也是拟合的一种,使数据变化趋势更易读
clear
x = (0:0.1:15)';
y = sin(x) + 0.5*(rand(size(x))-0.5);
y([90,110]) = 3;
yy0 = smooth(x,y,5); % 5表示平滑的参数
yy1 = smooth(x,y,0.1,'loess'); % loess和下面的rloess都是平滑的函数,0.1为该函数的参数
yy2 = smooth(x,y,0.1,'rloess');
subplot(3,1,1)
plot(x,y,'b.',x,yy0,'r-')
set(gca,'YLim',[-1.5 3.5]) % 设置y轴的范围
legend('5')
subplot(3,1,2)
plot(x,y,'b.',x,yy1,'g-')
set(gca,'YLim',[-1.5 3.5])
legend('loess') % Local regression
subplot(3,1,3)
plot(x,y,'b.',x,yy2,'y-')
set(gca,'YLim',[-1.5 3.5])
legend('rloess') % A robust version of 'lowess'
也可以用app进行平滑,同下面的fit函数进行平滑原理相同
f = fit(x,y,'smoothingspline');
figure
plot(f,x,y)
[f,gof,out]= fit(x,y,'smoothingspline','SmoothingParam',0.4) 这个0.4意义同app中参数
figure
plot(f,x,y)
% gof一些统计信息
% out一些输出信息
options = fitoptions('Method','Smooth','SmoothingParam',0.3); %也可以通过这种方法进行平滑
[f,gof,out] = fit(x,y,'smooth',options)
插值:在已知数据准确的情况下向其中插入新的数据
一维插值
clear
x=0:10;
y=cos(x);
xi=0:0.25:10;
strmod={'nearest','linear','spline','pchip'} % 将插值方法存储到元胞数组
strlb={'(a)method=nearest','(b)method=linear',...
'(c)method=spline','(d)method=pchip'} % 绘图标签
for i=1:4
yi=interp1(x,y,xi,strmod{i}); % 插值,x,y为原数据,xi表示要插入的数据,后面是方法
subplot(2,2,i) % 子图
plot(x,y,'ro',xi,yi,'b'),xlabel(strlb(i)) % 绘图,'ro'是颜色和标志,'b'是颜色
end
二维三维类似,请自行查阅文档
使用fit函数插值
figure
clear
load carbon12alpha % 内置数据
f1 = fit(angle,counts,'nearestinterp');
f2 = fit(angle,counts,'pchip');
% 第三个参数是两种插值的方法
p1 = plot(f1,angle,counts);
xlim([min(angle),max(angle)])
hold on
p2 = plot(f2,'b');
hold off
legend([p1;p2],'Counts per Angle','Nearest Neighbor','pchip',...
'Location','northwest')
也可以用app进行插值