-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex1_ch2.m
71 lines (62 loc) · 1.65 KB
/
ex1_ch2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
% Chrysa Tsimperi
% Data Analysis 2021
% Chapter 2 Excerise 1
% Generation of discrete uniform data and computation of the occurrence of a discrete value.
% Flipping coin
close all;
clc;
clear;
n = [10 100 500 1000 2500 5000 7500 10000];
N = categorical(n);
% Flipping coin - Uniformly distributed random numbers
numofHeads = zeros(length(n),1);
numofTails = zeros(length(n),1);
tailsRatio = zeros(length(n),1);
for j = 1:length(n)
heads = 0;
tails = 0;
% Flip the coin n times
for i = 1:n(j)
x = rand;
if x < 0.5
heads = heads + 1;
else
tails = tails + 1;
end
end
numofHeads(j) = heads;
numofTails(j) = tails;
tailsRatio(j) = numofTails(j)/n(j);
end
figure(1);
bar(N,tailsRatio);
xlabel('Number of iterations (n) of flipping coin');
ylabel('Tails ratio in n iterations')
ylim([0 1])
%--------------------------------------------------------------------------
% Flipping coin - Random numbers from discrete uniform distribution
% 1 for heads, 2 for tails
numofHeads = zeros(length(n),1);
numofTails = zeros(length(n),1);
tailsRatio = zeros(length(n),1);
for j = 1:length(n)
heads = 0;
tails = 0;
% Flip the coin n times
for i = 1:n(j)
x = unidrnd(2);
if x == 1
heads = heads + 1;
else
tails = tails + 1;
end
end
numofHeads(j) = heads;
numofTails(j) = tails;
tailsRatio(j) = numofTails(j)/n(j);
end
figure(2);
bar(N,tailsRatio);
xlabel('Number of iterations (n) of flipping coin');
ylabel('Tails ration in n iterations')
ylim([0 1])