%% Example US Census %load USCensus x=[1790:10:2000]; y=[3.93,5.31,7.24,9.64,12.87,17.07,23.19,31.44,39.82,50.16,62.95,75.99,91.97,105.71,122.78,131.67,151.33,179.32,203.21,226.5,249.63,281.42]; figure(1), plot(x,y,'o') axis([1780,x(end),0,y(end)+10]) xlabel('Decades of Census Data') ylabel('US Population in Millions') %% Compute k and y(0) using first two data points. % Time in years t = 0 corresponds to 1790. t = 0:10:210; % Model y0 = y(1); k = log(y(2)/y(1))/10; Pmodel = y0*exp(k*t); % Compare tt = t + 1790; figure(2) plot(tt,y,'o',tt,Pmodel) axis([1790 2000 0 300]) xlabel('year') ylabel('population') legend('Census Data','Model Prediction') %% Compute k and y(0) using least squares. p0 = [y(1),0.001]; p=nlinfit(t,y,@(p,x) p(1)*exp(p(2)*x),p0); exp_fit = p(1)*exp(p(2)*t); figure(3) plot(x,y,'o',x,exp_fit) axis([1780,x(end),0,y(end)+10]) xlabel('Decades of Census Data') ylabel('US Population in Millions') legend('Census Data','Exponential Fit')