I have distributed 100 random points(nodes) in an area by using following code
n=100; xm=100;ym=100;
for i = 1 : 1 :n
S(i).xd = rand(1,1)*xm ;
S(i).yd = rand (1,1)*ym ;
xd(i) = S(i).xd ;
yd(i) = S(i).yd ;
S(i).id = i;
S(i).type = 0;
S(i).g = 0 ;
S(i).E = Eo ;
S(i).type = 0 ;
end
Next I have fixed 10 gateway nodes at the edge of the area using following code
for i=1:1:10
Sg(i).xd= 2+100*rand(1,1);
Sg(i).yd=100;
xd(i)=Sg(i).xd;
yd(i)=Sg(i).yd;
Sg(i).id=i;
plot(Sg(i).xd,Sg(i).yd,'*k')
grid on;
hold on;
end
Now I have formed cluster heads using LEACH protocol from 100 nodes.
I have to find min distance gateway node from a CH. As there are 10 gateway nodes, I have to find which is closer to a CH in the area.
To determine CH I have used following code
for all nodes in the area
temp_rand1=rand;
if(temp_rand1<= (p/(1-p*mod(r,round(1/p)))))
countCHs1=countCHs1+1;
S3(i).type=1;
S3(i).g=round(1/p)-1;
C1(cluster1).xd=S3(i).xd;
C1(cluster1).yd=S3(i).yd;
**distance1=sqrt((S3(i).xd-(gw_node.x) )^2 + (S3(i).yd-(gw_node.y) )^2 );**
C1(cluster1).distance1=distance1;
C1(cluster1).id=i;
X(cluster1)=S3(i).xd;
Y(cluster1)=S3(i).yd;
cluster1=cluster1+1;
end
I know how to determine distance between a CH and one gateway node, But I do not know how to find closet gateway node from a set of gateway nodes. Thanks in advance
Please reply