% Variables Definition:
% DEAD : a rmax x 1 array of number of dead nodes per round
% DEAD_N : a rmax x 1 array of number of dead Normal nodes per round
% CLUSTERHS : a rmax x 1 array of number of CHs per round
% PACKETS_TO_BS : a rmax x 1 array of number packets send to Base Station
per round
% PACKETS_TO_CH : a rmax x 1 array of number of packets send to CHs per
round
% FIRST_DEAD: the round at which the first node died
clear;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Sensor Field Dimensions - x and y maximum (in meters)
xm = 200;
ym = 200;
%x and y Coordinates of the BS
sink.x = 0.5*xm;
sink.y = 0.5*ym;
%Number of Nodes in the field
n = 200;
%Optimal Election Probability of a node
%to become CH
p = 0.08;
%Energy Model (all values in Joules) based on LEACH Journal paper
%Initial Energy of each node
Eo = 0.5;
%Total Dissipated Energy
Ediss = 0;
%Eelec=Etx=Erx
ETX = 50*0.000000001;
ERX = 50*0.000000001;
%Transmit Amplifier types
Efs = 10*0.000000000001; %free space model
Emp = 0.0013*0.000000000001; %multipath model
%Data Aggregation Energy
EDA = 5*0.000000001;
%maximum number of rounds
rmax = 1500;
%%%%%%%%%%%%%%%%%%%%%%%%% END OF PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%
%Computation of do (crossover distance)
do = sqrt(Efs/Emp); %about 87.7 m
ndata = xlsread('locdata.xls'¡ 'Sheet1');% reads an input
%Excel file containing positions of the sensor nodes and the BS
%Creation of the random Sensor Network
figure(1);
for i=1 : 1 : n
S(i).xd = ndata(i¡1);
XR(i) = S(i).xd;
S(i).yd = ndata(i¡2);
YR(i) = S(i).yd;
S(i).G = 0; %the flag which determines the value of the indicator
function¡ Ci(t)
%initially there are no cluster heads only nodes
S(i).type = 'N'; %type all nodes are the same (homogeneous network)
S(i).NumOfCH = 0;
S(i).DtoBS = sqrt( (S(i).xd)^2 + (S(i).yd)^2 );
segments = '5';
switch segments
case '1' % 1-segment DBS identical to LEACH
S(i).p0 = 0.08; %one probability for all nodes ie. LEACH
case '2' % 2-segment DBS (deltaP = 0.02)
if (S(i).DtoBS <= 163.5) % segment #1
S(i).p0 = 0.1; %deltaP = 0.02;
end
if (S(i).DtoBS > 163.5) % segment #2
S(i).p0 = 0.06;
end
case '5' % 5-segment DBS (deltaP = 0.01)
if (S(i).DtoBS<=110.41) %segment #1
S(i).p0 = 0.1;
end
if (S(i).DtoBS > 110.41 & S(i).DtoBS<=147.92) % segment #2
S(i).p0 = 0.09;
end
if (S(i).DtoBS > 147.92 & S(i).DtoBS<=177.68) % segment #3
S(i).p0 = 0.08;
end
if (S(i).DtoBS > 177.68 & S(i).DtoBS<=203.13) % segment #4
S(i).p0 = 0.07;
end
if (S(i).DtoBS > 203.13) % segment #5
S(i).p0 = 0.06;
end
case '10' % 5-segment DB (deltaP = 0.005)
if (S(i).DtoBS<=85.69) %segment #1
S(i).p0 = 0.1;
end
if (S(i).DtoBS>85.69 & S(i).DtoBS<=110.39) %segment #2
S(i).p0 = 0.095;
end
if (S(i).DtoBS>110.39 & S(i).DtoBS<=130.49) %segment #3
S(i).p0 = 0.09;
end
if (S(i).DtoBS>130.49 & S(i).DtoBS<=147.89) %segment #4
S(i).p0 = 0.085;
end
if (S(i).DtoBS>147.89 & S(i).DtoBS<=163.45) %segment #5
S(i).p0 = 0.08;
end
if (S(i).DtoBS>163.45 & S(i).DtoBS<=177.65) %segment #6
S(i).p0 = 0.08;
end
if (S(i).DtoBS>177.65 & S(i).DtoBS<=199.79) %segment #7
S(i).p0 = 0.075;
end
if (S(i).DtoBS>199.79 & S(i).DtoBS<=203.09) %segment #8
S(i).p0 = 0.07;
end
if (S(i).DtoBS>203.09 & S(i).DtoBS<=214.68) %segment #9
S(i).p0 = 0.065;
end
if (S(i).DtoBS>214.68) %segment #10
S(i).p0 = 0.06;
end
otherwise
end
S(i).ENERGY = 0;
plot(S(i).xd¡S(i).yd¡'o');
hold on;
end %switch
S(n+1).xd = ndata(n+1¡1); %the last coordinate pair in the xls file
S(n+1).yd = ndata(n+1¡2); % is the location of the Base Station
plot(S(n+1).xd¡S(n+1).yd¡'x');
total_packets = 0;
%First Iteration
figure(1);
%counter for CHs
countCHs = 0;
%counter for CHs per round
rcountCHs = 0;
cluster = 1;
countCHs;
rcountCHs = rcountCHs + countCHs;
flag_first_dead = 0;
flag_half_dead = 0;
flag_all_dead = 0;
FIRST_DEAD = 0;
half_dead =0 ;
all_dead = 0;
for r=0 : 1 : rmax
r %writes the number of current round in the
%MATLAB command window
%Operation for Period
if(mod(r¡ round(1/p) )==0)
for i=1:1:n
S(i).G=0;
S(i).cl=0;
end
end
hold off;
%Number of dead nodes
dead = 0;
dead_a = 0; <------>
%Number of dead Normal Nodes
dead_n = 0;
%counter for bit transmitted to Bases Station and to CHs
packets_TO_BS = 0;
packets_TO_CH = 0;
%counter for bit transmitted to Bases Station and to CHs
%per round
PACKETS_TO_CH(r+1) = 0;
PACKETS_TO_BS(r+1) = 0;
figure(1);
for i=1 : 1 : n
%checking if there is a dead node
if (S(i).E <= 0)
plot(S(i).xd¡S(i).yd¡'k.');
dead = dead + 1;
if(S(i).ENERGY==1)
dead_a = dead_a + 1;
end
if(S(i).ENERGY==0)
dead_n = dead_n + 1;
end
hold on;
end
if S(i).E>0
S(i).type = 'N';
if (S(i).ENERGY==0)
plot(S(i).xd¡S(i).yd¡'o');
end
if (S(i).ENERGY==1)
plot(S(i).xd¡S(i).yd¡'+');
end
hold on;
end
end
plot(S(n+1).xd¡S(n+1).yd¡'x');
STATISTICS(r+1).DEAD = dead;
DEAD(r+1) = dead;
DEAD_N(r+1) = dead_n;
DEAD_A(r+1) = dead_a;
%When the first node dies
if (dead==1)
if(flag_first_dead==0)
FIRST_DEAD = r
flag_first_dead = 1;
end
end
if (dead==n/2)
if(flag_half_dead==0)
half_dead = r
flag_half_dead==1;
end
end
if (dead==n)
if(flag_all_dead==0)
all_dead = r
flag_all_dead==1;
end
end
countCHs = 0;
cluster = 1;
for i=1 : 1 : n
if(S(i).E > 0)
temp_rand=rand; %generating a random number (for CH election)
if ( (S(i).G) <= 0)
%Election of CHs
if(temp_rand <= (S(i).p0/(1-p*mod(r¡round(1/p))))) %election as CH for
current round
countCHs = countCHs + 1;
packets_TO_BS = packets_TO_BS + 1;
PACKETS_TO_BS(r+1) = packets_TO_BS;
S(i).type = 'C';
S(i).G = round(1/p) - 1;
S(i).NumOfCH = S(i).NumOfCH + 1;
C(cluster).xd = S(i).xd;
C(cluster).yd = S(i).yd;
%plot(S(i).xd¡S(i).yd¡'r*');
distance = sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-
(S(n+1).yd) )^2 );
C(cluster).distance = distance;
C(cluster).id = i;
X(cluster) = S(i).xd;
Y(cluster) = S(i).yd;
cluster = cluster + 1;
%Calculation of Energy dissipated
distance;
if (distance > do)
S(i).E = S(i).E - ( (ETX+EDA)*(4000) + Emp*4000*(
distance*distance*distance*distance ));
end
if (distance <= do)
S(i).E = S(i).E - ( (ETX+EDA)*(4000) + Efs*4000*(
distance * distance ));
end
end
end
end
end % end for
STATISTICS(r+1).CLUSTERHEADS = cluster-1;
CLUSTERHS(r+1) = cluster - 1;
%Election of Associated CH for Normal Nodes
for i=1 : 1 : n
if ( S(i).type=='N' && S(i).E > 0 )
if(cluster-1 >= 1)
min_dis = 3*sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2 );
min_dis_cluster = 1;
for c=1 : 1 : cluster-1
temp = min(min_dis¡sqrt( (S(i).xd-C(c).xd)^2 + (S(i).yd-
C(c).yd)^2 ) );
if ( temp < min_dis )
min_dis = temp;
min_dis_cluster = c;
end
end
%Energy dissipated by associated CH
min_dis;
if (min_dis > do)
S(i).E = S(i).E - ( ETX*(4000) + Emp*4000*( min_dis *
min_dis * min_dis *
end
if (min_dis <= do)
S(i).E = S(i).E - ( ETX*(4000) + Efs*4000*( min_dis *
min_dis));
end
%Energy dissipated
if(min_dis > 0)
ÇíÇääÇãå ÇÑÔäÇÓí äæíÏ Çãíäí 132
S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E - (
(ERX + EDA)*4000 );
PACKETS_TO_CH(r+1) = n-dead-cluster+1;
end
S(i).min_dis = min_dis;
S(i).min_dis_cluster = min_dis_cluster;
end
end
end %end for
hold on;
countCHs;
rcountCHs = rcountCHs+countCHs;
Ediss(r+1) = 0;
for i=1 : 1 : n
Ediss(r+1) = Ediss(r+1) + (Eo - S(i).E);
end
if (r==0)
totalpackets(r+1) = PACKETS_TO_BS(r+1) + total_packets;
end
if (r>0)
totalpackets(r+1) = PACKETS_TO_BS(r+1) + totalpackets(r);
end
end
figure(3);
for k= 1 : 1 : n
plot3(S(k).xd¡S(k).yd¡S(k).E);
hold on;
end
res1 = {S.xd; S.yd; S.E; S.NumOfCH};
temp = xlswrite('Results.xls'¡ res1'¡ 'Sheet2'¡ 'A2');
figure(4);
xlabel('round');
ylabel('Number of nodes alive');
for i=0 : 1 : rmax
plot(i+1¡n-DEAD(i+1)¡'k.');
hold on;
end
res2 = n-DEAD;
temp = xlswrite('Results.xls'¡ res2'¡ 'Sheet1'¡ 'B2');
temp = xlswrite('Results.xls'¡ FIRST_DEAD'¡ 'Sheet1'¡ 'E2');
temp = xlswrite('Results.xls'¡