Home > MATLAB, Simulation > Simulating M/M/1 queue in MATLAB

Simulating M/M/1 queue in MATLAB

M/M/1 can be modeled in MATLAB using Discrete Event simulation.

The arrival rate is \lambda and the service time is \mu.

The utilization \rho = \frac{\lambda}{\mu}

The interarrival times and the services times are exponentially distributed.  A vector of exponentially distributed interarrival and service times are generated.

To generate exponential random vector, a transformation on uniform random variables can be used. To generate an exponentially distributed random variable \emph{X} with parameter \lambda, we need to convert the expression

u = {F_X}\left( x \right) = 1 - {e^{ - \lambda x}}.

U =1 - {e^{ - \lambda X}}.

X=-\frac{1}{\lambda }\ln \left( 1-U \right)

This is equivalent to X = - \frac{1}{\lambda } \ln \left( U \right), since 1- U is uniformly distributed between 0 and 1

%generate two vector of random numbers with uniformly distribution
seed1 = RandStream.create(‘mcg16807′,’Seed’,5);
U1 = rand(seed1, 1, NSamples);
seed2 = RandStream.create(‘mcg16807′,’Seed’,6);
U2 = rand(seed2, 1, NSamples);

% service time vector
S = -1/mu*log(u1) * 1E3;
% inter-arrival time vector
tau = -1/lambda*log(u2) * 1E3;

Arrival and Departure Times
The arrival time is Poisson distributed and it is the sums of cumulative sum of the exponential random variables.

The Departure time for the first customer is
departure~time = arrival~time + service~time

The departure times for subsequent customers is
departure~time = max\left(arrival~time, departure~time(previous~user)\right) + service~time

The expected delay is
delay~time = waiting~time + service~time

% the arrival time vector
T = cumsum(tau);
% the departure time vector
D = zeros(NSamples, 1);
D(1) = S(1) + tau(1);

% the waiting time vector
W = zeros(NSamples, 1);
W(1) = 0;
for i = 2:NSamples
D(i) = max(T(i), D(i-1)) + S(i);
W(i) = max(D(i-1)-T(i), 0);
% the delay time vector – in–queue wait time and service time
Delay_Time = zeros(NSamples, 1);
for i = 1:NSamples,
Delay_Time(i) = W(i) + S(i);

W is a vector that contains the waiting time.

Delay_Time is a vector that contains the time spent by a packet in the system

For more on MM1 and queuing systems in general check

  1. BERTSEKAS, D. P., & GALLAGER, R. G. (1992). Data networks. Englewood Cliffs, N.J., Prentice Hall.
  2. LEON-GARCIA, A., & LEON-GARCIA, A. (2008). Probability, statistics, and random processes for electrical engineering. Upper Saddle River, NJ, Pearson/Prentice Hall.
Categories: MATLAB, Simulation
  1. No comments yet.
  1. February 22, 2013 at 8:58 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: