hw3_solns

# hw3_solns - ME 17 Spring 2007 Homework #3 Solutions Problem...

This preview shows pages 1–2. Sign up to view the full content.

ME 17 Spring 2007 1 / 5 Homework #3 Solutions Problem 1 (20 pts) (a) [15 pts] Write a Matlab program to determine (probabilistically – i.e. Monte Carlo method) whether it’s better to stick with Door 1 or switch to the other (unopened) door. % Goatprob.m: Use the Monte Carlo method to decide the best way to win (car). % Initialize variables: numtries = 1000; % number of trials for the Monte Carlo simulation numcorrectstick = 0; % counts # of times sticking with door 1 gets the car numcorrectswitch = 0; % counts # of times switching doors gets the car for k = 1:numtries % loop over ( numtries ) trials % Setup this game: Determine which door has the car: rcorrect = rand; % picks a random number between 0 and 1. % Assign the car to a door based on the random number generated, with % equal probability of the car being behind each door: if rcorrect <= 1/3 % if the car is behind the first door, set variable correct = 1; % to show that the winning door is door 1. elseif (rcorrect > 1/3) & (rcorrect < 2/3) correct = 2; % car is behind door 2 else correct = 3; % car is behind door 3 end % Figure out which strategy wins the car this game. If you win the car by % staying with Door 1, increment numcorrectstick by 1. If not, increment % numcorrectswitch . guess = 1; % guess which door the car is behind (WOLOG choose Door 1) if guess == correct % If the car is behind the door you guessed (Door 1) numcorrectstick = numcorrectstick + 1; else % The car was behind the other door numcorrectswitch = numcorrectswitch + 1; end % End of this game. Loop around to play the next game. end % Calculate the probability of winning using each strategy: probstickcorrect = numcorrectstick/numtries probswitchcorrect = numcorrectswitch/numtries % Note: WOLOG = “Without Loss Of Generality” Your results will vary a little from run to run. The first time I ran the program I got:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This homework help was uploaded on 04/07/2008 for the course ME 17 taught by Professor Milstein during the Spring '07 term at UCSB.

### Page1 / 5

hw3_solns - ME 17 Spring 2007 Homework #3 Solutions Problem...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online