ورود

View Full Version : ترجمه و رفع ایراد کد



ehsanocx
پنج شنبه 22 بهمن 1394, 22:36 عصر
سلام دوستان. دوستان کسی میتونه این کد های من رو ترجمه کنه و ایراداتش رو برطرف کنه؟ و بگه این کد ها دقیقا چه کاری انجام میدهند؟ با تشکر





تابع NETWORK BULDER

function [S]=networkbuilder()
global nodes
global networkx
global networky
global sinkx
global sinky
global InitialEnergy
global S
for i=1:1:nodes
S(i).E=InitialEnergy;
S(i).xd=rand(1,1)*networkx;
S(i).yd=rand(1,1)*networky;
S(i).attack=-1;
S(i).my_clusterhead=i;
plot(S(i).xd,S(i).yd,'o');
end
S(nodes+1).xd=sinkx;
S(nodes+1).yd=sinky;
plot(sinkx,sinky,'+');
S(nodes+1).E=1000;
End


ATTACK BUILDERتابع

function [S]=attackbuilder(S)
global nodes
global attackednodes
for i=1:1:attackednodes
S(ceil(rand(1,1)*nodes)).attack=1;
plot (S(ceil(rand(1,1)*nodes)).xd,S(ceil(rand(1,1)*node s)).yd,'ko');
end



energy_of_networkتابع
function sum=energy_of_network()
global S
sum=0;
for i=1:1:length(S)
if S(i).E>0
sum=sum+S(i).E;
end
end
end
function live=live_sensor()
global S
live=0;
for i=1:1:length(S)
if S(i).E>0
live=live+1;
else
S(i).type='dead';
end
end
end


head_election_pahseتابع
function [delay,number_of_cluster]=head_election_pahse(r,p)
global S
global sinkx
global sinky
global Data_packet
global non_CH
threshold=p/(1-p*(mod(r,1/p )));
number_of_cluster=0;
delay=0;
for i=1:1:length(S)-1
random=rand(1,1);
if random<threshold && S(i).Head_in_round <= r-1/p && ~strcmp(S(i).type,'dead')
S(i).type='cluster';
S(i).dist_to_BS=distance(S(i).xd,S(i).yd,sinkx,sin ky);
S(i).Head_in_round=r;
number_of_cluster=number_of_cluster+1;
end
end



Direct Transmitتابع
X=0;
if number_of_cluster==0
non_CH=non_CH+1;
for i=1:1:length(S)
if ~strcmp(S(i).type,'dead')
S(i).dist_to_BS=distance(S(i).xd,S(i).yd,sinkx,sin ky);
X=X+S(i).dist_to_BS;
S(i).E=S(i).E-consume_transmit(S(i).dist_to_BS,Data_packet);
if S(i).E<=0

end
end
end
end
delay=X/300000;
end
advertisement_phaseتابع
function advertisement_phase(nCluster)
global S
global ym
global xm
global Control_packet
LEACH_AD_DISTANCE=(ym+xm)*5;
for i=1:1:length(S)
if strcmp(S(i).type,'cluster')
S(i).E=S(i).E-consume_transmit(LEACH_AD_DISTANCE,Control_packet) ;
elseif ~strcmp(S(i).type,'dead')
S(i).E=S(i).E-(nCluster*consume_receive(Control_packet));
end
end
end


cluster_setup_phaseتابع
function cluster_setup_phase()
global S
global Control_packet
global sinkx
global sinky
holdon
for i=1:1:length(S)-1
plot(S(i).xd,S(i).yd,'o');
if strcmp(S(i).type,'normal') && ~strcmp(S(i).type,'dead')
for j=1:1:length(S)
if strcmp(S(j).type,'cluster')
if distance(S(i).xd,S(i).yd,S(j).xd,S(j).yd)<S(i).dist_to_myClusterhead
S(i).dist_to_myClusterhead=distance(S(i).xd,S(i).y d,S(j).xd,S(j).yd);
S(i).my_clusterhead=j;
end
end
end
plot([S(i).xd S(S(i).my_clusterhead).xd],[S(i).yd S(S(i).my_clusterhead).yd])
if S(i).attack==1
plot(S(i).xd,S(i).yd,'k*');
end


S(i).E=S(i).E-consume_transmit(S(i).dist_to_myClusterhead,Contro l_packet);
S(S(i).my_clusterhead).cluster_member=S(S(i).my_cl usterhead).cluster_member+1;
S(S(i).my_clusterhead).E=S(S(i).my_clusterhead).E-consume_receive(Control_packet);
end
end
plot(sinkx,sinky,'r*');
pause(1);
holdoff
plot(98,98,'.');
end


schedule_creation_phaseتابع
function schedule_creation_phase()
global S
global Control_packet
global ym
global xm
SCHEDULE_DISTANCE=sqrt(ym^2+xm^2)/2;
for i=1:1:length(S)
if strcmp(S(i).type,'cluster')
S(i).E=S(i).E-consume_transmit(SCHEDULE_DISTANCE,(S(i).cluster_m ember+1)*Control_packet);
if S(i).E<=0
end
else
S(i).E=S(i).E-consume_receive(Control_packet);
end
end
end


data_transmission_phaseتابع
function [z,delay,numpacket]=data_transmission_phase()
global S
global Data_packet
global alpha
global live
numpacket=0;
delay=0;
X=0;
z=0;
for i=1:1:length(S)
S(i).ttl=rand(1,1)*distance(0,0,50,50);
er=S(i).dist_to_myClusterhead;
er=er+S(i).dist_to_BS;
if S(i).ttl<er

elseif (20*log10(4*3.14)+10*log10(er)+7)>45
z=z+1;
end
if strcmp(S(i).type,'normal') && ~strcmp(S(i).type,'dead')

S(i).E=S(i).E-consume_transmit(S(i).dist_to_myClusterhead,Data_p acket);
X=X+S(i).dist_to_myClusterhead/2;
S(S(i).my_clusterhead).E=S(S(i).my_clusterhead).E-consume_receive(Data_packet);
numpacket=numpacket+Data_packet;
if S(i).E<=0
end
if S(S(i).my_clusterhead).E<=0
end
elseif strcmp(S(i).type,'cluster')
S(i).E=S(i).E-consume_fusion(Data_packet*(S(i).cluster_member+1) );
S(i).packets=Data_packet*(S(i).cluster_member+1)*a lpha;
S(i).E=S(i).E-consume_transmit(S(i).dist_to_BS,S(i).packets);
X=X+S(i).dist_to_BS;
end
end
delay=(X/300000)/live;
end


Energy_consumption_CHتابع
function ECH=Energy_consumption_CH()
global S
ECH=0;
for i=1:1:length(S)
if strcmp(S(i).type,'cluster') && S(i).E>0
ECH=ECH+S(i).E;
end
end
end

prepare_to_next_roundتابع
function prepare_to_next_round()
global S
for i=1:1:length(S)
if ~strcmp(S(i).type,'dead')
S(i).type='normal';
S(i).my_clusterhead=i;
S(i).dist_to_myClusterhead=100000;
S(i).dist_to_BS=0;
S(i).cluster_member=0;
S(i).packets=0;
end
end
end


consume_fusion تابع
function con=consume_fusion(data)
global EDA;
con=data*EDA;
end
consume_transmitتابع
function con=consume_transmit(LEACH_AD_DISTANCE,Control_pac ket)
global Eelec
global Efs
global Emp
global d0
if LEACH_AD_DISTANCE<d0
con=Control_packet*Eelec+Control_packet*Efs*LEACH_ AD_DISTANCE^2;
else
con=Control_packet*Eelec+Control_packet*Emp*LEACH_ AD_DISTANCE^4;
end
end


consume_receive تابع
function con=consume_receive(Control_packet)
global Eelec
con=Control_packet*Eelec;
end