running a consensus protocol on a directed graph created my an Adj-matrix

조회 수: 24 (최근 30일)
Tiara DeRosa
Tiara DeRosa 2021년 10월 11일
댓글: Matt J 2021년 10월 11일
getting weired error that i dont understand
"Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 6-by-2."
the warning appers on ux(i,1) = ux(i,1) + (x(:,j)- x(:,i)); most likely it will happen for the line after that as well
close all; clear all; clc
max_iter = 600;
N = 6; % Number of agents
dt = 0.02;
% Agents' Initial Positions
x0 = [0, 2, 2, 4, 4, 3]';
y0 = [0, 1,-1, 1,-1, 3]';
A = [0,1,0,0,0,0; 1,0,0,0,0,0; 1,1,0,0,1,0; 0,1,0,0,0,0; 0,0,1,1,0,0; 0,1,0,1,0,0];
G= digraph(A)
% Initialize vectors
x = zeros(N,max_iter);
y = zeros(N,max_iter);
xc = zeros(2,max_iter);
x(:,1) = x0;
y(:,1) = y0;
xc(:,1) = (1/N*ones(1,N)*[x0,y0])';
% Initialize plot
figure(1), hold on
set(gca,'xcolor','none','ycolor','none')
agenPlot = plot(x(:,1),y(:,1),'o','markersize',16,'MarkerFaceColor',[0.12,0.49,0.65],'MarkerEdgeColor','none');
xBplot = plot(xc(1,1),xc(2,1),'x','markersize',16,'MarkerFaceColor',[0.62,0.49,0.15],'linewidth',3);
axis([-1,5,-3,3])
for k = 1:max_iter-1
% Initialize zero velocities
ux = zeros(N,1);
uy = zeros(N,1);
% FILL THIS PART
% Compute consensus
for i = 1:N
inNeighbors= predecessors(G,i)
for j = inNeighbors
ux(i,1) = ux(i,1) + (x(:,j)- x(:,i));
uy(i,1) = uy(i,1)+ (y(:,i)-y(:,j));
end
end
% Integration step
x(:,k+1) = x(:,k) + ux.*dt;
y(:,k+1) = y(:,k) + uy.*dt;
% Store centroid's position
xc(:,k+1) = (1/N*ones(1,N)*[x(:,k),y(:,k)])';
% Update plot
set(agenPlot,'xdata',x(:,k),'ydata',y(:,k))
set(xBplot,'xdata',xc(1,k),'ydata',xc(2,k))
drawnow
end
figure(2), hold on
title('Solution to Q.2.c')
set(gcf,'color','white')
plot( 1:max_iter , sqrt(xc(1,:).^2 + xc(2,:).^2),'b','linewidth',2 )
plot( 1:max_iter , sqrt(x.^2 + y.^2),'k' )
legend({'$\bar{x}$','$x_i$'},'interpreter','latex','fontsize',16)
xlabel('Iterations','fontsize',16)
ylabel('$\| x \|$','interpreter','latex','fontsize',16)

답변 (1개)

Matt J
Matt J 2021년 10월 11일
편집: Matt J 2021년 10월 11일
Did you mean to have this?
ux(i,1) = ux(i,1) + (x(i,j)- x(i,i))
It's not clear from your original code how you expected to get a scalar value on the right hand side.
  댓글 수: 2
Tiara DeRosa
Tiara DeRosa 2021년 10월 11일
well i change some stuff around found out that i can use indegree to get the in negbours of each node
and i use your change and got the out put of the indegree matrx
n = 6×6
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
2 4 1 2 1 0
i also got this error message "Index in position 2 is invalid. Array indices must be positive integers or logical values."
the warnning sign was one the ux(i,1) = ux(i,1) + (x(i,j)- x(i,i)); line
% FILL THIS PART
% Compute consensus
for i = 1:N
n(N,i)= indegree(G,i)
for j = n(N,i)
ux(i,1) = ux(i,1) + (x(i,j)- x(i,i));
uy(i,1) = uy(i,1)+ (y(i,i)-y(i,j));
end
end
Matt J
Matt J 2021년 10월 11일
You have to handle the case where the indegree is 0.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by