please why i cant get a figure ( it give me an empty figure)

L=16;
for n=1:100:500
Pidle=(1-(1/L))^n;
Psucc = (n*(1-1/L)^(n-1)) ;
Pcoll=(1-Pidle0-Psucc0);
c= L*(1-1/L)^n;
end
plot(n,c);

Answers (2)

When you have a "for" loop, such as
for n = 1 : 100 : 500
then after the "for" loop, the controlling variable ("n") is left at the value that it had in the last iteration of the loop. In your code that means that at the end of the loop, "n" will be 500.
When you have an unindex assignment in a "for" loop, such as
for n = 1 : 100 : 500
c = L*(1-1/L)^n;
end
then that variable will get overwritten completely on every iteration of the "for" loop. At the end of the "for" loop, the variable will have only the value that it had on the last iteration.
So at the end of your "for" loop, n will be a scalar, and c will be a scalar, and you are plotting with just that one point. The plot is not empty, but it only has one point to draw.
Look at the first example

4 Comments

so what i must change in my code?
Have you looked into the first example?
s = 10;
H = zeros(s);
for c = 1:s
for r = 1:s
H(r,c) = 1/(r+c-1);
end
end
Pay attention to H(r,c) = .. part of the code.
yes i correct that but it doesnt work :/ # % L=500; s = 16; Pidle = zeros(s); Psucc=zeros(s);
Pcoll=zeros(s);
for n =1: s
for L =1: s
Pidle(L,n)=(1-(1/L))^n;
Psucc(L,n) = (n*(1-(1/L))^(n-1)) ;
Pcoll=(1-Pidle-Psucc);
end
end
plot(n,Pcoll); #
nvals = 1:100:500;
numn = length(nvals);
c = zeros(numn, 1);
for K = 1 : numn
n = nvals(K);
Pidle=(1-(1/L))^n;
Psucc = (n*(1-1/L)^(n-1)) ;
Pcoll=(1-Pidle0-Psucc0);
c(K) = L*(1-1/L)^n;
end
plot(nvals, c);

Sign in to comment.

It would be much simpler to vectorize the whole code (no loops), like this:
L=16;
N = 1:100:500;
Pidle = (1-(1/L)).^N;
Psucc = N.*(1-1/L).^(N-1);
Pcoll = 1-Pidle-Psucc;
C = L*(1-1/L).^N;
plot(N,C)
which produces this plot:
Although note that your code calculates Pcoll, these values are never used.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

No tags entered yet.

Asked:

on 3 Jun 2015

Commented:

on 3 Jun 2015

Community Treasure Hunt

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

Start Hunting!