
Better way to plot a ring to speed up function
2 views (last 30 days)
Show older comments
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 Comment
John BG
on 9 Mar 2017
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
Answers (1)
Jan
on 12 Mar 2017
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 Comments
Jan
on 13 Mar 2017
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'.
See Also
Categories
Find more on Graphics Performance 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!