Delete points after a certian i>
2 views (last 30 days)
Show older comments
Hi everyone,
I'm trying to simulate an object orbiting another object. As by now I've made a plot where the position of the object gets updated for every step with a changing acceleration/velocity in order to change the direction of it (I hope it makes sense; the code is below):
while i<1000
a_1 = G*M/sqrt(x_p1^2+y_p1^2).*direction_p1; %acceleration
v0_1 = v0_1+h.*a_1; %velocity being updated with step_size h times acc.
x_p1 = x_p1+dt*v0_1(1); y_p1 = y_p1+dt*v0_1(2); % x- og y-position
plot(x_p1,y_p1,'r.')
end
Currently it just makes a lot of points which stays there, so that after a couple of orbits, the points are all one big mess placed on top of each other.

Is there a way so that after, let's say, 15 points have been placed the first placed points will start to get deleted so only 15 points will be visible at the time?
(If you can imagine it look like a trail of points after each other with the motion)
I've tried writing something like
if i>10
delete(x_p1(i-10))
end
but then the simulation just stops after i>10. Any ideas on how I can do it?
0 Comments
Answers (1)
Walter Roberson
on 8 Jan 2016
If you have R2014b or later, use animatedline()
2 Comments
Walter Roberson
on 9 Jan 2016
an_line = animatedline('MaxinumNumPoints', 15, 'LineStyle', 'none', 'Marker', '.', 'Color', 'r');
while i<1000
a_1 = G*M/sqrt(x_p1^2+y_p1^2).*direction_p1; %acceleration
v0_1 = v0_1+h.*a_1; %velocity being updated with step_size h times acc.
x_p1 = x_p1+dt*v0_1(1); y_p1 = y_p1+dt*v0_1(2); % x- og y-position
an_line.addpoint(x_p1 ,y_p1);
drawnow();
end
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!