How to draw an animated line using my data?

5 ビュー (過去 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 Center および File ExchangeSpecifying 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