**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

# How to get and residual energy for cluster head and cluster member ?

21 views (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

Geoff Hayes
on 14 Oct 2014

Arul - why not just add a third column to the clusterHeads and clusterNodes matrices. From the link you provided, just modify the code to something like the following

clusterData = cell(size(X,2),1);

for k=1:size(clusterData)

clusterData{k}.clusterHead = [X(k) Y(k) E(k)];

clusterData{k}.clusterNodes = [];

end

for k=1:size(S,2)

x = S(k).xd;

y = S(k).yd;

energy = S(k).ENERGY;

minDist = Inf;

at = 0;

for u=1:size(X,2)

dist = sqrt((x-X(u))^2+(y-Y(u))^2);

if dist<minDist

minDist = dist;

at = u;

end

end

if at>0

clusterData{at}.clusterNodes = ...

[clusterData{at}.clusterNodes ; [x y energy] ];

end

end

Note that the above is untested, and assumes that you have created an E energy array in the manner that you created the X and Y arrays i.e.

X(cluster)=S(i).xd;

Y(cluster)=S(i).yd;

E(cluster)=S(i).ENERGY; % <-- add this line

##### 22 Comments

Arul prasath
on 15 Oct 2014

sir after modifying it's showing an error

"??? Error using ==> vertcat CAT arguments dimensions are not consistent."

" clusterData{at}.clusterNodes = [clusterData{at}.clusterNodes ; [x y energy] ]; "

Geoff Hayes
on 15 Oct 2014

Why is this occurring? If you put a breakpoint at the line

clusterData{at}.clusterNodes = [clusterData{at}.clusterNodes ; [x y energy] ];

what do you see? (Hint - you are not setting the ENERGY for all nodes.)

Arul prasath
on 15 Oct 2014

Geoff Hayes
on 15 Oct 2014

Arul - put a breakpoint at the line

clusterData{at}.clusterNodes = [clusterData{at}.clusterNodes ; [x y energy] ];

and see what is going wrong. What is energy? Look again at the hint in the previous comment.

Arul prasath
on 15 Oct 2014

sir, i executed it's showing wrong value like

"cluster head

ans =

clusterHead: [75.2022 104.7560 -2.4486]

clusterNodes: [69.3790 103.3981 0]"

cluster head energy should be more than '0' but it's showing negative value.

And one more thing while putting the break point it's coming why it's not coming while running the normal simulation?.

Geoff Hayes
on 15 Oct 2014

cluster head energy should be more than '0' but it's showing negative value.

You have not provided your code, so I can't guess as to why your code is setting the energy to be negative (or zero).

why it's not coming while running the normal simulation

Perhaps you have a clear all statement that is clearing your breakpoints. Again, without seeing your code, it is difficult to know exactly what the code is doing, how you are calling it, etc.

Arul prasath
on 15 Oct 2014

k sir, here is my code

clc;

clear all;

close all;

n=100;

Eo=1;

xm=200;

ym=200;

sink.x=0.5*xm;

sink.y=0.5*ym;

p=0.1;

%Eelec=Etx=Erx

ETX=50*0.000000001;

ERX=50*0.000000001;

%Transmit Amplifier types

Efs=10*0.000000000001;

Emp=0.0013*0.000000000001;

%Data Aggregation Energy

EDA=5*0.000000001;

a=0.5;

rmax=2; %New Metaheuristic Bat-Inspired Algorithm;

do=sqrt(Efs/Emp);

%Creation of the random Sensor Network

figure(1);

hold off;

for i=1:1:n

S(i).xd=rand(1,1)*xm;

XR(i)=S(i).xd;

S(i).yd=rand(1,1)*ym;

YR(i)=S(i).yd;

S(i).G=0;

%initially there are no cluster heads only nodes

S(i).type='N';

S(i).E=Eo;

S(i).ENERGY=0;

plot(S(i).xd,S(i).yd,'o');

hold on;

end

S(n+1).xd=sink.x;

S(n+1).yd=sink.y;

%plot(S(n+1).xd,S(n+1).yd,'o', 'MarkerSize', 12, 'MarkerFaceColor', 'r');

figure(1);

%First Iteration

%counter for CHs

countCHs=0;

%counter for CHs per round

rcountCHs=0;

cluster=1;

countCHs;

rcountCHs=rcountCHs+countCHs;

flag_first_dead=0;

dead1=0;

dead2=0;

for r=0:1:rmax

r

%Operation for epoch

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;

%counter for bit transmitted to Bases Station and to Cluster Heads

packets_TO_BS=0;

packets_TO_CH=0;

%counter for bit transmitted to Bases Station and to Cluster Heads

%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)

if(dead1==0)

dead1=0;

%disp(dead1);

disp('1st node dead');

dead1=[S(i).xd,S(i).yd];

disp(dead1);

end

if(r==1)

if (S(i).E<=0)

dead2=[S(i).xd,S(i).yd];

disp('2st node dead');

c1 = dead2(~ismember(dead2, dead1));

disp(c1);

end

end

if(r==2)

if (S(i).E<=0)

dead3=[S(i).xd,S(i).yd];

disp('3st node dead');

c2 = dead3(~ismember(dead3, dead2));

disp(c2);

end

end

plot(S(i).xd,S(i).yd,'*r');

dead=dead+1;

hold on;

end

%dead2=[S(i-1).xd,S(i-1).yd];

if S(i).E>0

S(i).type='N';

if (S(i).ENERGY==0)

plot(S(i).xd,S(i).yd,'o');

%'o','LineWidth',1, 'MarkerEdgeColor','k', 'MarkerFaceColor','g', 'MarkerSize',8);

end

if (S(i).ENERGY==1)

plot(S(i).xd,S(i).yd,'+');

%,'+','LineWidth',3, 'MarkerEdgeColor','k', 'MarkerFaceColor','r', 'MarkerSize',8);

end

hold on;

end

end

plot(S(n+1).xd,S(n+1).yd,'x');

STATISTICS(r+1).DEAD=dead;

DEAD(r+1)=dead;

%When the first node dies

if (dead==1)

if(flag_first_dead==0)

first_dead=r;

flag_first_dead=1;

end

end

countCHs=0;

cluster=1;

for i=1:1:n

if(S(i).E>0)

temp_rand=rand;

if ( (S(i).G)<=0)

%Election of Cluster Heads

if(temp_rand<= (p/(1-p*mod(r,round(1/p)))))

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;

C(cluster).xd=S(i).xd;

C(cluster).yd=S(i).yd;

plot(S(i).xd,S(i).yd,'k*');

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)

%0000

S(i).E=S(i).E- ( (ETX+EDA)*(40000000) + Emp*40000000*( distance*distance*distance*distance ));

E(cluster)=S(i).E;

%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)*(40000000) + Efs*40000000*( distance * distance ));

E(cluster)=S(i).E;

%S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Efs*4000*( distance * distance ));

end

Energy_disp(r+1) = S(i).E;

end

end

end

end

STATISTICS(r+1).CLUSTERHEADS=cluster-1;

CLUSTERHS(r+1)=cluster-1;

end

for i=1:1:r

r

if(r==2)

CIa1=0;

clusterData = cell(size(X,2),1);

for k=1:size(clusterData)

clusterData{k}.clusterHead = [X(k) Y(k) E(k)];

%CH=clusterData{k}.clusterHead;

clusterData{k}.clusterNodes = [];

end

for k=1:size(S,2)

x = S(k).xd;

y = S(k).yd;

energy = S(k).ENERGY;

minDist = Inf;

at = 0;

for u=1:size(X,2)

dist = sqrt((x-X(u))^2+(y-Y(u))^2);

if dist<minDist

minDist = dist;

at = u;

end

end

if at>0

clusterData{at}.clusterNodes = [ clusterData{at}.clusterNodes ; [x y energy] ];

disp('cluster head')

clusterData{at}

disp ('cluster head member node')

clusterData{at}.clusterNodes

end

end

end

end

hold on;

warning('OFF');

[VX,VY]=voronoi(X,Y);

plot(X,Y,'k+',VX,VY,'r-');

axis([0 xm 0 ym]);

Geoff Hayes
on 17 Oct 2014

Yes, you are right. If the code that you have written is executed, then those negative values appear. One problem is that you are incrementing cluster too early - note how you increment it and then set the E value a few lines later. You must do the following instead

C(cluster).id=i;

X(cluster)=S(i).xd;

Y(cluster)=S(i).yd;

%Calculation of Energy dissipated

distance;

if (distance>do)

S(i).E=S(i).E- ( (ETX+EDA)*(40000000) + Emp*40000000*( ...

distance*distance*distance*distance ));

end

if (distance<=do)

S(i).E=S(i).E- ( (ETX+EDA)*(40000000) + Efs*40000000*( ...

distance * distance ));

end

E(cluster)=S(i).E;

Energy_disp(r+1) = S(i).E;

cluster=cluster+1;

So that should fix the problem with a zero appearing as the first value in your E matrix.

As for negative values, where did these equations come from? Does the 40000000 correspond to 40 MHz from a WLAN 802.11n 40 MHz channel? Why have you chose EDA, ETX, and Emp to be the values that they are?

Much of you code seems to be copied from the Akshay Gore's FEX submission Comparison of LEACH EAMMH SEP TEEN Protocols SEP.m file. If this the case? Based on the block of code for the calculation of energy dissipation, the author does

%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

I'm guessing, based on the documentation included in the FEX submission, that 4000 corresponds to the 4000 bit packet size. So what does your 40000000 correspond to?

As for the error message vertcat CAT arguments dimensions are not consistent, this is because (for whatever reason) the energy local variable is empty, which causes the line

[ clusterData{at}.clusterNodes ; [x y energy] ]

to generate the error. This seems to occur only for the last element, the sink, which never gets the energy set, as only the first n elements of the n+1 struct array S gets updated properly.

Arul prasath
on 26 Oct 2014

yes your are correct. " So what does your 40000000 correspond to? " sir that's wrong value(40000000) but 4000 is correct value after correcting that value i'm getting the same error problem

- ??? Error using ==> vertcat CAT arguments dimensions are not consistent.*

so what shall i do for this ?

Geoff Hayes
on 26 Oct 2014

Arul prasath
on 29 Oct 2014

Sir, can u give some idea how to set energy for sink node. i tried like this

"S(n+1).xd=5;S(n+1).yd=5;"

but still i'm getting zero value for member nodes and same error what i wrote in previous comment.

Geoff Hayes
on 29 Oct 2014

Arul prasath
on 29 Oct 2014

Geoff Hayes
on 29 Oct 2014

Arul prasath
on 29 Oct 2014

Sir,i already tried like this

"S(n+1).xd=5;S(n+1).yd=5;"

but i'm not getting.

Geoff Hayes
on 29 Oct 2014

Arul - we seem to be going in circles! You already mentioned this in a previous comment, and my response was that you will just overwrite the original sink values. Here is the code that you are overwriting (or not, as I don't know where you have added these lines)

S(n+1).xd=sink.x;

S(n+1).yd=sink.y;

Again, as in my previous comment, why do that changing the xd and yd for the sink, at S(n+1), will change anything? You keep avoiding the problem which is why the energy is not set for the sink or why bother with the energy for the sink.

The sink is the (n+1)th element in S, but most of your (or rather, Akshay Gore's) loops only consider the first n elements...that is why the energy is never set.

Or, in your last for loop, just ignore the sink. Iterate as

for k=1:size(S,2)-1

instead.

Arul prasath
on 30 Oct 2014

Arul prasath
on 30 Oct 2014

1. sir, now i'm getting value for member nodes but that value is not correct. it's showing like this..

"cluster head

ans =

clusterHead: [11.6533 92.5642 0.9940]

clusterNodes: [9x3 double]

cluster head member node

ans =

11.6533 92.5642 0.9940

37.9830 122.7349 0.9961

6.7117 114.9017 0.9937

34.8620 46.0027 0.9949

16.9343 76.2849 0.9948

24.8237 56.9441 0.9948

29.7975 90.6634 0.9958

8.7175 33.2884 0.9893

0.8790 137.4425 0.9912"

Cluster head should have the higher residual energy compere than cluster head member nodes, but it's showing wrong value.

2. And one more thing, the same 'cluster head node' is displaying under 'cluster head member node', so it should not show the "cluster head node" in "cluster head member node".

Geoff Hayes
on 30 Oct 2014

Arul prasath
on 30 Oct 2014

k sir thank you , but according to author which ever has the higher residual energy that's going to be the cluster head and even in basic LEACH algorithm also saying the same thing. so please sir try this one please.

Don't ignore this code please tell the mistake in my code please...

### More Answers (0)

### See Also

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.