function census(callbackarg) %CENSUSGUI Try to predict the US population in the year 2020. % This example is older than MATLAB. It started as an exercise in % "Computer Methods for Mathematical Computations", by Forsythe, % Malcolm and Moler, published by Prentice-Hall in 1977. % The data set has been updated every ten years since then. % Today, MATLAB makes it easier to vary the parameters and see the % results, but the underlying mathematical principles are unchanged: % % Using polynomials of even modest degree to predict % the future by extrapolating data is a risky business. % % The data is from the decennial census of the United States for the % years 1900 to 2010. The task is to extrapolate beyond 2010. p = [ 75.995 91.972 105.711 123.203 131.669 150.697 ... 179.323 203.212 226.505 249.633 281.422 308.748]'; t = (1900:10:2010)'; % Census years x = (1890:1:2039)'; % Evaluation years w = (2020:10:2040)'; % Extrapolation target guess = 320; % Eyeball extrapolation z = guess; % Extrapolated value dmax = length(t)-1; % Maximum polynomial degree % Polynomial degree d = 2; %% degree % Update plot with new model model = 'spline';%%'polynomial';%%'exponential';%%polynomial naif';%%'pchip';%% switch model case 'census data' y = NaN*x; z = 320; case 'polynomial naif' s = t; c = polyfit(s,p,d); s = x; y = polyval(c,s); s = w; z = polyval(c,s); case 'polynomial' s = (t-1955)/55; c = polyfit(s,p,d); s = (x-1955)/55; y = polyval(c,s); s = (w-1955)/55; z = polyval(c,s); case 'pchip' y = pchip(t,p,x); z = pchip(t,p,w); case 'spline' y = spline(t,p,x); z = spline(t,p,w); case 'exponential' c = polyfit(log(t),log(p),1); y = exp(polyval(c,log(x))); z = exp(polyval(c,log(w))); end h=plot(t,p,"or",x,y,"-",w,z,"og"); set(h,"linewidth",2); z