Delete points after a certian i>

조회 수: 2 (최근 30일)
NaGi
NaGi 2016년 1월 8일
댓글: Walter Roberson 2016년 1월 9일
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?

답변 (1개)

Walter Roberson
Walter Roberson 2016년 1월 8일
If you have R2014b or later, use animatedline()
  댓글 수: 2
NaGi
NaGi 2016년 1월 8일
I'm not sure how to use animatedline() - isn't there a way with what I've written now or could you maybe tell me what to do to use this with animatedline?
Walter Roberson
Walter Roberson 2016년 1월 9일
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

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by