Confusion in using polyfit and polyval to predict data

4 views (last 30 days)
hi, I have a date of New Zealand population of 92 years. I am reading the code which uses polyfit and polyval to predict population for 2020 Here is the code
% loaded the nz_population.xlsx as Numeric Matrix
% making nz_years object from the first column
nz_years=nzpopulation(:,1);
% making nz_population object from the first column
nz_population=nzpopulation(:,2);
plot(nz_years,nz_population,'--');
%axis([1920 2020 1400000 5000000]);
ylim([1400000 5000000]);
title('NZ population from 1920 to 2018')
xlabel('Years')
ylabel('Population in Millions')
%adjusting the years axis
nz_years_adjust=(nz_years-1900)/50;
%getting the coefficents for linear,quardatic and cubic equations to to predict data
coef1=polyfit(nz_years_adjust,nz_population,1)
coef2=polyfit(nz_years_adjust,nz_population,2)
coef3=polyfit(nz_years_adjust,nz_population,3)
%predicting data using polyfit for linear,quardatic and cubic equations
pred1=polyval(coef1,nz_years_adjust);
pred2=polyval(coef2,nz_years_adjust);
pred3=polyval(coef3,nz_years_adjust);
[pred1;pred2;pred3]
% Getting the maximum values from the prediction made from linear,quardatic and cubic equations
max(pred1)
max(pred2)
max(pred3)
%plotting out predictions
%using hold on so that the previous curve is shown in these plots
hold on
plot(nz_years,pred1)
plot(nz_years,pred2)
plot(nz_years,pred3)
ylim([1100000 4690000])
legend({'Data','Linear','Quardatic','Cubic'},'Location','northwest')
hold off
xlim([1920 2020])
ylim([1300000 4650000])
xlim([1955.9 2022.6])
ylim([2721064 4954398])
%predicting the new zealand population for the year 2020
length(nz_years_adjust)
year2020=(2020-1900)/50;
pred1=polyval(coef1,year2020);
pred2=polyval(coef2,year2020);
pred3=polyval(coef3,year2020);
[pred1; pred2; pred3]
All code is understandable except for the last part where prediction is made for 2020 Please can anyone tell me why in the following last part,we are subtracting 1900 from 2020 and then dividing by 50? I am pasting the last part again
year2020=(2020-1900)/50;
pred1=polyval(coef1,year2020);
pred2=polyval(coef2,year2020);
pred3=polyval(coef3,year2020);
[pred1; pred2; pred3]

Accepted Answer

James Tursa
James Tursa on 11 Apr 2018
Because that is the "time unit" used for your polyfit calls earlier in the code:
nz_years_adjust=(nz_years-1900)/50;
I.e., the coefficients generated by polyfit were based on these "adjusted" time units.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!