Matlab擬合詳解

多項式擬合


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)

p = 1×3      -0.7630    8.5343   25.9050

xi=1:0.5:10;

yi=polyval(p,xi); % 計算擬合的結果

hold on
plot(xi,yi); % 繪制擬合結果圖

hold off

Matlab擬合詳解的圖1


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

Matlab擬合詳解的圖2

如果要進行預測,看之后某些點的走向

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

Matlab擬合詳解的圖3

更為強大的fit函數

一維多項式擬合(曲線)


clear

x = linspace(0,4*pi,10)';
y = sin(x);
f=fit(x,y,'poly7') % 最大可到poly9

figure

plot(f,x,y)

Matlab擬合詳解的圖4

二維多項式擬合(曲面)


load franke

sf = fit([x, y],z,'poly23') % 最大可到poly55

plot(sf,[x,y],z)

Matlab擬合詳解的圖5

指定擬合參數和類型


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

Matlab擬合詳解的圖6

定義函數,根據指定函數文件進行擬合
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 )

Matlab擬合詳解的圖7

排除個別點之后進行擬合


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')

Matlab擬合詳解的圖8

figure
plot(f2,x,y)
title('Fit with data points excluded such that x < 800')

Matlab擬合詳解的圖9

曲面情況下剔除部分點,并在圖中標記

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')

Matlab擬合詳解的圖10

figure
plot(f2, [x y], z, 'Exclude', z > 1);
title('Fit with data points excluded such that z > 1')

Matlab擬合詳解的圖11

Curve Fitting app

Curve Fitting 3 階和5階多項式擬合

滑動平均

Matlab擬合詳解的圖12


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])

Matlab擬合詳解的圖13

平滑樣條

Matlab擬合詳解的圖14

p=0 最小二乘直線擬合

p=1 三次樣條插值

Matlab擬合詳解的圖15

使用fit函數進行平滑


f = fit(x,y,'smoothingspline');
figure
plot(f,x,y)

Matlab擬合詳解的圖16


f = fit(x,y,'smoothingspline','SmoothingParam',0.4);
figure
plot(f,x,y)

Matlab擬合詳解的圖17

[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;
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}); % 插值
subplot(2,2,i) % 子圖plot(x,y,'ro',xi,yi,'b'),xlabel(strlb(i)) % 繪圖

end

Matlab擬合詳解的圖18

二維數據插值


clear

[x,y,z]=peaks(6); % MATLAB自帶的測試函數

figure
mesh(x,y,z) % 繪制原始數據圖

title('原始數據')

Matlab擬合詳解的圖19

Matlab擬合詳解的圖20

Matlab擬合詳解的圖21

使用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擬合詳解的圖22

以上內容僅是Matlab課程中一節課很小部分的內容,每節課程內容都非常豐富,老師的經驗和技巧,讓同學們受益匪淺。想學習更多內容,請添加客服咨詢。

登錄后免費查看全文
立即登錄
App下載
技術鄰APP
工程師必備
  • 項目客服
  • 培訓客服
  • 平臺客服

TOP

10
2
2