clear;
% clear memory variables
Xm=100;
% x rangeYm=100;
% y range
Sink. X=0.5 * xm. % base station x
Sink. Y=0.5 * ym; Y % base station
n=100; % total node
P=0.1; Cluster probability %
E0=0.02; Initial energy %
ETX=50 * 0.000000000001; % energy transmission, every bit
ERX=50 * 0.000000000001; % energy, receive every bit
Efs=10 * 0.000000000001; % of dissipation energy, each bit
EDA=5 * 0.000000000001; % fusion, energy consumption per bit
Cc=0.6; % fusion rate
Rmax=1000; % to the total number of round
CM=32; % control information size
DM=4000; % data size
figure(1); % display picture
For I=1: n
S (I). Xd=rand (1, 1) * xm;
S (I). Yd=rand (1, 1) * ym;
S (I). G=0; Each cycle % end this variable to 0
S (I). E=E0; % set the initial energy to E0
S (I) type='N'; % the node type for the common
The plot (S (I). Xd, S (I). Yd, 'o');
hold on;
% keep the painted imageEnd % for each node randomly assigned to coordinate, and set up the initial energy E0, node type for the common
S (n + 1). Xd=sink. X.
S (n + 1). Yd=sink. Y;
The plot (S (n + 1). Xd, S (n + 1). Yd, 'x'); % drawing base station node
Flag_first_dead=0; % the first nodes death symbol variable
For r=1:1: begin each cycle rmax %
R + 1% showed round number
If (mod (r, round (1/p))==0)
For I=1: n
S (I). G=0;
End
End % how round number is just a cycle of integer times, is set S (I). E 0
Hold off. % pictures redraw each round
Cluster=0; Initial cluster % Numbers 0
Dead=0; % the initial node death number of 0
figure(1);
For I=1: n
If (S (I). E<=0)
The plot (S (I). Xd, S (I). Yd, 'red.');
Dead=dead + 1; % energy less than or equal to 0 node map into red, and will die node by the increase in the number 1
If (dead==1)
If (flag_first_dead==0)
The death of the first node first_dead=r % round number
The save ltest first_dead;
Flag_first_dead=1;
End
End % energy less than or equal to 0 node map into red, and will die node by the increase in the number 1
hold on;
The else
S (I) type='N';
The plot (S (I). Xd, S (I). Yd, 'o'); % to draw other node
hold on;
End
End
The plot (S (n + 1). Xd, S (n + 1). Yd, 'x');
% draw base station
Dead (r + 1)=Dead; % death each round the node number
The save ltest, Dead (r + 1); % the data stored in ltest file
For I=1: n
If (S (I). E> 0)
If (S (I). G<=0)
Temp_rand=rand; Take a random number %
If (temp_rand & lt; Mod=(p/(1 - p * (r, round (1/p))))) % if random number less than or equal to
S (I) type='C'. % this node for this round of cluster head
S (I) G=round (1/p) - 1; % S (I) G is set to greater than zero, this cycle can't be chosen for cluster head
Cluster=cluster + 1; % cluster Numbers 1
C (cluster). Xd=S (I). Xd.
C (cluster). Yd=S (I). Yd; % will sign this node for cluster head
The plot (S (I). Xd, S (I). Yd, 'k *'); % to draw this cluster head
Short=SQRT ((S (I). Xd - (S (n + 1). Xd)) ^ 2 + (S (I). Yd - (S (n + 1). Yd)) ^ 2); % of cluster heads to the base station distance
C (cluster). Short=short; % sign cluster heads for this distance
C (cluster). The id=I; % of the cluster head node id
Packet_To_BS (cluster)=1; % the number of packets sent to the base station to 1
End
End
End
End
CH_Num (r + 1)=cluster; % per round the Numbers of cluster
The save ltest, CH_Num (r + 1); % save cluster Numbers each round to the ltest
For I=1: n
If (S (I) type=='N' & amp; & S (I). E> Greater than 0 0) % for each energy and the cluster head node
Min_dis=SQRT ((S (I). Xd - (C (1). Xd)) ^ 2 + (S (I). Yd - (C (1). Yd)) ^ 2); % to calculate the distance the node to the cluster head 1
Min_dis_cluster=1;
For c=2:1: cluster
Temp=SQRT ((S (I). Xd - (C (C). Xd)) ^ 2 + (S (I). Yd - (C (C). Yd)) ^ 2);
If (temp
Min_dis_cluster=c;
End
End % select this cluster of points to which the smallest distance
Packet_To_BS (min_dis_cluster)=packet_To_BS (min_dis_cluster) + 1; % to add this node to the cluster
% head node packet number 1
Er1=ERX * CM * (cluster + 1); % this node receives each cluster head control information
% this node to join the cluster of cluster head slot total control information receiving energy consumption
Et1=ETX * (CM + DM) + Efs * * min_dis * min_dis (CM + DM); % this node sends information and send data information
% to the energy consumption of the cluster head
S (I). E=S (I). E - Er1 - Et1; % after this round of the residual energy
End
End
For c=1:1: cluster % each cluster head
Packet_To_BS (c); The % of cluster number of packets to be sent to the base station
CEr1=ERX * CM * (packet_To_BS (c) - 1); Receive this cluster of energy consumption of each node to join information %
CEr2 DM=ERX * * (packet_To_BS (c) - 1); Receive this cluster of energy consumption data of each node is %
CEt1 CM + Efs=ETX * * CM * (SQRT (xm * ym)) * (SQRT (xm * ym)); % of the cluster heads broadcast clusters information consumption
DM CEt2=(EDA) ETX + * * cc * packet_To_BS (c) + DM Efs * * cc * packet_To_BS (c) * c (c) in short * c (c) in short; % of cluster will be shipped so data fusion of the base station energy consumption
S (C (C). Id). E=S (C (C). The id). E - CEr1 - CEr2 - CEt1 - CEt2; The surplus energy % after this round of cluster head
End
For I=1: n
R (R + 1, I)=S (I) E; % per round the residual energy of each node
% the save ltest, R (R + 1, I); Save the data to the ltest %
End
hold on;
end
CodePudding user response:
The program is running ok