Can I create a quiver plot that ends up pointing to one area?

2 views (last 30 days)
Shelby Hacker on 10 Jun 2021
Commented: Shelby Hacker on 10 Jun 2021
Here is some context:
I am doing a research project over swarm robotics. My goal is to prevent an "invader" robot (red) from being able to interfere with the swarm. In order to start simulating this situation, I started by creating a team of robots and one invader. My plan of action is to use the force of potential flows to push the invader outside of the swarm area.
So, here is my issue. I created a quiver plot to show potential velocity direction of the robots (blue), seen below.
The issue I encounter here is that I need the robot's arrows to all end up pointing in the direction of the invader (red). Is there a way I can achieve this? My guess would be that the invader would need some form of "attraction", but then again I do not know. If there is a better way to achieve this, I am open to suggestions.
I have attatched the code I am using below. You will need the ObjectFlow code and Swarm_Invader code. Thank you in advance!
ObjectFlow.m
function [Vxi,Vyi,Psi,Phi] = objectFlow(xi,yi,ui,vi,S,X,Y)
% doubletFlow Calculates the x and y component of the path vector due to a
% doublet flow
% Uses Laplacian Transforms to calculate x and y components from global
% corrdinates X,Y, the source location xi,yi, and strength S
r = sqrt((X-xi).^2+(Y-yi).^2);
theta = atan2(Y-yi,X-xi)-atan2(vi,ui);
Vr = -S.*cos(theta)./(2.*pi.*r.^2);
Vt = -S.*sin(theta)./(2.*pi.*r.^2);
Vxi = Vr.*cos(theta)+Vt.*cos(theta+pi/2);
Vyi = Vr.*sin(theta)+Vt.*sin(theta+pi/2);
Psi = -S.*sin(theta)./(2.*pi.*r);
Phi = (S.*cos(theta))./(2.*pi.*r);
end
% Robot Team Spawn is random------------------------------------------------
n = 10; % Number of Robots
XY = 1 * rand(2,n); % Next point, right now completely random
for i=1:n
plot(XY(1,i),XY(2,i),'Ob','MarkerSize',6,'MarkerFaceColor','b')
grid on;
hold on
axis([0 1 0 1])
pause(.5) % how fast each point plots
end
% Invader Team Spawn is random----------------------------------------------
a = 1; % Number of Invaders
AB = 1 * rand(2,a); % Next point, right now completely random
for i=1:a
plot(AB(1,i),AB(2,i),'+r','MarkerSize',6)
hold on
axis([0 1 0 1])
pause(.5) % how fast each point plots
end
% Goal Spawn is fixed------------------------------------------------------
c = 0.5;
d = 0.5;
plot(c,d,'dk','Markersize',6,'MarkerFaceColor','k')
% Quiver----------------------------------------------------------
ui = -1*(AB(1) - .5);
vi = -1*(AB(1) - .5);
[Vxi,Vyi,PSI,PHI] = objectFlow(AB(1),AB(2),ui,vi,5,XY(1,:),XY(2,:));
hold on;
% contour(XY(1,:),XY(2,:),PSI, -2*pi:.2:2*pi, 'ShowText', 'on'); %0:.2:5
% hold on
quiver(XY(1,:),XY(2,:),Vxi,Vyi,0);
hold on;
Shelby Hacker on 10 Jun 2021
Sorry, I want the arrows coming from the blue dots to point to the red point.

Adam Danz on 10 Jun 2021
Edited: Adam Danz on 10 Jun 2021
Two versions; one uses scaled vectors and the other turns off scaling.
To apply this to your work, you just need to define u and v properly. u and v are merely the difference in x and y coordinates between the invader and robots.
% Create robot coordinates (xy)
n = 8;
xy = rand(n,2)*.5+.25; % [x,y] coordinates, nx2
xyi = [.5, 1.1]; % [x,y] coordinates, 1x2
% Compute vector components
u = xyi(1) - xy(:,1); % <------- compute u
v = xyi(2) - xy(:,2); % <------- compute v
% Plot results
figure()
tiledlayout(1,2)
nexttile
hold on
plot(xy(:,1), xy(:,2), 'bo')
plot(xyi(1), xyi(2), 'rx')
quiver(xy(:,1), xy(:,2), u, v)
xlim([0, 1])
ylim([-.5,1.2])
grid on
title('Scaled quivers')
nexttile
hold on
plot(xy(:,1), xy(:,2), 'bo')
plot(xyi(1), xyi(2), 'rx')
quiver(xy(:,1), xy(:,2), u, v, 'off')
xlim([0, 1])
ylim([-.5,1.2])
grid on
title('Not scaled')
Shelby Hacker on 10 Jun 2021
Great work! Thank you for the help.

R2020a

Community Treasure Hunt

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

Start Hunting!