Year=[1625 1830 1930 1960 1974 1987 1999];
Population=[5 10 20 30 40 50 60];
Year1=1625:2020;
Year2=2000:2020;
[P2 S2]=polyfit(Year,Population,3);
Population1=polyval(P2,Year1);
Population2=polyval(P2,Year2);
plot(Year,Population,'*',Year2,Population2,'X',Year1,Population1);
这里的S2是误差的大小~
拟合值与实际值做差,使得sum(abs(shiji-nihe))取得最小即可
一般阶次高精度也较高,但表达式往往比较复杂,在简单与精度方面折中考虑