无线传感器网络加权质心定位算法Matlab仿真的一些疑问。
现在在做毕设,有一个Matlab程序请同学帮忙修改后还是有问题,想请问一下各位大神,谢谢!Errorin==>zhixinat5NodeAmount=UNAmount+B...
现在在做毕设,有一个Matlab程序请同学帮忙修改后还是有问题,想请问一下各位大神,谢谢!
Error in ==> zhixin at 5
NodeAmount=UNAmount+BeaconAmount;%网络节点个数
附程序:
function [Accuracy]=dingwei(BeaconAmount)
BorderLength=100;%正方形区域的边长
UNAmount=60;
NodeAmount=UNAmount+BeaconAmount;%网络节点个数
R=50;%节点的通信距离
% D=zeros(NodeAmount,NodeAmount);%未知节电到信标节点距离初始矩阵;BeaconAmount行NodeAmount列
%X=zeros(2,UNAmount);%节点估计坐标初始矩
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~在正方形区域内产生均匀分布的随机拓扑~~~~~~~~~~~~~~~~~~~~
C=BorderLength.*rand(2,NodeAmount);
%带逻辑号的节点坐标
Sxy=[[1:NodeAmount];C];
Beacon=[Sxy(2,1:BeaconAmount);Sxy(3,1:BeaconAmount)];%信标节点坐标
UN=[Sxy(2,(BeaconAmount+1):NodeAmount);Sxy(3,(BeaconAmount+1):NodeAmount)];%未知节点坐标
sume=0;
for i=1:1:UNAmount
m=0;
sumx=0; %x的累积值
sumy=0; %y的累积值
for j=1:1:BeaconAmount %n为信标节点个数
if sqrt(abs(UN(1,i)-Beacon(1,j))^2+abs(UN(2,i)-Beacon(2,j))^2)<=R %x2、y2为网络节点坐标,S.xd、S.yd为目标坐标
m=m+1;
sumx=sumx+Beacon(1,j);
sumy=sumy+Beacon(2,j);
end
end
if m==0
X(1,i)=0;
X(2,i)=0;
end
T.xd(i)=sumx./m;
X(1,i)= T.xd(i); %x的坐标平均值
T.yd(i)=sumy./m;
X(2,i)=T.yd(i); %y的坐标平均值
error(1,i)=sqrt(abs(UN(1,i)-X(1,i))^2+abs(UN(2,i)-X(2,i))^2);
sume=sume+error(1,i);
end
Accuracy=sume/(UNAmount*R); 展开
Error in ==> zhixin at 5
NodeAmount=UNAmount+BeaconAmount;%网络节点个数
附程序:
function [Accuracy]=dingwei(BeaconAmount)
BorderLength=100;%正方形区域的边长
UNAmount=60;
NodeAmount=UNAmount+BeaconAmount;%网络节点个数
R=50;%节点的通信距离
% D=zeros(NodeAmount,NodeAmount);%未知节电到信标节点距离初始矩阵;BeaconAmount行NodeAmount列
%X=zeros(2,UNAmount);%节点估计坐标初始矩
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~在正方形区域内产生均匀分布的随机拓扑~~~~~~~~~~~~~~~~~~~~
C=BorderLength.*rand(2,NodeAmount);
%带逻辑号的节点坐标
Sxy=[[1:NodeAmount];C];
Beacon=[Sxy(2,1:BeaconAmount);Sxy(3,1:BeaconAmount)];%信标节点坐标
UN=[Sxy(2,(BeaconAmount+1):NodeAmount);Sxy(3,(BeaconAmount+1):NodeAmount)];%未知节点坐标
sume=0;
for i=1:1:UNAmount
m=0;
sumx=0; %x的累积值
sumy=0; %y的累积值
for j=1:1:BeaconAmount %n为信标节点个数
if sqrt(abs(UN(1,i)-Beacon(1,j))^2+abs(UN(2,i)-Beacon(2,j))^2)<=R %x2、y2为网络节点坐标,S.xd、S.yd为目标坐标
m=m+1;
sumx=sumx+Beacon(1,j);
sumy=sumy+Beacon(2,j);
end
end
if m==0
X(1,i)=0;
X(2,i)=0;
end
T.xd(i)=sumx./m;
X(1,i)= T.xd(i); %x的坐标平均值
T.yd(i)=sumy./m;
X(2,i)=T.yd(i); %y的坐标平均值
error(1,i)=sqrt(abs(UN(1,i)-X(1,i))^2+abs(UN(2,i)-X(2,i))^2);
sume=sume+error(1,i);
end
Accuracy=sume/(UNAmount*R); 展开
1个回答
展开全部
你没有定义信标节点(BeaconAmount)的个数。不定义肯定报错啊。一下是我最近随便编的一段类似于质心算法的东西的核心部分,你的同学应该能看懂,有点帮助。
if num_of_neb_anchor(i)>1&&num_of_neb_anchor(i)<6
%如果未知节点i的邻居锚节点个数在2和5之间
fenmu(i)=0;
fenzi_x(i)=0;
fenzi_y(i)=0;
fenzi_z(i)=0;
for k=1:num_of_neb_anchor(i)
distant_rssi(i,k)=sqrt((node_x(i)-neighbor_anchor_x(i,k))^2+(node_y(i)-neighbor_anchor_y(i,k))^2+(node_z(i)-neighbor_anchor_z(i,k))^2);
fenmu(i)=fenmu(i)+1/distant_rssi(i,k);
fenzi_x(i)=fenzi_x(i)+neighbor_anchor_x(i,k)/distant_rssi(i,k);
fenzi_y(i)=fenzi_y(i)+neighbor_anchor_y(i,k)/distant_rssi(i,k);
fenzi_z(i)=fenzi_z(i)+neighbor_anchor_z(i,k)/distant_rssi(i,k);
end
esti_node_x(i)=fenzi_x(i)/fenmu(i);
esti_node_y(i)=fenzi_y(i)/fenmu(i);
esti_node_z(i)=fenzi_z(i)/fenmu(i);%未知节点的估计坐标
end
if num_of_neb_anchor(i)>1&&num_of_neb_anchor(i)<6
%如果未知节点i的邻居锚节点个数在2和5之间
fenmu(i)=0;
fenzi_x(i)=0;
fenzi_y(i)=0;
fenzi_z(i)=0;
for k=1:num_of_neb_anchor(i)
distant_rssi(i,k)=sqrt((node_x(i)-neighbor_anchor_x(i,k))^2+(node_y(i)-neighbor_anchor_y(i,k))^2+(node_z(i)-neighbor_anchor_z(i,k))^2);
fenmu(i)=fenmu(i)+1/distant_rssi(i,k);
fenzi_x(i)=fenzi_x(i)+neighbor_anchor_x(i,k)/distant_rssi(i,k);
fenzi_y(i)=fenzi_y(i)+neighbor_anchor_y(i,k)/distant_rssi(i,k);
fenzi_z(i)=fenzi_z(i)+neighbor_anchor_z(i,k)/distant_rssi(i,k);
end
esti_node_x(i)=fenzi_x(i)/fenmu(i);
esti_node_y(i)=fenzi_y(i)/fenmu(i);
esti_node_z(i)=fenzi_z(i)/fenmu(i);%未知节点的估计坐标
end
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询