How to draw an animated line using my data?

1 visualización (últimos 30 días)
Jack Zimmerman
Jack Zimmerman el 10 de Feb. de 2017
Comentada: John Chilleri el 12 de Feb. de 2017
%Declaring the balls initial conditions
R_Ball = 2;
initpos.x = 0;
initpos.y = 2.4;
initvel.x = 2;
initvel.y = 4;
gravity.x = 0;
gravity.y = 9.81;
restitution = 0.7;
GroundBall_friction = 0.2;
%Animation timestep
dt = 0.01;
%Executing the animation
pos.x = initpos.x; % initial position
pos.y = initpos.y; % initial position
vel.x = initvel.x; % initial velocity-x
vel.y = initvel.y; % initial velocity-y
t_arc = linspace(0,(2*vel.y)/gravity.y,5000);
for k = 1:5000
%Updating the ball's position
vel.x = vel.x;
vel.y = vel.y - gravity.y*t_arc(k);
pos.x = pos.x + vel.x*t_arc(k);
pos.y = pos.y + vel.y*t_arc(k) - (1/2)*gravity.y*(t_arc(k).^2);
if vel.y < 0 && pos.y < 0
vel.y = (restitution)*vel.y;
vel.y = -vel.y;
vel.x = vel.x + GroundBall_friction*(restitution - 1)*vel.x;
end
clf;
%Drawing the frame
subplot(2,1,1)
hold on
line([0 30],[0 0]);
rectangle('position', [pos.x pos.y R_Ball R_Ball],'Curvature',[1 1],'FaceColor', 'r');
axis([0 30 0 10]);
axis('equal');
hold off
subplot(2,1,2)
axis([0 30 0 10]);
axis('equal');
%Refresh rate
pause(dt)
end
How do I draw an animated line in the subplot(2,1,1) of my x and y velocity over the same time period as my bouncing ball?

Respuesta aceptada

John Chilleri
John Chilleri el 11 de Feb. de 2017
Hello,
In this scenario, I'd recommend just plotting the line every step as the line develops.
You can do this by recording your x and y positions with a counter variable and plotting, I'll paste your code with the changes below:
count = 1; %%%%%%%%%%%%ADDED
%Declaring the balls initial conditions
R_Ball = 2;
initpos.x = 0;
initpos.y = 2.4;
initvel.x = 2;
initvel.y = 4;
gravity.x = 0;
gravity.y = 9.81;
restitution = 0.7;
GroundBall_friction = 0.2;
%Animation timestep
dt = 0.01;
%Executing the animation
pos.x = initpos.x; % initial position
pos.y = initpos.y; % initial position
vel.x = initvel.x; % initial velocity-x
vel.y = initvel.y; % initial velocity-y
t_arc = linspace(0,(2*vel.y)/gravity.y,5000);
for k = 1:5000
%Updating the ball's position
vel.x = vel.x;
vel.y = vel.y - gravity.y*t_arc(k);
pos.x = pos.x + vel.x*t_arc(k);
pos.y = pos.y + vel.y*t_arc(k) - (1/2)*gravity.y*(t_arc(k).^2);
if vel.y < 0 && pos.y < 0
vel.y = (restitution)*vel.y;
vel.y = -vel.y;
vel.x = vel.x + GroundBall_friction*(restitution - 1)*vel.x;
end
clf;
%Drawing the frame
subplot(2,1,1)
hold on
line([0 30],[0 0]);
rectangle('position', [pos.x pos.y R_Ball R_Ball],'Curvature',[1 1],'FaceColor', 'r');
axis([0 30 0 10]);
hold off
subplot(2,1,2)
posxx(count) = pos.x; %%%%%%%%%%%%ADDED
posyy(count) = pos.y; %%%%%%%%%%%%ADDED
plot(posxx, posyy,'r') %%%%%%%%%%%%ADDED
axis([0 30 0 10]);
count = count+1; %%%%%%%%%%%%ADDED
%Refresh rate
pause(dt)
end
Note that I also removed your axis equal commands as they contradict your axis([0 30 0 10]) command (you won't notice a difference without them, but it will be buggy with them).
My last comment is that your x and y positions seem to be the left side of the ball, so you probably will want to figure out the variable that represents the center of the ball (or just shift these to the right by the ball radius by adding ball radius to posxx in the added line).
Hope this helps!
  2 comentarios
Jack Zimmerman
Jack Zimmerman el 11 de Feb. de 2017
Editada: Jack Zimmerman el 11 de Feb. de 2017
Whilst this is VERY helpful. The intention was to plot 2 lines Velocity.x and velocity.y that represent the x and y velocity respectively over time.
John Chilleri
John Chilleri el 12 de Feb. de 2017
Sorry for the misunderstanding, but I'm guessing you've figured out how to do so! If you're struggling, you just need to change what I have added to store the vel.x and vel.y and then plot separately in second subplot with time as x (and vel x/y as the y).

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Block and Blockset Authoring en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by