How do I ensure that the animation remain within the axis limit?

4 views (last 30 days)
I defined an axis limit for my simulation. However, after my simulation is running, the animation still goes out of the limit that was set. How can I ensure that my animations stay within the defined axis? I do not want to increase the axis limit, instead I would like to display an error message to show that animation is required to stay within the axis limit that was defined. Thank you
clear;
close all;
%Number of agents
prompt = 'Enter number of Agents: ';
Agents = input(prompt);
if Agents > 200
error('Error! Too many agents, decrease the number of agents');
end
if Agents < 2
error('Error! Too little agents, increase the number of agents');
end
%Number of iterations
Step_Num = 600;
%Initial condition
D = 2;
p = 10*randn(D,Agents); %Vector Position p
v = 5*randn(D,Agents); %Vector Velocity v
%Separation/Collision Avoidance
S = 1; %Controls the distance between agents
%Bigger number, further the agents are from each other
%Cohesion/Flock Centering
K = 1; %Controls the collision between the agents
%Bigger number the smaller the flock
%Alignment/Velocity Matching
M = 1; %Controls the flock motion
%Separation/Collision avoidance gain
ca = 5; %Larger gain -- agents more spread out
%Cohesion/Flock centering gain
fc = 0.1; %Larger gain -- flock centering is smaller
%Time
t = 0.01;
%Accleration of agents
acc = 10; %Controls the acceleration speed of the agents
%Axis Limit
Li = 50;
%Flocking Model
result = zeros(Step_Num,Agents);
for n=1:Step_Num %For every iteration from 1 to desired step
s = zeros(D,Agents); %Collision avoidance vector / Seperation Vector
k = zeros(D,Agents); %Flock centering vector / Cohesion Vector
vx = sum(v(1,:))/Agents; %Velocity of X direction
vy = sum(v(2,:))/Agents; %Velocity of Y direction
m = [-vx;vy]; %Average velocity
norm_m = norm(m);
if (norm_m > acc) %If statement to control the acceleration of the
agents
m = acc*m/norm_m;
end
for i = 1:Agents
for j = 1:Agents
if i~= j %Distance = 0 when i = j (ignore)
r = (p(:,j)-p(:,i)); %Distance between two points i & j
d = sqrt(r(1)^2+(r(2)^2)); %Euclidean distance
s(:,i) = s(:,i) - ca*r/d^2; %Collision avoidance/Seperation
k(:,i) = k(:,i) + fc*r; %Flock centering/Cohesion
end
end
v(:,i) = S*s(:,i)+ K*k(:,i)+ M*m; %Total Velocity
p(:,i) = p(:,i) + v(:,i)*t; %New Position formula p = p+vt
end
%Plot simulations
result(D*n-1,:) = p(1,:);
result(D*n,:) = p(2,:);
plot(p(1,:),p(2,:),'k.','LineWidth',3,'Markersize',15);
grid on;
axis([-Li Li -Li Li]); %Axis limit
xlabel('X-axis') %X-axis
ylabel('Y-axis') %Y-axis
title(['Step Number :', num2str(n)]);
F(:,n) = getframe;
end

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 19 Mar 2019
Well you could achieve that by doing something like:
plot(p(1,:),p(2,:),'k.','LineWidth',3,'Markersize',15);
axPcurr = axis;
if axPcurr(1) < -Li || axPcurr(2) > Li axPcurr(3) < -Li || axPcurr(4) > Li
% Insert whatever limit-breaking handling you desire here
end
But I think a better way of going about this is to let your flock wrap around so that points leaving over an edge would appear at the opposite edge, perhaps something like this (untested):
plot(mod(p(1,:)+Li,2*Li),mod(p(2,:)+Li,2*Li))
Then if you need the tickmark to be exactly for the original points you can set them manually and your flock would never escape.
HTH
  3 Comments
Bjorn Gustavsson
Bjorn Gustavsson on 20 Mar 2019
You set m in your code, for the x-component to the negative of the average x-velocity, and the y-component to the average x-velocity. If you want your flock to move in a prescribed direction you have to set your m t something more predictable/prescribed. Maybe you can use something like the average speed and then use that to average velocity vector in your desired direction:
m = v_avg*[cos(phi_dir_of_desire);sin(phi_dir_of_desire)];
HTH
Jaren Ho
Jaren Ho on 21 Mar 2019
Thanks! I managed to set the flock to move to 8 different directions. However, im unable to control the acceleration of the flock to move to the specified direction.
%Flock Movement Direction
m = [cos(pi/2); sin(1)]; %North Direction
%Accleration of agents
acc = 5; %Controls the acceleration speed of the agents
%Flocking model
for n=1:Step_Num %For every iteration from 1 to desired step
s = zeros(D,Agents); %Collision avoidance vector / Seperation Vector
k = zeros(D,Agents); %Flock centering vector / Cohesion Vector
vx = sum(v(1,:))/Agents; %Velocity of X direction
vy = sum(v(2,:))/Agents; %Velocity of Y direction
av = [vx;vy]; %Average velocity
norm_m = norm(av);
if (norm_m > acc) %If statement to control the acceleration of the agents
av = acc*av/norm_m;
end
for i = 1:Agents
for j = 1:Agents
if i~= j %Distance = 0 when i = j (ignore)
r = (p(:,j)-p(:,i)); %Distance between two points i & j
d = sqrt(r(1)^2+(r(2)^2)); %Euclidean distance
s(:,i) = s(:,i) - ca*r/d^2; %Collision avoidance/Seperation
k(:,i) = k(:,i) + fc*r; %Flock centering/Cohesion
end
end
v(:,i) = S*s(:,i)+ K*k(:,i)+ M*m*av; %Total Velocity
p(:,i) = p(:,i) + v(:,i)*t; %New Position formula p = p+vt
end

Sign in to comment.

More Answers (0)

Categories

Find more on Simulate Responses to Biological Variability and Doses in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!