How can animate my data point ?

1 view (last 30 days)
Deepika
Deepika on 26 Sep 2022
Commented: Dev on 6 Oct 2022
hello...I need to plot the moving point in such a way that when this moving point go at boundary of the simulation area then it will not reflect back but it will go outside the boundary and disappear and then at the same time from the opposite direction at same angle (at which the previous point was disappeared ) ,the new point will get appear.
Here is my code,It works good but still not got the desired output so please help me for getting the output as mentioned above.....
clear all;
clc;
% ................parameters........................
MAX_X= 100;
MAX_Y =100;
NODES = 50;
tim_sim=3;
alpha=0.99;
velocity =10;
direction =60;
mean_velocity=mean(randn(5),'all');
mean_direction=mean(randn(5),'all');
%....................scan all existing nodes..................
x_tmp1=[];
y_tmp1=[];
for nb=1:NODES
x= (0 + (MAX_X-0)*rand);
y= (0 + (MAX_Y-0)*rand);
%..............node position in every instant t...........
for t=1:tim_sim
%...calculate the new position by using the new_formula of Gauss-Markov...
x_tmp = x + velocity * cos(direction);
x_tmp1=[x_tmp1 x_tmp];
y_tmp = y + velocity * sin(direction);
y_tmp1=[y_tmp1 y_tmp];
normal_velocity=normrnd(0,1);
normal_direction=normrnd(0,1);
%..calculate the new velocity and new direction by using the newformula of
%Gauss-Markov..
velocity_tmp=(alpha*velocity+(1-alpha)*mean_velocity+sqrt(1-alpha*alpha)*normal_velocity);
direction_tmp=(alpha*direction+(1-alpha)*mean_direction+sqrt(1-alpha*alpha)*normal_direction);
% .....node bounces on the margins......
if (x_tmp1<0)
x_tmp1 = -1.*x_tmp1;
direction = pi-direction;
mean_direction = pi-mean_direction;
end
if (x_tmp1>MAX_X)
x_tmp1 = 2*MAX_X - x_tmp1;
direction = pi-direction;
mean_direction = pi-mean_direction;
end
if (y_tmp1<0)
y_tmp1 = -1.*y_tmp1;
direction = -direction;
mean_direction = -1*mean_direction;
end
if (y_tmp1>MAX_Y)
y_tmp1 = 2*MAX_Y - y_tmp1;
direction = -direction;
mean_direction = -1*mean_direction;
end
% plot(x_tmp1,y_tmp1,'color',[0.3 0.3 1])
end
%.......................Animation.......................
n=length(y_tmp1);
% plot(x_tmp1,y_tmp1,'*')
% axis([0 MAX_X 0 MAX_Y])
% pause(0.05)
for i=1:n
plot(x_tmp1(i),y_tmp1(i),'*')
axis([0 MAX_X 0 MAX_Y])
pause(0.05)
end
end
xlabel('X (meters)');
ylabel('Y (meters)');
  1 Comment
Dev
Dev on 6 Oct 2022
I understand that you want to animate your data point in a particular way such that when it disappears on one side, it reappears from the opposite side with the same velocity.
You can change the conditions of the if’ statement to be such that whenever
if x_tmp>MAX_X:
x_tmp=x_tmp-MAX_x %so that it appears from the other side, without changing direction.
similarly,
if x_tmp<0:
x_tmp=MAX_x+x_tmp
And same for y_tmp as well. Additionally, x_tmp1 is an array of x_tmp so while comparing with 0 or MAX_x, you should compare with x_tmp and not x_tmp1.
I hope this resolves your query.

Sign in to comment.

Answers (0)

Categories

Find more on Animation in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!