User Tools

Site Tools


gibson:teaching:fall-2012:math445:sickcat1.m
% Problem 1. Suppose my cat gets sick one day out of seven. 
% How likely is she to get sick two days in one week?

% Here's a straightforward, lengthy way to estimate the probability

% Put all the constants up front
Ntrials  = 1000; 
probsick = 1/7;
count = 0;

for n = 1:Ntrials
  
  random = rand(7,1); % get seven random numbers between 0 and 1
  
  % count up the number of times the cat is sick in week
  sickdays = 0;
  for d = 1:7
    % if the random number is less than psick, cat is sick.
    if random(d) < probsick
      sickdays = sickdays + 1;
    end
  end
  
  % if the cat's been sick twice this week, increment the count
  if sickdays == 2
    count = count + 1;
  end
  
end

prob = count/Ntrials; 

fprintf('Prob of two sick days per week is %f\n', prob);
    
gibson/teaching/fall-2012/math445/sickcat1.m.txt · Last modified: 2012/09/27 13:43 by gibson