%%
clear
load census

%%
x = cdate-1900;
y = log( pop );

cdate_inicial = 0;
cdate_final = 1860;
% cdate_inicial = 1910;
% cdate_final = 2000;

ind_ok = find( cdate>=cdate_inicial & cdate<=cdate_final );
[ P ] = polyfit( x(ind_ok), y(ind_ok), 1 );

yfit = polyval( P, x );
Dif = y - yfit;

figure
subplot( 3, 1, 1:2 )
plot( x+1900, y, '*', x(ind_ok)+1900, y(ind_ok), 'ok', x+1900, yfit, '-r' )
xlim( [cdate(1)-5  cdate(end)+5] )
subplot( 3, 1, 3 )
plot( x+1900, Dif, '*', x(ind_ok)+1900, Dif(ind_ok), 'ok', x+1900, zeros(size(Dif)), '-k' )
xlim( [cdate(1)-5  cdate(end)+5] )
