clear;
noOfNodes = 50;
rand('state', 0);
figure(1);
clf;
hold on;
L = 1000;
R = 200; % maximum range;
netXloc = rand(1,noOfNodes)*L;
netYloc = rand(1,noOfNodes)*L;
for i = 1:noOfNodes
plot(netXloc(i), netYloc(i), '.');
text(netXloc(i), netYloc(i), num2str(i));
for j = 1:noOfNodes
distance = sqrt((netXloc(i) - netXloc(j))^2 + (netYloc(i) - netYloc(j))^2);
if distance <= R
matrix(i, j) = 1; % there is a link;
line([netXloc(i) netXloc(j)], [netYloc(i) netYloc(j)], 'LineStyle', ':');
else
matrix(i, j) = inf;
end;
end;
end;
الكود يقوم بعمل خمسين نقطه عشوائه ك موقع ل خمسين نود في شبكه
وموقعها في الاكس والواي اكسس هو netXloc و netYloc على التوالي وبعد ذلك يوقم برسمها وتمثيلها بنقطه بالامر التالي
plot(netXloc(i), netYloc(i), ‘.’);
وكذلك يوقم بكتابه رقم كل نود في نفس الموقع بالامر التالي
text(netXloc(i), netYloc(i), num2str(i));
بعد ذلك يحسب المسافه بين كل نودين بالامر التالي
distance = sqrt((netXloc(i) - netXloc(j))^2 + (netYloc(i) - netYloc(j))^2);
اذا كانت المسافه بين النودين اكبر من 200 فانها يعتبر ان بينهما اتصال ويمثلها بخط
مالم فلا يوجد بينهما كونكشن
if distance <= R
matrix(i, j) = 1; % there is a link;
line([netXloc(i) netXloc(j)], [netYloc(i) netYloc(j)], ‘LineStyle’, ‘:’);
else
والمكاسمم رينج هنا 1000 وليس 200 اي انه L not R
All the best