fprintf ( '\n' );
fprintf ( 'CALIFORNIA:\n' );
fprintf ( '  Suppose that every year, 30%% of California moves out\n' );
fprintf ( '  and 10%% of the US moves into California.\n' );
fprintf ( '  Use this to predict the yearly California population.\n' );
fprintf ( '\n' );
fprintf ( '  Year     CA Pop     US pop      Total\n' );
fprintf ( '\n' );

m = 21;
ca = zeros ( 1, m );
us = zeros ( 1, m );

for i = 1 : m

  year = 1959 + i;

  if ( year == 1960 )
    ca(i) =  16000000;
    us(i) = 164000000;
  else
    [ ca(i), us(i) ] = california_update ( ca(i-1), us(i-1) );
  end

  fprintf ( '  %4d  %9.0f  %9.0f  %9.0f\n', year, ca(i), us(i), ca(i) + us(i) );

end

plot ( 1959+1:1959+m, ca, 1959+1:1959+m, us, 'Linewidth', 3 );
grid on
xlabel ( '<-- Year -->' );
ylabel ( '<-- California Population -->' );
title ( 'California population model', 'Fontsize', 24 );
legend ( 'CA', 'US' );
print ( '-djpeg', 'california.jpg' );     

function [ ca, us ] = california_update ( ca, us )
%
%% CALIFORNIA_UPDATE updates the California and US populations for one year.
%
%  Discussion:
%
%    For a while in the 1960's, the following statement was approximately true:
%
%   "Every year, 30% of the population of California leaves the state, and
%    every year, 10% of the population of the other states moves to California."
%
%    1) The statement sounds nonsensical.  Can we write down some equations
%       that give us numbers we can think about?
%
%    2) If 30% move out, and 10% move it, does this mean California is
%       gradually going to have no population at all?
%
%    3) If this behavior lasts long enough, does the population curve
%       of California look chaotic, go towards infinity, become negative, 
%       or oscillatory, or does it settle down?
%
%    We can make some simplifying assumptions:
%
%    * Assume that the 30% and 10% values are exact and don't change.
%
%    * The population estimates will be real numbers with fractional parts,
%      but we'll just accept figures like a population of 10,000.73
%
%    * Find the populations of California and the US in 1960, and assume
%      that the total population never changes after that, but just
%      moves back and forth.
%
%    Our goal will be simply to compute the populations of California and
%    the US (minus California), for each year, from 1960 onwards, and to
%    plot the California population, and to compute it long enough that
%    we feel we understand the pattern, if any.
%
%  Parameters:
%
%    Input/output, real CA, US.  On input, the populations of California
%    and the rest of the US at the end of the previous year.  On output, 
%    the populations one year later.
%
  ca_old = ca;
  us_old = us;

  us = 0.90 * us_old + 0.30 * ca_old;
  ca = 0.10 * us_old + 0.70 * ca_old;

  return
end

