Http://www.ilovematlab.cn/thread-177006-1-1.html
Leach
clear;% clear the amount of internal changes
Xm=100;%x Axis Range
Ym=100;%y Axis Range
sink.x=0.5*xm;% Base Station X axis
sink.y=0.5*ym;% Base Station Y axis
n=100;% episodes
Probability of p=0.1;% cluster head
e0=0.02;% Initial Energy
etx=50*0.000000000001;% transmit energy, per bit
erx=50*0.000000000001;% receive energy, per bit
efs=10*0.000000000001;% dissipative Energy, per bit
eda=5*0.000000000001;% blends energy consumption, per bit
cc=0.6;% Fusion Rate
rmax=1000;% Total number of wheels
cm=32;% Control Information Size
dm=4000;% Data size
Figure (1);% Display Picture
For I=1:1:n
S (i). Xd=rand (*XM);
S (i). Yd=rand (*ym);
S (i). g=0;% Each cycle this change is 0
S (i). e=e0;% set the initial energy to E0
S (i). type= ' N ';% dot type is normal
Plot (S (i). Xd,s (i). yd, ' o ');
Hold on;% to keep the image you are painting
End% assigns a seat to each of the nodes and set the initial energy to E0, which is normal
S (n+1). xd=sink.x;
S (n+1). Yd=sink.y;
Plot (S (n+1). Xd,s (n+1). yd, ' x ');% graphics Base station nodes
flag_first_dead=0;% The first death episode of the logo change
For r=1:1:rmax% start every loop
r+1% showing the number of wheels
if (mod (R,round (1/p)) ==0)
For I=1:1:n
S (i). g=0;
End
end% how the number of wheels is exactly the whole number of times of a period, set S (i). E for 0
Hold off;% re-graphics
cluster=0;% Initial cluster of 0
dead=0;% initial death episode of 0
Figure (1);
For I=1:1:n
if (S (i). e<=0)
Plot (S (i). Xd,s (i). yd, ' red. ');
dead=dead+1;% the energy less than 0, and adds 1 to the death point.
if (dead==1)
if (flag_first_dead==0)
First_dead=r% of the first episode of the Death Chakra
Save Ltest, First_dead;
flag_first_dead=1;
End
end% the energy less than 0, and adds 1 to the death point.
Hold on;
Else
S (i). type= ' N ';
Plot (S (i). Xd,s (i). yd, ' o ');% drawing other nodes
Hold on;
End
End
Plot (S (n+1). Xd,s (n+1). yd, ' x ');% graphics Base station
Dead (r+1) =dead; % death episodes per round
Save Ltest, Dead (r+1);% will deposit this to ltest file
For I=1:1:n
if (S (i). E>0)
if (S (i). g<=0)
temp_rand=rand;% take a random number
if (temp_rand<= (p/(1-p*mod (R,round (1/p))))% if the random number is less than
S (i). type= ' C ';% this point is the cluster head
S (i). G=round (1/p) -1;%s (i). G is set to be greater than 0 and this cycle can no longer be chosen as a cluster
cluster=cluster+1;% Cluster head plus 1
C (cluster). Xd=s (i). xd;
C (cluster). Yd=s (i). yd;% to mark this episode as a cluster
Plot (S (i). Xd,s (i). yd, ' k* ');% graphics this cluster
Distance=sqrt ((S (i). xd-(S (n+1). xd)) ^2+ (S (i). yd-(S (n+1). yd)) ^2); The distance from the cluster head to the base station
C (cluster). distance=distance;% logo for this cluster
C (cluster). id=i; % of this cluster's nodes ID
Packet_to_bs (cluster) =1;% sent to the base station with a packet of 1
End
End
End
End
Ch_num (r+1) =cluster; % of clusters per round
Save Ltest,ch_num (r+1);% saves each cluster head to Ltest
For I=1:1:n
if (S (i). type== ' N ' &&s (i). e>0)% of each energy greater than 0 and non-cluster nodes
Min_dis=sqrt ((S (i). xd-(c (1). xd)) ^2+ (S (i). yd-(c (1). yd)) ^2);% calculates the distance from the cluster 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)
Min_dis=temp;
Min_dis_cluster=c;
End
end% Select the minimum distance to which cluster
Packet_to_bs (Min_dis_cluster) =packet_to_bs (min_dis_cluster) +1;% the cluster to which this dot is added
% of nodes plus 1
er1=erx*cm* (cluster+1);% This nodes receive control information for each cluster
% this point joins the cluster's time slot to control the total receiving power of the information
et1=etx* (CM+DM) +efs* (CM+DM) *min_dis*min_dis;% this to send add information and send data
% to cluster energy consumption
S (i). E=s (i). e-er1-et1;% the remaining energy of this round
End
End
For c=1:1:cluster% each cluster
Packet_to_bs (c); The number of packets to be sent to the base station by the cluster
cer1=erx*cm* (Packet_to_bs (c)-1);% received the energy consumption of the information added to each of the nodes of this cluster
cer2=erx*dm* (Packet_to_bs (c)-1);% received the energy consumption of each of the nodes of this cluster
cet1=etx*cm+efs*cm* (sqrt (XM*YM)) * (sqrt (xm*ym));% The energy consumption of cluster information
Cet2= (Etx+eda) *dm*cc*packet_to_bs (c) +efs*dm*cc*packet_to_bs (c) *c (c). Distance*c (c). distance;% cluster so the energy consumption of the base station is then fused.
S (c (c). id). E=s (c (c). id). e-cer1-cer2-cet1-cet2;% the remaining energy of the rear cluster
End
For I=1:1:n
R (r+1,i) =s (i). E % surplus energy per point per round
% save Ltest,r (r+1,i);% save this statistic to Ltest
End
Hold on;
End
Leach Protocol MATLAB Simulation code