Write a MATLAB code that will generate a random variable whi
Write a MATLAB code that will generate a random variable, which can take only five possible integer values 10,11,12,13, and 14. Generate a sequence of outcomes of 100 random trials. Count how many times 10 and 14 are generated out of the 100 outcomes. How can you estimate the probability of 10 and 14 in this case? Repeat the experiment with a 1 000, 5000 and 10000 outcomes generated. Estimate the probabilities in each case. What do you notice?
Thank you for helping :)
Solution
prompt = \'How many samples do you want to consider\';
n = input(prompt);
count1=0; % to calculate number of 10
count2=0; % to calculate number of 14
numbers = {10,11,12,13,14};
sampleList = []; % Initialize array/vector
for i=1:n
x = numbers(randperm(length(numbers),1))
sampleList(i)=x;
end
%To count 10 & 14
for i=1:n
if sampleList(i)==10
count1=count1+1;
elseif sampleList(i)==14
count2=count2+1;
end
prob_1 = count1/n; % probablity of occurance of 10
prob_2 = count2/n; % probablity of occurance of 14
X=[\'10 is generated\',count1,\' times & 14 is generated \',count2, \'times\' ];
Z=[\'Probablity of occurance of 10 is\',prob_1,\' & Probablity of occurance of 14 is\',prob_2];
disp(X); %for displaying purpose
disp(Z);
------------------------------------------------------------------------------
By increasing number of outcomes generated,we get that probablity value gets more exact as number of samples has increased.
