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 件)

Roger Stafford
Roger Stafford 2017 年 2 月 19 日
編集済み: Roger Stafford 2017 年 2 月 19 日

0 投票

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

3 件のコメント

raed khader
raed khader 2017 年 2 月 19 日
編集済み: raed khader 2017 年 2 月 19 日
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?
Roger Stafford
Roger Stafford 2017 年 2 月 19 日
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.
Walter Roberson
Walter Roberson 2017 年 2 月 20 日
Or just plot(y, 'y-') . When the x are 1:length(y) then you can omit the x.

この質問は閉じられています。

タグ

質問済み:

2017 年 2 月 19 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by