load census.dat
xd=census(:,1);
yd=census(:,2);
figure(1)
plot(xd,yd,'O:')

% LINE FIT ax + b
linecoeff=polyfit(xd,yd,1)
xp=linspace(1890,2010,100);
yp1=polyval(linecoeff,xp);
figure(1)
plot(xd,yd,'O',xp,yp1,'r','linewidth',2)

% CUBIC FIT ax^3 + bx^2 + cx + d
cubiccoeff=polyfit(xd,yd,3)
yp3=polyval(cubiccoeff,xp);
figure(2)
plot(xd,yd,'O',xp,yp3,'r','linewidth',2)

% EXPONENTIAL FIT c*exp(a*x) via the ln(y) transformation
lnyd=log(yd);
expcoeff=polyfit(xd,lnyd,1);
ypexp=exp(polyval(expcoeff,xp));
figure(3)
plot(xd,yd,'O',xp,ypexp,'r','linewidth',2)

% evaluate root-mean-square error of the fits
n=length(xd);
yf1=polyval(linecoeff,xd);
E2line=sqrt( sum((yd-yf1).^2)/n )
yf3=polyval(cubiccoeff,xd);
E2cubic=sqrt( sum((yd-yf3).^2)/n )
yfexp=exp(polyval(expcoeff,xd));
E2exp=sqrt( sum((yd-yfexp).^2)/n )
