fprintf ( '\n' );
fprintf ( 'Simulate 50 weather transitions between Sunny/Cloudy/Rainy\n' );
fprintf ( '\n' );

for i = 0 : 50
  if ( i == 0 )
    today = 1;
  else
    today = weather_today ( today );
  end
  fprintf ( '%c', today );
end
fprintf ( '\n' );

function today = weather_today ( yesterday )

%% WEATHER_TODAY simulates today's weather given yesterday's weather.
%
%  The simulation is based on previously determined transition data.
%
  P = [ 4/8, 2/9, 2/7; ...
        3/8, 2/9, 3/7; ...
        1/8, 5/9, 2/7 ];

  C = cumsum ( P );

  r = rand ( );

  for i = 1 : 3
    if ( r < C(i,yesterday) )
      today = i;
      break;
    end
  end


  return
end
