Variable simulation not being able to be graphed.

조회 수: 2 (최근 30일)
Kiran Sai
Kiran Sai 2023년 5월 2일
댓글: Kiran Sai 2023년 5월 3일
I was working on some piece of code that simulates the birthday paradox based on the user selected sample size and the number of realizations that the user wants. I'm a little bit confused as the variable 'Simulation' only produces one probablity value not a array of probablity values. Therefore the exponential graph can't be sketched.
Here's the code:
disp('Welcome to the Birtday Paradox')
ready = false;
while ~ ready
% User inputs and defining variables
birthday_repeats = input('select the number of birthday repeats from 10-500:');
if birthday_repeats > 500 || birthday_repeats < 10 || isempty(birthday_repeats) || round(birthday_repeats) ~= birthday_repeats|| isnan(birthday_repeats)
disp('error')
continue
end
sample_size = input('Select the sample size from 2-365: ');
if sample_size > 365 || sample_size < 2|| isempty(sample_size) || round(sample_size) ~= sample_size || isnan(sample_size)
disp('error')
continue
end
N = 1000;
days = randi(365,1,365);
tally = zeros(1,sample_size);
% simulation run
for k = 2:sample_size
match = 0;
for j = 1:birthday_repeats% desired number of trials
birthdays = randi(365,1,k);
if test(days)
match = k + 1;
end
end
Simulation = tally(k) - match/birthday_repeats;
end
% plotting graphs
domain = 2:sample_size;
figure(1)
plot(domain,Simulation)
xlabel('Number of people Chosen')
title('Birthday Paradox simulation')
ylabel('Probability')
fprintf('the probablity is %.3f\n',Simulation)
break
end
Here's the code for the user defined function 'test'
function out = test(data)
out = false;
n = length(data);
for k = 1:n
for i = k+1:n
if data(k) == data(i)
out = true;
return
end
end
end
end

답변 (2개)

LeoAiE
LeoAiE 2023년 5월 2일
The main change I made was to update the tally(k) value inside the for loop instead of assigning it at the end. This way, the tally array is populated with the probability values for each sample size.
disp('Welcome to the Birtday Paradox');
ready = false;
while ~ ready
% User inputs and defining variables
birthday_repeats = input('select the number of birthday repeats from 10-500:');
if birthday_repeats > 500 || birthday_repeats < 10 || isempty(birthday_repeats) || round(birthday_repeats) ~= birthday_repeats || isnan(birthday_repeats)
disp('error')
continue
end
sample_size = input('Select the sample size from 2-365: ');
if sample_size > 365 || sample_size < 2|| isempty(sample_size) || round(sample_size) ~= sample_size || isnan(sample_size)
disp('error')
continue
end
N = 1000;
days = randi(365,1,365);
tally = zeros(1,sample_size);
% simulation run
for k = 2:sample_size
match = 0;
for j = 1:birthday_repeats % desired number of trials
birthdays = randi(365,1,k);
if test(birthdays)
match = match + 1;
end
end
tally(k) = match/birthday_repeats;
end
% plotting graphs
domain = 2:sample_size;
figure(1)
plot(domain,tally(2:end))
xlabel('Number of people Chosen')
title('Birthday Paradox simulation')
ylabel('Probability')
fprintf('the probability array is:\n');
disp(tally(2:end))
break
end
function out = test(data)
out = false;
n = length(data);
for k = 1:n
for i = k+1:n
if data(k) == data(i)
out = true;
return
end
end
end
end
  댓글 수: 1
Kiran Sai
Kiran Sai 2023년 5월 3일
Is it supposed to give a linear graph as an output?

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2023년 5월 2일
Simulation = tally(k) - match/birthday_repeats;
tally(k) is a scalar. So are match and birthday_repeats. So the right hand side of the assignment is a scalar, and that means that the output will be a scalar -- not something that is the same size as 2:sample_size
  댓글 수: 2
Kiran Sai
Kiran Sai 2023년 5월 3일
Is there a way to convert the scald into a array?
Walter Roberson
Walter Roberson 2023년 5월 3일
편집: Walter Roberson 2023년 5월 3일
Sure. Assign to Simulation(k-1) instead of to the entire array Simulation
The -1 part is because k starts at 2 not at 1, and when you use Simulation later, you are not indexing it -- so the first output of Simulation (offset 1) has to correspond to k = 2.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Birthdays에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by