Animated plot with trajectory points

Hey,
I have a problem with an animated plot. I have two vectors X and Y. They have the same lengths and describe displacement of particle in x and y direction. I would like to plot an animation in order to show the Brownian motion of a particle. I have used a code found here: https://www.mathworks.com/help/matlab/ref/animatedline.html. The problem is that it only works if y is a function, for instance y = sin(x). I also used plot function and it only shows the moving point. I would like to plot a trajectory using x and y data points. Is there anyone who knows how to do it and would be so nice to help me?
This is how my code looks like:
x = [0.829848791287224;0.948370872838282;0.989452821303060;0.996024855335383;1;0.969369687549902;0.777472060032697;0.542272394635493;0.303269777962775;0.261866149221235;0.449096037776645;0.342339750593142;0.504837452260772;0.449109692828380;0.247277691338249;0.365412415382128;0.252188006905490;0.272147375214313;0.440129799125733;0.426260245776882;0.355887430155561;0.291149918872756;0.405235503759262;0.502506627500109;0.377850764292477;0.356932635731755;0.346095667999794;0.447465313710477;0.443817170404488;0.438680633046618;0.395574383902766;0.496781871809609;0.419365352164421;0.353852060794176;0.570399700097915;0.489108097147449;0.443857779245358;0.374099544188009;0.483521135410798;0.425585289444380;0.329356516261867];
y = [0.786715362478319;0.807735700665893;0.792037976460206;0.767568910766359;0.748967197199836;0.789931528242810;0.799466207107378;0.764263916683651;0.749928950660036;0.623191339216798;0.471091005010575;0.553053151853275;0.371960939525172;0.645945059477088;0.550268151713167;0.559008326538745;0.558128471197608;0.431157919333808;0.486659202095650;0.307524406245544;0.415792667244793;0.558497859524983;0.654623921193199;0.468670379038440;0.493516697294893;0.299038897262837;0.326864959815376;0.267842627532650;0.501136765022152;0.533613993461779;0.346199723430800;0.555933405488048;0.383769125847753;0.312403419394111;0.403308990402350;0.391980211566912;0.489621684154288;0.425101013176526;0.393683406459521;1;0.690016448809313];
curve = animatedline('Color', 'b', 'LineStyle', '-', 'LineWidth',3);
set(gca, 'XLim', [0 1], 'YLim', [0 1]);
grid on;
for i = 1:length(x)
% addpoints(curve, x(i), y(i));
plot(x(i), y(i), '-b')
drawnow
end
Thank you for your help.
Kind Regards
Ann

 Respuesta aceptada

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 13 de Mayo de 2019
Hi,
You have almost done all the work except for some small points. Here is the finalized one:
x = [0.829848791287224;0.948370872838282;0.989452821303060;0.996024855335383;1;0.969369687549902;0.777472060032697;0.542272394635493;0.303269777962775;0.261866149221235;0.449096037776645;0.342339750593142;0.504837452260772;0.449109692828380;0.247277691338249;0.365412415382128;0.252188006905490;0.272147375214313;0.440129799125733;0.426260245776882;0.355887430155561;0.291149918872756;0.405235503759262;0.502506627500109;0.377850764292477;0.356932635731755;0.346095667999794;0.447465313710477;0.443817170404488;0.438680633046618;0.395574383902766;0.496781871809609;0.419365352164421;0.353852060794176;0.570399700097915;0.489108097147449;0.443857779245358;0.374099544188009;0.483521135410798;0.425585289444380;0.329356516261867];
y = [0.786715362478319;0.807735700665893;0.792037976460206;0.767568910766359;0.748967197199836;0.789931528242810;0.799466207107378;0.764263916683651;0.749928950660036;0.623191339216798;0.471091005010575;0.553053151853275;0.371960939525172;0.645945059477088;0.550268151713167;0.559008326538745;0.558128471197608;0.431157919333808;0.486659202095650;0.307524406245544;0.415792667244793;0.558497859524983;0.654623921193199;0.468670379038440;0.493516697294893;0.299038897262837;0.326864959815376;0.267842627532650;0.501136765022152;0.533613993461779;0.346199723430800;0.555933405488048;0.383769125847753;0.312403419394111;0.403308990402350;0.391980211566912;0.489621684154288;0.425101013176526;0.393683406459521;1;0.690016448809313];
for i = 1:length(x)
plot(x(i), y(i), 'o', 'markersize', 9), hold all
drawnow, pause(.5)
end
set(gca, 'XLim', [0 1], 'YLim', [0 1]); grid on
Good luck.

2 comentarios

darova
darova el 13 de Mayo de 2019
I would add:
axis([min(x) max(x) min(y) max(y)]*1.1)
Ann
Ann el 13 de Mayo de 2019
Thank you all! It works perfectly :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Animation en Centro de ayuda y File Exchange.

Productos

Preguntada:

Ann
el 12 de Mayo de 2019

Comentada:

Ann
el 13 de Mayo de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by