我有一个问题要求我对人口数据进行线性、二次和三次拟合,然后估计 1915 年的人口。线性和二次拟合有效,但是三次似乎引发了一个错误,告诉我多项式条件很差。该图非常接近数据值,似乎很合适。我能做些什么来解决这个问题?代码是:
clear;
clc;
close all;
year = [1815,1845,1875,1905,1935,1965];
population = [8.3,19.7,44.4,83.2,127.1,190.9];
rlinear = polyfit(year,population,1);
rquadratic = polyfit(year,population,2);
rcubic = polyfit(year,population,3);
newTime = linspace(1815,1965,100);
vrlinear = polyval(rlinear,newTime);
vrquadratic = polyval(rquadratic,newTime);
vrcubic = polyval(rcubic,newTime);
subplot(2,2,1)
plot(year,population,'ob',newTime,vrlinear)
xlabel('Year')
ylabel('Population (millions)')
title('Year vs. US population')
subplot(2,2,2)
plot(year,population,'ob',newTime,vrquadratic)
xlabel('Year')
ylabel('Population (millions)')
title('Year vs. US population')
subplot(2,2,3)
plot(year,population,'ob',newTime,vrcubic)
xlabel('Year')
ylabel('Population (millions)')
title('Year vs. US population')
estimate = polyval(rquadratic,1915);
fprintf('The estimated population in the year 1915 is %d million. \r',estimate)