# Need help finding out if I wrote this code correctly

1 view (last 30 days)
Kevin Nelson on 5 Sep 2022
Edited: Star Strider on 5 Sep 2022
N = 365;
n = (1:80);
for M = 1:1000
j = (1:n);
pn = 1-(1-((n-1)/N));
end
%%
fig = figure(1); clf
plot(pn)
xlabel('Value')
ylabel('Probability')
ylim(x,0.3)
What I'm trying to do is perform an experiment on (pn) 1000 times, with a value (n) from 1 through 80. I'm supposed to be getting 0.5 at a value greater than 22 but I'm not getting that. First time using matlab so help would be very much appreciated.
##### 1 CommentShow -1 older commentsHide -1 older comments
Kevin Nelson on 5 Sep 2022
What I'm trying to do is perform an experiment on (pn) 1000 times, with a value (n) from 1 through 80. I'm supposed to be getting 0.5 at a value greater than 22 but I'm not getting that. First time using matlab so help would be very much appreciated.

Sign in to comment.

### Answers (2)

Star Strider on 5 Sep 2022
Edited: Star Strider on 5 Sep 2022
If you want to save the values of ‘pn’ in the loop, subscript it —
pn(M) = 1-(1-((n-1)/N));
That will create a row vector that you can then plot.
What is the ‘j’ calculation supposed to do? The code calculates it in each iteration and never uses it (at least not that I can see).
##### 2 CommentsShow NoneHide None
Walter Roberson on 5 Sep 2022
n is a vector, so you would have to assign to either pn(M,:) or pn(:,M)
Star Strider on 5 Sep 2022
Edited: Star Strider on 5 Sep 2022
I just now noticed that.
EDIT — (5 Sep 2022 at 13:16)
I believe this is the ‘Birthday Problem’ the solution of which is simply:
N = 365;
V = 1:N;
pn = cumprod((N:-1:1) / N);
Result = [V; pn]
Result = 2×365
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000 23.0000 24.0000 25.0000 26.0000 27.0000 28.0000 29.0000 30.0000 1.0000 0.9973 0.9918 0.9836 0.9729 0.9595 0.9438 0.9257 0.9054 0.8831 0.8589 0.8330 0.8056 0.7769 0.7471 0.7164 0.6850 0.6531 0.6209 0.5886 0.5563 0.5243 0.4927 0.4617 0.4313 0.4018 0.3731 0.3455 0.3190 0.2937
Eq = find(pn >= 0.5, 1, 'last')
Eq = 22
I don’t understand the reason that it needs to be simulated 1000 times unless some different sort of calculation is involved.
.

Sign in to comment.

Walter Roberson on 5 Sep 2022
n = (1:80);
n is a vector.
for M = 1:1000
j = (1:n);
n is a vector, so j is 1:VECTOR instead of being 1:SCALAR or 1:SCALAR:SCALAR . You have a logic error there. What MATLAB does in this particular case is to define the output as if you had written 1:VECTOR(1) . Since n = 1:80 then n(1) is 1, so you are effectively defining j = 1:1 which is the same as j = 1
pn = 1-(1-((n-1)/N));
but you never use j in the calculation or afterwards, so there is no point calculating it.
You overwrite all of pn in the calculation. The end result is the same if you had only done the last iteration, when M = 1000.
You did not include any comments, so we cannot tell what you are trying to calculate, so we cannot suggest what correct code might be. (But I get the impression that possibly you are testing out the "Birthday Paradox" ?)
##### 4 CommentsShow 2 older commentsHide 2 older comments
Kevin Nelson on 5 Sep 2022
Do I set M = rand to have it calculate with random numbers? How do I specifically let it calculate each experiment 1000 times?
Walter Roberson on 5 Sep 2022
It is not very meaningful to talk about probability without random numbers, or at least without cdf or pdf formulas.
1-(1-((n-1)/N))
1-(n-1)/N is 1/N
1 - 1/N is (n-1)/N
Therefore your current formula is calculating the same thing as (n-1)/N
If you were doing something like birthday paradox, you would be doing a calculation similar to
1-(1-((n-1)/N).^POWER)
for appropriate POWER -- if, that is, you were calculating based on the closed-form solution, rather than doing it as an "experiment".

Sign in to comment.

### Categories

Find more on Birthdays in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!