
在matlab里如何将下面两组数据拟合成函数?是拟合成二次函数,三次函数,还是指数函数更准确?
y=[94.1582 94.2 94.28 94.45 94.6565 94.77 94.9 95.22 95.5501 95.75 96.1 96.3 96.5748 96.65 96.75 96.83 96.9191 96.75 96.6 96.1 95.6500]; 展开
x=[510 512.5 515 517.5 520 522.5 525 527.5 530 532.5 535 537.5 540 542.5 545 547.5 550 552.5 555 557.5 560];
y=[94.1582 94.2 94.28 94.45 94.6565 94.77 94.9 95.22 95.5501 95.75 96.1 96.3 96.5748 96.65 96.75 96.83 96.9191 96.75 96.6 96.1 95.6500];
n=1;
p=polyfit(x,y,n);
r=sum(sqrt((y-polyval(p,x)).^2));
TF=1;
while(TF)
nnew=n+1;
pnew=polyfit(x,y,nnew);
rnew=sum(sqrt((y-polyval(pnew,x)).^2));
if rnew>=r
break;
end
n=nnew;p=pnew;r=rnew;
end
xx=linspace(min(x),max(x));
plot(x,y,'k.');hold on
plot(xx,polyval(p,xx),'r');
hold off;
title(['y=' poly2str(p,'x')]);
legend('数据','拟合曲线','location','northwest');
以上程序按用多项式拟合数据,并自动修改,寻找误差最小的多项式
结果上看,5次多项式的拟合得到误差最小
其实4次也已经不错了,用6次的误差反而增加

2023-08-15 广告