t= [1920:10:1990];
pop= [106.46 123.08 132.12 152.27 180.67 205.05 227.23 249.46];
plot(t,pop,'o')

hold on
a=[2000];
pop= [281.42];
plot(a, pop, 'ro')

% first order 
t=[1990 1980];
pop=[249.46 227.23];
ts= (t-1955)/35;
p=polyfit(ts, pop, 1);
k= polyval(p,(2000-1955)/35);
fprintf('First Order approximation: %f\n', k)

tt= linspace(1920, 2000);
pp= polyval(p,(tt- 1955)/35);
plot(tt,pp,'g--')

% second order 
t=[t 1970];
pop=[pop 205.05];
ts= (t-1955)/35;
p=polyfit(ts, pop, 2);
k= polyval(p,(2000-1955)/35);
fprintf('Second Order approximation: %f\n', k)

pp= polyval(p,(tt- 1955)/35);
plot(tt,pp,'k-.')

% third order 
t=[t 1960];
pop=[pop 180.67];
ts= (t-1955)/35;
p=polyfit(ts, pop, 3);
k= polyval(p,(2000-1955)/35);
fprintf('Third Order approximation: %f\n', k)

pp= polyval(p,(tt- 1955)/35);
plot(tt,pp,'m:')

% fourth order 
t=[t 1950];
pop=[pop 152.27];
ts= (t-1955)/35;
p=polyfit(ts, pop, 4);
k= polyval(p,(2000-1955)/35);
fprintf('Fourth Order approximation: %f\n', k)

pp= polyval(p,(tt- 1955)/35);
plot(tt,pp)

% fifth order 
t=[t 1940];
pop=[pop 132.12];
ts= (t-1955)/35;
p=polyfit(ts, pop, 5);
k= polyval(p,(2000-1955)/35);
fprintf('Fifth Order approximation: %f\n', k)

pp= polyval(p,(tt- 1955)/35);
plot(tt,pp, 'k:')

% sixth order 
t=[t 1930];
pop=[pop 123.08];
ts= (t-1955)/35;
p=polyfit(ts, pop, 6);
k= polyval(p,(2000-1955)/35);
fprintf('Sixth Order approximation: %f\n', k)

pp= polyval(p,(tt- 1955)/35);
plot(tt,pp, 'b-.')

% seventh order 
t=[t 1920];
pop=[pop 106.46];
ts= (t-1955)/35;
p=polyfit(ts, pop, 7);
k= polyval(p,(2000-1955)/35);
fprintf('Seventh Order approximation: %f\n', k)

pp= polyval(p,(tt- 1955)/35);
plot(tt,pp, 'm-.')

legend('data', 'goal','1st order', '2nd order', '3rd order', '4th order', '5th order', '6th order', '7th order', 'Location', 'SouthEast')
