How to generate figure1 and delay from this code

% N is the number of nodes in a fully-connected network
N=5;
% W is the contention window size
W=128;
% m is the length of the active period in a cycle (unit: 0.1 ms)
m=285.6;
% Q is the maximum queue size at each node
Q=10;
% lamda is the data arrival rate in the unit of # pkts per ms
lamda=15/10000;
% resolution is the resolution for numerical calculation of p and \pi_0
resolution=0.001;
M=1/resolution;
figure(1)
% Pi_1(i,j) = F[p_1(i,j)] according to the Markov model
% i = 1 -> duty cycle = 10%
% i = 2 -> duty cycle = 30%
% i = 3 -> duty cycle = 50%
% i = 4 -> duty cycle = 70%
% i = 5 -> duty cycle = 90%
p_1=zeros(5,M+1);
Pi_1=zeros(5,M+1);
% p_2(j) = G[Pi_2(j)] according to the media access rules of S-MAC
p_2=zeros(1,M+1);
Pi_2=zeros(1,M+1);
% ps_2(j) is the probability of successfully transmiting a packet at each node
% in a cycle
ps=zeros(1,M+1);
pf=zeros(1,M+1);
p=ps+pf;
% b is the duty cycle
b=-0.1;
for l=1:5l
b=b+0.2;
% T is the cycle lengh
T=m*(1-b)/b+1+m; % unit: 0.1 ms
T=T*0.1 % unit: ms
% px (x=0..50) is the probability that x pkts arrive at a node in a cycle
p0=exp(-lamda*T)*(lamda*T)^0/1;
p1=exp(-lamda*T)*(lamda*T)^1/1;
p2=exp(-lamda*T)*(lamda*T)^2/2;
p3=exp(-lamda*T)*(lamda*T)^3/6;
p4=exp(-lamda*T)*(lamda*T)^4/24;
p5=exp(-lamda*T)*(lamda*T)^5/120;
p6=exp(-lamda*T)*(lamda*T)^6/720;
p7=exp(-lamda*T)*(lamda*T)^7/(720*7);
p8=exp(-lamda*T)*(lamda*T)^8/(720*56);
p9=exp(-lamda*T)*(lamda*T)^9/(720*7*8*9);
p10=exp(-lamda*T)*(lamda*T)^10/(720*7*8*9*10);
% ppxy (x=0..10, y=x+1) is the probability that no less than x pakcets arrive at a node in a cycle
pp01 =1;
pp12 =1-p0;
pp23 =1-p0-p1;
pp34 =1-p0-p1-p2;
pp45 =1-p0-p1-p2-p3;
pp56 =1-p0-p1-p2-p3-p4;
pp67 =1-p0-p1-p2-p3-p4-p5;
pp78 =1-p0-p1-p2-p3-p4-p5-p6;
pp89 =1-p0-p1-p2-p3-p4-p5-p6-p7;
pp910 =1-p0-p1-p2-p3-p4-p5-p6-p7-p8;
pp1011=1-p0-p1-p2-p3-p4-p5-p6-p7-p8-p9;
num1=0;
for p=0:resolution:1
% A is the transition matrix of the Markov model
A=zeros(17,17);
A=[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ps (1-p) 0 0 0 pf 0 0 0 0 0 0 0 0 0 0 0
0 ps (1-p) 0 0 0 pf 0 0 0 0 0 0 0 0 0 0
0 0 ps (1-p) 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 (1-p) 0 0 0 pf 0 0 0 0 0 0 0 0
ps 0 0 0 0 (1-p) 0 0 0 pf 0 0 0 0 0 0 0
0 ps 0 0 0 0 (1-p) 0 0 0 pf 0 0 0 0 0 0
0 0 ps 0 0 0 0 (1-p) 0 0 0 pf 0 0 0 0 0
0 0 0 0 0 0 0 0 (1-p) 0 0 0 pf 0 0 0 0
ps 0 0 0 0 0 0 0 0 (1-p) 0 0 0 pf 0 0 0
0 ps 0 0 0 0 0 0 0 0 (1-p) 0 0 0 pf 0 0
0 0 ps 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 (1-p) 0 0 0
p 0 0 0 0 0 0 0 0 0 0 0 0 0 (1-p) 0 0
0 p 0 0 0 0 0 0 0 0 0 0 0 0 0 (1-p) 0
0 0 p 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
A=A'-eye(17);
A(17,:)=ones(1,17);
B=zeros(17,1);
B(17,1)=1;
% X0 is the array of stationary probabilities for each state in the Markov model
X0=A\B; % X0=inv(A)*B
num1=num1+1;
ps_1(l,num1)=ps;
pf_1(l,num1)=pf;
Pi_1(l,num1)=X0(1);
end;
% h1 is the curve of \pi_0=F(p) according to the Markov model
h1=plot(ps_1(l,:),pf_1(l,:),Pi_1(l,:),'b');
hold on
xlabel('ps');
ylabel('pf');
zlabel('\pi _0');
title(['N=',num2str(N),' W=',num2str(W),' Q=',num2str(Q),' \lambda=',num2str(lamda*10000),'pkt/10s']);
end
% AA(i) is the probability that i-1 out of N-1 nodes in the network are
% contending the media in a cycle
AA=zeros(1,N);
M=zeros(1,N);
Msuc=zeros(1,N);
num2=0;
for pi0=0:resolution:1
for i=1:N
co=1;
for j=1:i-1
co=co*(N-j);
end;
for j=1:i-1
co=co/j;
end;
AA(i)=co*pi0^(N-i)*(1-pi0)^(i-1);
end;
% ptx is the probability that a node wins the contention when other i-1
% nodes are contending the media
ptx=0;
% ps is the probability that a node successfully transmits a packet
% when other i-1 nodes are contending the media
ps=0;
for i=1:W
for j=1:N
M(j)=((W+1-i)/W)^(j-1);
Msuc(j)=((W-i)/W)^(j-1);
end;
for j=1:N
ptx=ptx+(1/W)*M(j)*AA(j);
ps=ps+(1/W)*Msuc(j)*AA(j);
end;
end;
num2=num2+1;
p_2(num2)=ptx;
Pi_2(num2)=pi0;
ps_2(num2)=ps;
end;
% h2 is the curve of p=G(\pi_0) according to the media access rules of
% S-MAC
h2=plot(p_2,Pi_2,'r');
% Solving \pi_0=F(p) and p=G(\pi_0)
% ||
% \/
% Finding the intersection of curve (p_1,Pi_1) and curve (p_2,Pi_2)
% pointX(i) is the p of the intersection
% pointY(i) is the \pi_0 of the intersection
% pointK(i) is the corresponding ps
% i = 1..5 corresponding to 5 different duty cycles
pointX=zeros(1,5);
pointY=zeros(1,5);
pointK=zeros(1,5);
% packet delivery ratio of S-MAC under 5 different duty cycles
PDR=zeros(1,5);
% packet delay of S-MAC under 5 different duty cycles, unit: ms
D=zeros(1,5);
% energy over time of S-MAC under 5 different duty cycles, unit: W
energy=zeros(1,5);
b=-0.1;
for l=1:5
minimum=1;
b=b+0.2;
T=m*(1-b)/b+1+m;
T=T*0.1
for j=1:num1
for k=1:num2
if ((p_1(l,j)-p_2(k))^2+(Pi_1(l,j)-Pi_2(k))^2<minimum)
minimum=(p_1(l,j)-p_2(k))^2+(Pi_1(l,j)-Pi_2(k))^2;
pointX(l)=p_2(k);
pointY(l)=Pi_2(k);
pointK(l)=ps_2(k);
%-------------------- Packet Delivery Ratio -------------------------------
PDR(l)=(1-pointY(l))*ps_2(k)/(lamda*T);
end;
end;
end;
%--------------------- Delay ----------------------------------------------
p=pointX(l);
A=zeros(17,17);
A=[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ps (1-p) 0 0 0 pf 0 0 0 0 0 0 0 0 0 0 0
0 ps (1-p) 0 0 0 pf 0 0 0 0 0 0 0 0 0 0
0 0 ps (1-p) 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 (1-p) 0 0 0 pf 0 0 0 0 0 0 0 0
ps 0 0 0 0 (1-p) 0 0 0 pf 0 0 0 0 0 0 0
0 ps 0 0 0 0 (1-p) 0 0 0 pf 0 0 0 0 0 0
0 0 ps 0 0 0 0 (1-p) 0 0 0 pf 0 0 0 0 0
0 0 0 0 0 0 0 0 (1-p) 0 0 0 pf 0 0 0 0
ps 0 0 0 0 0 0 0 0 (1-p) 0 0 0 pf 0 0 0
0 ps 0 0 0 0 0 0 0 0 (1-p) 0 0 0 pf 0 0
0 0 ps 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 (1-p) 0 0 0
p 0 0 0 0 0 0 0 0 0 0 0 0 0 (1-p) 0 0
0 p 0 0 0 0 0 0 0 0 0 0 0 0 0 (1-p) 0
0 0 p 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
A=A'-eye(17);
A(17,:)=ones(1,17);
B=zeros(17,1);
B(17,1)=1;

Answers (0)

Asked:

on 13 Feb 2013

Commented:

on 13 Feb 2018

Community Treasure Hunt

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

Start Hunting!