資源簡介
無線傳感器網絡(WSN)leach分簇路由協議matlab仿真
代碼片段和文件信息
NodeNums?=?100;?%?the?num?of?node?
AreaR?=?100?;???%?the?area?of?simulate
NodeTranR=10;???%??the?transit?Radius
Elec=50?*?10^(-9);?%
Eamp=100*10^(-12);?
Bx=50;??%?The?Postion?of?baseation
By=175;
MaxInteral?=700;?%?the?leach?simulate?time
Pch=0.05;??%?the?desired?percentage?of?cluster?heads?
InitEn=0.5;??%?the?init?energy?of?all?node
Tr=30;??
?TDMA=100;
Kbit=2000;?%?the?bits?of?a?node?transmiting?a?packet?every?time
BandWitch?=?1*10.^(6);?%??Channel?Bandwitch
TOS_LOCAL_ADDRESS?=?0;?
for?i=1:(MaxInteral)
????AliveNode(i)=NodeNums;
????AmountData(i)=0;
end
sym?alldata;
alldata=0;
LAECH?=?zeros(1MaxInteral);
LAENO?=?zeros(1MaxInteral);??
for?i=1:1:NodeNums?
????EnNode(i)=InitEn;??%?the?init?energy?of?all?node
????StateNode(i)=1;????%?the?State?of?all?node?1:?alive?0:dead
????ClusterHeads(i)=0;??%?the?Set?of?Cluster?Head?1:?cluster?head?0?:node
????
????Rounds=0;??%?the?round
end
Threshold=0;????%?the?threshold?of?node?becoming?a?cluster-head
????Node.x=AreaR*rand(1NodeNums);??%?the?position?of?node?
????Node.y=AreaR*rand(1NodeNums);
????Node.c=zeros(1NodeNums);
????Node.d=zeros(1NodeNums);
????Node.l=zeros(1NodeNums);
????Node.csize=zeros(1NodeNums);
?????Node.initclEn=zeros(1NodeNums);
%????for?i=1:NodeNums
%?????Node.c(i)=0;???????????????????%?the?Cluster?head?of?node?
%?????Node.d(i)=0;???????????????????%?the?distance?between?cluster?head?and?node
%?????Node.l(i)=Kbit;????????????????%?the?length?of?node?i?transmit?packet
%?????Node.csize(i)=0;
%????end
?
for?Rounds?=?1:MaxInteral??
???%?the?Setup?phase?of?cluster
????Node.csize=Node.csize-Node.csize;
????Node.d=Node.d-Node.d;
????Node.c=Node.c-Node.c;?
????for?i?=1:NodeNums
??????Threshold=Pch/(1-Pch*(mod(Rounds-11/Pch)));?
??????if??StateNode(i)==1?????????%?if??node?is?alive
??????????if?ClusterHeads(i)?==1?
??????????????ClusterHeads(i)=0;
??????????elseif?rand(11) ??????????
??????????ClusterHeads(i)=1;
??????????Node.c(i)=TOS_LOCAL_ADDRESS;
???????????Node.initclEn(i)=EnNode(i);
?????????else???ClusetHeads(i)=0;
????????????????Node.initclEn(i)=EnNode(i);
?????????end
??????end
????end
???if?sum(ClusterHeads)==0
???????continue;
???end
????
????EntranPCH?=??Elec?*?Kbit+?Eamp*Kbit*((Tr.^2+Tr.^2));??%?The?expended?engergy?by?new?Cluster?head?advertising?that?it?is?new?cluster?head
????for?i=1:NodeNums?????
????????if?ClusterHeads(i)?==1?
?????????
??????????if??EnNode(i)?>=??EntranPCH
??????????????????EnNode(i)?=?EnNode(i)?-?EntranPCH?;
??????????????else??
??????????????????StateNode(i)=0;
???????????end???
???????end?
????end?
????for?i=1:NodeNums
??????if??StateNode(i)==1?????????%?if??node?is?alive
????????if?ClusterHeads(i)?~=1???%?the?node??is?not?cluster?head
??????????for?j=1:NodeNums???????
?????????????if?ClusterHeads(j)?==1???
???????????????dist?=?((Node.x(i)-Node.x(j)).^2)+((Node.y(i)-Node.y(j)).^2);??%?the?distance.^2
%????????????????if?dist?(Tr.^2+Tr.^2)??????????
- 上一篇:無線傳感器網絡SEP協議的MATLAB實現
- 下一篇:循環譜代碼
評論
共有 條評論