user1049018
user1049018

Reputation:

Plotting a graph using the values generated

I have created a queue buffer and need to generate a plot of the number of packets in the queue over time. I am a beginner and cannot find ways to store the number of packets in the buffer at each time after executing the while loop and therefore when I run the program it just gives a plot of the number of packets in queue at the termination of loop. Can anyone please suggest a way of getting a plot of 'Q' over time at every stage of the execution of loop. I have provided the code below.

%Queue Buffer%
Q=0;%queue length
s=10;%Number of packets departing from queue at each time
Q2=0;%New queue length
Ti=0;%Number of times packets arrive
while Ti<=20
   Q=0+Q2;
   a= randi(32,1,1);
   a1=a-s;
   a2=Q+a1;
   Q2= max(0,a2);
   Ti=Ti+1;
end
    t=0:1:100;
    plot (t,Q,'o')

Upvotes: 0

Views: 231

Answers (1)

Stefan Gretar
Stefan Gretar

Reputation: 178

Easy way

Q = [];
%For loop start
Q = [Q Q2];
%End for loop

Better way

Q = zeros(1,101); %Since you are plotting from 0:1:100. (Default step is 1, so 0:1:100 = 0:100)
cnt = 1;
%For loop start
Q(cnt) = Q2;
cnt = cnt + 1;
%End for loop

Upvotes: 1

Related Questions