# How to draw an animated line using my data?

5 views (last 30 days)

Show older comments

Jack Zimmerman
on 10 Feb 2017

Commented: John Chilleri
on 12 Feb 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?

##### 0 Comments

### Accepted Answer

John Chilleri
on 11 Feb 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 Comments

John Chilleri
on 12 Feb 2017

### More Answers (0)

### See Also

### Community Treasure Hunt

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

Start Hunting!