Archive for the ‘MATLAB’ Category

Creating N Random points in a Hexagon

December 25, 2009 1 comment

In wireless communication there is a concept in which frequencies are allocated to an area in a regular pattern of areas, called ‘cells’. In modeling and simulation the hexagonal cell is usually used. The following routine describes how random nodes can be created in the cell area. The nodes created are random enough for all practical purposes.

In the code, I will create a hexagon centered at (0,0) with radius R. The snipplets can be used in mobile capacity predicts and general systems level simulation of cellular networks.

N = 40;     %Number of users
R = 10;     %Radius of Hexagon

Define the vertexes of the hexagon. They for angles 0, 60, 120, 180, 240 and 300 withe origin.
v_x = R * cos((0:6)*pi/3);
v_y = R * sin((0:6)*pi/3);

The method used here is to generate many points in a square and choose N points that fall within the hexagon

%Generate 3N random points with square that is 2R by 2R
c_x = R – rand(1, 3*N)*2*R;
c_y = R – rand(1, 3*N)*2*R;

There is a command in MATLAB inploygon.

The command finds points within a polygon region.

%get the points within the polygon
IN = inpolygon(c_x, c_y, v_x, v_y);

%drop nodes outside the hexagon
c_x = c_x(IN);
c_y = c_y(IN);

%choose only N points
idx = randperm(length(c_x));
c_x = c_x(idx(1:N));
c_y = c_y(idx(1:N));

plot(c_x, c_y, ‘r*’)
hold on
axis square
hold off

Categories: MATLAB, Simulation, Wireless

Simulating M/M/1 queue in MATLAB

December 24, 2009 1 comment

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