Professional Documents
Culture Documents
Lab 3 Simulating Discrete Random Variable: Object: Code
Lab 3 Simulating Discrete Random Variable: Object: Code
f=cumsum(px); 0.8
figure(1); 0.7
clf; 0.6
Output:
0
0 1 2 3 4 5 6 7 8 9 10
Object:
Simulate dice throw.
Code:
num_experiments=1000
count=zeros(1,6)
outcomes=[1,2,3,4,5,6]
for i=1:num_experiments
dice_throw=ceil(6*rand(1))
for j=1:6
if(dice_throw==outcomes(j))
count(j)=count(j)+1
end
end
end
Object: 0.25
Discrete pdf
This is probability of X
stem(X,pdf); 0.15
Object:
Plotting cdf.
Code: 1
X = [1 2 3 4 5 6]; 0.9
cdf=cumsum(pdf); 0.7
stairs(X(1:1:6),cdf); 0.6
Output: 0.4
0.3
0.2
0.1
0
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6
Object:
Plotting histogram.
Code:
X = [1 2 3 4 5 6];
pdf =[0.2 0.14 0.22 0.16 0.17 0.11];
for i = 1:100
die_throw=rand(1)
X(1,i) =min(find(die_throw<cdf))
end
hist(X);
Output:
25
20
15
10
0
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6