How to draw an animated line using my data?
1 次查看(过去 30 天)
显示 更早的评论
%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 个评论
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 Block and Blockset Authoring 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!