how to plot a method in matlab

I'm trying to plot a method i defined for calculating the probability of n people having different birthday which has the following code:
function y = d_birthday( n )
year = 365;
y=1;
for i=0:n-1
y = y*((year-i)/year);
end
end
and in the command line I'm defining a vector variable x to hold values from 1:100
x=1:100;
but when I try to plot my method using x using this statement
plot(x,d_birthday(x))
all my values exhibit the same value, how do I fix it so that each value of x has it own value

回答(1 个)

year = 365;
y=ones(year,1);
for i=2:year
y(i) = y(i-1)*((year-i+1)/year);
end

3 个评论

doesn't this treat each input as a vector in itself? for example if x=1:10 then each x will have as many values as it's value so x=10 will have 10 values, which is causing me to have the following error
Vectors must be the same lengths.
can you show me how to plot your method?
x = (1:year)';
plot(x,y,'y-')
or
x = 1:100
plot(x,y(1:100),'y-')
The vectors you plot must be of the same size.
Or just plot(y, 'y-') . When the x are 1:length(y) then you can omit the x.

此问题已关闭。

标签

关闭:

2021-8-20

Community Treasure Hunt

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

Start Hunting!

Translated by