how to plot a method in matlab
Infos
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Afficher commentaires plus anciens
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
Réponses (1)
Roger Stafford
le 19 Fév 2017
Modifié(e) : Roger Stafford
le 19 Fév 2017
year = 365;
y=ones(year,1);
for i=2:year
y(i) = y(i-1)*((year-i+1)/year);
end
3 commentaires
raed khader
le 19 Fév 2017
Modifié(e) : raed khader
le 19 Fév 2017
Roger Stafford
le 19 Fév 2017
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
le 20 Fév 2017
Or just plot(y, 'y-') . When the x are 1:length(y) then you can omit the x.
Cette question est clôturée.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!