matlab函数拟合与误差检验?

2024-12-30 08:50:10
推荐回答(1个)
回答1:

可这样改:
x=1:1:7;
y=[170,1228,1841,3331,1784,1379,1499];
[p,S]=polyfit(x,y,4);
p_x=polyval(p,x);
plot(x,y,'-o')
xlabel('x'),ylabel('y')
hold on
plot(x,p_x,'-+')
yerr=y-p_x;
plot(x,yerr,'-*')
hold off
text(1,min(y)-0.4,['p(x)=',poly2str(p,'x')])
legend('原数据','拟合数据','误差')