Attachment 'Aufgabe3.m'
Download 1 function Aufgabe3
2 %Aufgabe3 - Simulate Monty-Hall-Problem and plot empirical evidence.
3 %
4 %usage
5 % Aufgabe3
6 %
7 %description
8 % Aufgabe3 simulates the Monty-Hall-Problem 10000 times and plots are pie-chart
9 % which indicates whether it is wise to switch doors or not.
10 %
11 %author
12 % buenau@cs.tu-berlin.de
13
14 N = 10000;
15
16 who_won = zeros(2, N);
17
18 for i=1:N
19 % Put price behind a random door.
20 price = zeros(1,3);
21 price(randint(1, 1, [1 3])) = 1;
22
23 % Strategy 1: Choose door at random.
24 strat1_sel = randint(1, 1, [1 3]);
25
26 % Strategy 2: Switch to other door.
27 host_opens = randomchoice(setdiff(1:3, [ strat1_sel find(price) ]));
28 strat2_sel = randomchoice(setdiff(1:3, [ strat1_sel host_opens ]));
29
30 who_won(1, i) = price(strat1_sel);
31 who_won(2, i) = price(strat2_sel);
32 end
33
34 pie(sum(who_won, 2) / N, { 'Strategy 1: Do not change', 'Strategy 2: Change doors' });
35 title(sprintf('Frequency of wins for the two strategies in %d simulations', N));
36
37 % Choose an element of vector a at random.
38 function c = randomchoice(a)
39 c = a(randint(1, 1, [1 length(a)]));
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.