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

for i = 0 : 50
  if ( i == 0 )
    today = 'S';
  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.
%
  r = rand ( );

  if ( yesterday == 'S' )
    if ( r < 4.0 / 8.0 )
      today = 'S';
    elseif ( r < 7.0 / 8.0 )
      today = 'C';
    else
      today = 'R';
    end
  elseif ( yesterday == 'C' )
    if ( r < 2.0 / 9.0 )
      today = 'S';
    elseif ( r < 4.0 / 9.0 )
      today = 'C';
    else
      today = 'R';
    end
  elseif ( yesterday == 'R' )
    if ( r < 2.0 / 7.0 )
      today = 'S';
    elseif ( r < 5.0 / 7.0 )
      today = 'C';
    else
      today = 'R';
    end
  end

  return
end
