Info
This question is closed. Reopen it to edit or answer.
Some Error in code
1 view (last 30 days)
Show older comments
I wrote this code and It ran. However, when I changed r>1, I got some problems:
Index exceeds matrix dimensions.
Error in DOAN (line 106)
[SN(i).dP, col] = max(dP(:,i));
Subscripted assignment dimension mismatch.
Error in DOAN (line 95)
dD(i,j) = sqrt((CL(i).x-SN(j).x)^2+(CL(i).y-SN(j).y)^2);
Please help me.
close all;
clear;
clc;
% Dien tich mang xm*ym(m2) %
xm = 100;
ym = 100;
% Thiet lap thong so mang %
nodes = 50; %So nut mang
p = 0.1; %Ti le nut chu
cluster_head = p*nodes; %So nut chu
R = 30; %Ban kinh phat toi da ban tin quang ba cua nut chu(m)
Pt = 30; %Cong suat phat ban tin quang ba cua nut chu(mW)
Pl = 1; %Cong suat suy hao do khoang cach(mW/m)
to = 0.001; %Thoi gian lan truyen ban tin tren mot don vi quang duong truyen(s/m)
% Nang luong $
%Nang luong cho ban dau cua mot nut
Eo = 2; %Don vi J
%Nang luong tieu ton de xu ly mot bit trong mot nut
Eelec = 50*10^(-9); %Don vi J/b
%Nang luong bo khuech dai dung de lam viec
Eamp = 100*10^(-12); %Don vi J/b/m^2
%Nang luong dung de tong hop du lieu
EDA = 5*10^(-9); %Don vi J/b
% Kich thuoc goi tin %
k = 4000; %Don vi bit
% Vi tri nut Sink %
xsink = 50;
ysink = 150;
% So chu ky toi da %
r = 1;
% Khoi tao bien so CL %
CLs = 1; %Bien so de tinh toan
% Khoi tao bien dem so nut duoc ket noi voi nut chu cum
con_nodes = 0;
% Khoi tao bien dem so nut khong ket noi dc voi nut chu cum
dis_nodes = 0;
figure('Name','Mang cam bien khong day su dung giao thuc LEACH');
% Thiet lap mang %
plot(xsink,ysink,'d','LineWidth',2,...
'MarkerEdgeColor','y',...
'MarkerFaceColor','b',...
'MarkerSize',10);
hold on;
for i = 1:nodes
SN(i).id = i; %Dia chi nut mang
SN(i).x = rand*xm; %Hoanh do nut mang
SN(i).y = rand*ym; %Tung do nut mang
SN(i).CL = 0; %Bang 0 thi la nut binh thuong, bang 1 thi la nut chu cum
SN(i).rleft = 0; %Chu ky khong duoc lam nut chu(Thiet lap ban dau)
SN(i).E = Eo; %Nang luong ban dau cua nut
SN(i).S = 1; %Tinh trang ban dau cua nut 1=LIVE 0=DIE
hold on;
plot(SN(i).x,SN(i).y,'o','LineWidth',2,...
'MarkerEdgeColor','r',...
'MarkerFaceColor','y',...
'MarkerSize',10);
end
% Khoi tao bien tong nang luong tieu ton %
e = 0;
for t = 1:r
ClusterHeads = 0;
% Thiet lap cluster head %
for j = 1:nodes
SN(j).g = rand;
if ( SN(j).g < p/(1-p*mod(r,1/p)) ) && ( i ~= SN(j).rleft ) && ( SN(j).S == 1)
CL(j).id = SN(j).id;
CL(j).x = SN(j).x;
CL(j).y = SN(j).y;
% Khoang cach giua nut chu va nut Sink %
CL(j).dS = sqrt((CL(j).x-xsink)^2+(CL(j).y-ysink)^2);
CL(CLs) = CL(j);
SN(j).rleft = 1/p-mod(r,1/p);
SN(j).CL = 1;
CLs = CLs+1;
plot(CL(j).x,CL(j).y,'o','LineWidth',2,...
'MarkerEdgeColor','y',...
'MarkerFaceColor','k',...
'MarkerSize',10);
draw_circle1(SN(j).x,SN(j).y,R,'g');
end
end
ClusterHeads = CLs-1;
disp('So nut chu cum la');
disp(ClusterHeads);
legend('Sink','Sensor Node','Operator Node');
xlabel('Truc hoanh(m)');
ylabel('Truc tung(m)');
title('Mo hinh mang cam bien khong day');
grid on;
% Gop cac node thanh cac cum %
for i = 1:ClusterHeads
for j = 1:nodes
if ( SN(j).CL ~= 1 ) && ( SN(j).S == 1 )
% Khoang cach giua nut binh thuong va cac nut chu cum %
dD(i,j) = sqrt((CL(i).x-SN(j).x)^2+(CL(i).y-SN(j).y)^2);
% Cong suat ban tin quang ba nhan duoc cua nut binh thuong %
dP(i,j) = Pt-Pl*dD(i,j)-10*rand; %Cong suat ton hao do nhieu Pf=10*rand
end
end
end
% Gop cum dua theo Cong suat ban tin quang ba nhan duoc %
for i=1:nodes
%Khoang cach giua nut thuong va nut chu cum SN(i).dD
%Cong suat ban tin quang ba lon nhat ma nut nhan duoc tu nut chu SN(i).dP
%Dia chi nut chu ma nut thuoc cum do SN(i).C
[SN(i).dP, col] = max(dP(:,i));
if (SN(i).CL ~= 1) && (SN(i).dP > 0.1) && ( SN(i).S == 1 )%Cong suat toi thieu de co the nhan duoc la 0.1mW
plot([SN(i).x CL(col).x], [SN(i).y CL(col).y], 'r');
SN(i).C = CL(col).id;
SN(i).dD = dD(col,i);
con_nodes = con_nodes+1;
CL(col).ETx1 = Pt*to*R; %Nang luong phat ban tin quang ba cua nut chu cum
SN(i).ERx = (Eelec+EDA)*k*1/4; %Nang luong nut binh thuong tieu hao thu nhan va xu ly du lieu tu ban tin quang ba
SN(i).ETx1 = Pt*to*dD(col,i); %Nang luong phat ban tin hoi dap cua nut thuong
e = e+SN(i).ERx+SN(i).ETx1;
else
if (SN(i).CL ~= 1)
SN(i).C = 0; %Nut chua thuoc cum nao
dis_nodes = dis_nodes+1;
SN(i).ETx1 = 0;
else
SN(i).C = -1; %Nut chu
SN(i).ETx1 = 0;
end
end
end
for i = 1:ClusterHeads
e = e+CL(i).ETx1;
plot([CL(i).x xsink], [CL(i).y ysink]);
end
disp('So nut duoc ket noi voi nut chu cum:');
disp(con_nodes);
disp('So nut khong duoc ket noi voi nut chu cum:');
disp(dis_nodes);
% Giai doan on dinh %
% Nang luong nut binh thuong tieu hao gui du lieu cho nut chu cum %
for i = 1:nodes
if ( SN(i).CL ~= 1 ) && ( SN(i).S == 1 ) && ( SN(i).C > 0)
SN(i).ETx2 = Eelec*k+Eamp*k*SN(i).dD;
e = e+SN(i).ETx2;
if ( SN(i).E-SN(i).ETx1-SN(i).ETx2-SN(i).ERx ) < 0.2 %Nut chet
SN(i).S = 0;
end
end
end
% Nang luong nut chu cum thu nhan va xu ly du lieu %
for i = 1:ClusterHeads
if ( SN( CL(i).id ).CL == 1 ) && ( SN( CL(i).id ).S == 1 )
CL(i).ERx = (Eelec+EDA)*k;
e = e + CL(i).ERx;
if ( SN( CL(i).id ).E-CL(i).ETx1-CL(i).ERx ) < 0.2 %Nut chet
SN( CL(i).id ).S = 0;
end
end
end
% Nang luong nut chu cum gui du lieu cho nut Sink %
for i = 1:ClusterHeads
if ( SN( CL(i).id ).CL == 1 ) && ( SN( CL(i).id ).S == 1 )
CL(i).ETx2 = (Eelec+EDA)*k + Eamp*k*CL(i).dS;
e = e + CL(i).ETx2;
if ( SN( CL(i).id ).E-CL(i).ETx1-CL(i).ERx-CL(i).ETx2 ) < 0.2 %Nut chet
SN( CL(i).id ).S = 0;
end
end
end
end
1 Comment
Answers (1)
KALYAN ACHARJYA
on 8 May 2019
Edited: KALYAN ACHARJYA
on 8 May 2019
However, when I changed r>1, I got some problems:
I assumed r=1.5 and its OK, I didnot get any coding errror
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/218601/image.png)
4 Comments
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!