Filter löschen
Filter löschen

Variable simulation not being able to be graphed.

2 Ansichten (letzte 30 Tage)
Kiran Sai
Kiran Sai am 2 Mai 2023
Kommentiert: Kiran Sai am 3 Mai 2023
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

Antworten (2)

LeoAiE
LeoAiE am 2 Mai 2023
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

Walter Roberson
Walter Roberson am 2 Mai 2023
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 Kommentare
Kiran Sai
Kiran Sai am 3 Mai 2023
Is there a way to convert the scald into a array?
Walter Roberson
Walter Roberson am 3 Mai 2023
Bearbeitet: Walter Roberson am 3 Mai 2023
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.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by