Better way to plot a ring to speed up function

2 ビュー (過去 30 日間)
Francesco Virga
Francesco Virga 2017 年 3 月 9 日
コメント済み: Jan 2017 年 3 月 13 日
My program for a school project plots and updates planet trajectories. I'm plotting a ring around saturn to seem more realistic, but its slowing down my program quite a bit because of plotting so many points. Is there a better way to do this? Here's my code plotting the initial ring (r_S is the position of saturn), using a random radius that works with my plot for now so ignore that)
angle=0:0.5:2*pi;
xring = 60300000 * cos(angle) + r_S(1);
yring = 60300000 * sin(angle) + r_S(2);
zring = ones(1,length(xring))*r_S(3);
ring = plot3(xring,yring,zring,'LineWidth',1)
and then position is updated in a for loop along with position of all other planets:
ring.XData = 60300000000 * cos(angle) + r_S(1);
ring.YData = 60300000000 * sin(angle) + r_S(2);
ring.ZData = ones(1,length(xring))*r_S(3);
  1 件のコメント
John BG
John BG 2017 年 3 月 9 日
Hi Francesco
what would be an acceptable delay?
or how much faster (what percentage delay shorter run script) do you want it to be?
I ask this because repeating 9 times (Pluto included) the simplified trajectories (planets move following ellipses not circles, ref Kepler) the slowest delay is 0.08 seconds
.
r_S=[1 2 3]
d=zeros(1,8)
angle=0:0.5:2*pi;
tic
for k=1:1:numel(d)
for s=9 % Pluto included
xring = 603*10^(k-1) * cos(angle) + r_S(1);
yring = 603*10^(k-1) * sin(angle) + r_S(2);
zring = ones(1,length(xring))*r_S(3);
figure(1);ring = plot3(xring,yring,zring,'LineWidth',1)
hold all
end
d(k)=toc
end
d
=
Columns 1 through 3
0.023922579111702 0.037325921844445 0.057681586879720
Columns 4 through 6
0.107874977041671 0.139749781220627 0.153394591557817
Columns 7 through 8
0.161977765533335 0.173254896876587
figure(2);stem(d)
.
please comment so an answer can be developed
John BG

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2017 年 3 月 12 日
Your method to create the graphics object once and updating the XData et.c later on is efficient.
This ring is created by 13 points only. You could store the value of cos(angle) and cos(angle) in a variable to avoid the repeated calculations, but I do not assume that 26 trigonometric values slow down the computations remarkably.
I suggest to use the profile to find the bottleneck of the code. If it is the drawnow command, updating the graphic elements takes the most time. But I do not assume, that this simple line really matters. Perhaps another calculation is responsible for the observed slowdown.
  2 件のコメント
Francesco Virga
Francesco Virga 2017 年 3 月 12 日
Okay I see. I'll try using the profile to trouble shoot a bit. The reason I asked the question is because when I added the ring, my program seemed to slow down a noticeable amount. On another note, should I be first calculating all positions into arrays and then use another loop to plot? It seems by what you mentioned this may speed up the program a bit. Here's my while loop, the traj function takes the mass, position, and velocity of each planet and uses force and kinematic equations to return the position and velocity of that planet a day later.
while i <= 100 % looping through 1 year, updated position and velocity data each day
[r_Me,v_Me] = traj(mMe,r_Me,v_Me);
[r_V,v_V] = traj(mV,r_V,v_V);
[r_E,v_E] = traj(mE,r_E,v_E);
[r_Ma,v_Ma] = traj(mMa,r_Ma,v_Ma);
[r_J,v_J] = traj(mJ,r_J,v_J);
[r_S,v_S] = traj(mS,r_S,v_S);
% updating plot
Me.XData = r_Me(1);
Me.YData = r_Me(2);
Me.ZData = r_Me(3);
addpoints(MeL,r_Me(1),r_Me(2),r_Me(3));
V.XData = r_V(1);
V.YData = r_V(2);
V.ZData = r_V(3);
addpoints(VL,r_V(1),r_V(2),r_V(3));
E.XData = r_E(1);
E.YData = r_E(2);
E.ZData = r_E(3);
addpoints(EL,r_E(1),r_E(2),r_E(3));
Ma.XData = r_Ma(1);
Ma.YData = r_Ma(2);
Ma.ZData = r_Ma(3);
addpoints(MaL,r_Ma(1),r_Ma(2),r_Ma(3));
J.XData = r_J(1);
J.YData = r_J(2);
J.ZData = r_J(3);
addpoints(JL,r_J(1),r_J(2),r_J(3));
S.XData = r_S(1);
S.YData = r_S(2);
S.ZData = r_S(3);
addpoints(SL,r_S(1),r_S(2),r_S(3));
ring.XData = 60300000000 * cos(angle) + r_S(1);
ring.YData = 60300000000 * sin(angle) + r_S(2);
ring.ZData = ones(1,length(xring))*r_S(3);
drawnow
i = i+1;
end
The addpoints line is to simply add a trailing line behind each planet to observe path.
Jan
Jan 2017 年 3 月 13 日
The slowdown can by caused by an automatic switch between the Painters and the OpenGL renderer, when the RendererMode of the figure is set to 'auto'.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by