How to draw an animated line using my data?

조회 수: 4 (최근 30일)
Jack Zimmerman
Jack Zimmerman 2017년 2월 10일
댓글: John Chilleri 2017년 2월 12일
%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?

채택된 답변

John Chilleri
John Chilleri 2017년 2월 11일
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
Jack Zimmerman
Jack Zimmerman 2017년 2월 11일
편집: Jack Zimmerman 2017년 2월 11일
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 2017년 2월 12일
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).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by