Matlab拟合详解

多项式拟合
clear x=1:1:10; plot(x,y,'o') % 绘图并标出原始数据点 p=polyfit(x,y,2) |
p = 1×3 -0.7630 8.5343 25.9050
xi=1:0.5:10; yi=polyval(p,xi); % 计算拟合的结果 hold on hold off |
clear x = linspace(0,4*pi,10)'; y = sin(x); p = polyfit(x,y,7); x1 = linspace(0,4*pi); y1 = polyval(p,x1); figure plot(x,y,'o') hold onplot(x1,y1) hold off |
如果要进行预测,看之后某些点的走向
x2=linspace(0,4.2*pi,120); y2=sin(x2); x3=x2; y3=polyval(p,x3); figure plot(x,y,'o') hold onplot(x2,y2,'r') plot(x3,y3,'--g') hold off |
更为强大的fit函数
一维多项式拟合(曲线)
clear x = linspace(0,4*pi,10)'; |
figure
plot(f,x,y)
二维多项式拟合(曲面)
load franke sf = fit([x, y],z,'poly23') % 最大可到poly55 |
plot(sf,[x,y],z)
指定拟合参数和类型
clear load censusplot(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) |
[curve3,gof3] = fit(cdate,pop,ft,'problem',3)
hold onplot(curve2,'m') plot(curve3,'c') legend('Data','n=2','n=3') hold off
定义函数,根据指定函数文件进行拟合
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])
f2 = fit(x',y',gaussEqn,'Start', startPoints, 'Exclude', x < 800)
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 frankef1 = 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')
Curve Fitting app
Curve Fitting 3 阶和5阶多项式拟合
滑动平均
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); yy1 = smooth(x,y,0.1,'loess'); 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]) legend('5') subplot(3,1,2) plot(x,y,'b.',x,yy1,'g-') set(gca,'YLim',[-1.5 3.5]) legend('loess') % Local regressionsubplot(3,1,3) plot(x,y,'b.',x,yy2,'y-') set(gca,'YLim',[-1.5 3.5]) |
平滑样条
p=0 最小二乘直线拟合
p=1 三次样条插值
使用fit函数进行平滑
f = fit(x,y,'smoothingspline'); figure plot(f,x,y) |
f = fit(x,y,'smoothingspline','SmoothingParam',0.4); figure plot(f,x,y) |
[f,gof,out]= fit(x,y,'smoothingspline','SmoothingParam',0.4)
options = fitoptions('Method','Smooth','SmoothingParam',0.3); [f,gof,out] = fit(x,y,'smooth',options)
插值
一维数据插值
clear x=0:10; |
strlb={'(a)method=nearest','(b)method=linear',... '(c)method=spline','(d)method=pchip'} % 绘图标签 |
for i=1:4 yi=interp1(x,y,xi,strmod{i}); % 插值 end |
二维数据插值
clear [x,y,z]=peaks(6); % MATLAB自带的测试函数 figure title('原始数据') |
使用fit函数进行插值
figure clear load carbon12alphaf1 = 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 offlegend([p1;p2],'Counts per Angle','Nearest Neighbor','pchip',...
'Location','northwest')
以上内容仅是Matlab课程中一节课很小部分的内容,每节课程内容都非常丰富,老师的经验和技巧,让同学们受益匪浅。想学习更多内容,请添加客服咨询。

工程师必备
- 项目客服
- 培训客服
- 平台客服
TOP
