hold on is plotting strange lines to 0 when used for this code. Any ideas why?

gamma=1; alpha=0; beta=0; N=100; x0=50;
xn=zeros(1,N); xn(1)=x0;
realisations=1;
for i=1:realisations
hold on
for j=1:N-1
pn = (((gamma*xn(j)*(1-alpha))+(N-xn(j))*beta)/(gamma*xn(j)*(1-alpha)+...
(N-xn(j))*beta+(N-xn(j))*(1-beta)+gamma*xn(j)*alpha))*(xn(j)<=N);
xn(j+1)=binornd(N,pn);
plot(xn)
axis([-10 N+10 -10 N+10])
end
end
Which produces a plot like this:
I was hoping someone could help as to why it plots these lines to 0.
Thanks

1 commentaire

James McCusker
James McCusker le 31 Oct 2017
Modifié(e) : James McCusker le 31 Oct 2017
Sorry I should have changed the value of realisations in the code provided to something greater than 1 - I intended to plot multiple realisations of xn in the same plot.

Connectez-vous pour commenter.

 Réponse acceptée

gamma=1; alpha=0; beta=0; N=100; x0=50;
xn=zeros(1,N); xn(1)=x0;
realisations=1;
for i=1:realisations
hold on
for j=1:N-1
pn = (((gamma*xn(j)*(1-alpha))+(N-xn(j))*beta)/(gamma*xn(j)*(1-alpha)+...
(N-xn(j))*beta+(N-xn(j))*(1-beta)+gamma*xn(j)*alpha))*(xn(j)<=N);
xn(j+1)=binornd(N,pn);
end
end
plot(xn)
axis([-10 N+10 -10 N+10])

4 commentaires

Sorry I forgot to mention that I need multiple realisations on the same plot hence using hold on and plot inside the for loop.
gamma=1; alpha=0; beta=0; N=100; x0=50;
xn=zeros(1,N); xn(1)=x0;
realisations=1;
for i=1:realisations
hold on
for j=1:N-1
pn = (((gamma*xn(j)*(1-alpha))+(N-xn(j))*beta)/(gamma*xn(j)*(1-alpha)+...
(N-xn(j))*beta+(N-xn(j))*(1-beta)+gamma*xn(j)*alpha))*(xn(j)<=N);
xn(j+1)=binornd(N,pn);
end
plot(xn)
end
hold off
axis([-10 N+10 -10 N+10])
Thank you so much! Do you know why that solved the problem? Just curious
You were redrawing at each step with the complete xn, including all of the 0's you had initialized with. After j = 1 only xn(1:2) had non-zero values so it had to drawn a return to 0 to plot all of xn after the second value. After j = 2, only xn(1:3) had non-zero values so it had to draw a return to 0 after the third value to plot all xn... and so on.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Object Properties dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by