How to speed up the plot?
6 次查看(过去 30 天)
显示 更早的评论
How to speed up the plot?
Here is the code if you are too lazy to download it :P :
function bouncer
% BOUNCER Simple illustration of gravity.
[z0,h] = initialize_bouncer;
g = 9.8; % Gravity
c = 0.75; % Elasticity
delta = 0.005; % Time step
v0 = 21; % Initial velocity
while v0 >= 1
v = v0;
z = z0;
while all(z >= 0)
set(h,'zdata',z)
drawnow
v = v - delta*g;
z = z + delta*v;
end
v0 = c*v0;
end
finalize_bouncer
end
%-----------------------------------------------
function [z,h] = initialize_bouncer
% INITIALIZE_BOUNCER
% The z-coordinates and the handle for a surf plot of a sphere.
clf
shg
set(gcf,'color','white','numbertitle','off','name',' Bounce')
axes('position',[0 0 1 1])
[x,y,z] = sphere(20);
z = z + 1;
h = surf(x,y,z);
colormap copper
shading interp
axis([-12.5 12.5 -12.5 12.5 0 25.0])
axis square off
view(90,0)
uicontrol('string','TOSS','style','pushbutton', ...
'units','normal','position',[.10 .05 .12 .05], ...
'background','white','fontweight','normal', ...
'enable','off','callback','bouncer')
drawnow
end
%-----------------------------------------------
function finalize_bouncer
set(findobj('string','TOSS'),'fontweight','bold','enable','on')
end
0 个评论
回答(1 个)
Daniel Shub
2012-3-1
IT smells like homework to me ... I hope I am wrong. Can you see how this might help.
cnt = cnt+1;
if ~rem(cnt, 4)
set(h,'zdata',z)
drawnow
end
3 个评论
Daniel Shub
2012-3-1
The slow part of the code is the plotting (specifically the drawnow which causes the screen to refresh). You can see how fast it runs if you comment out the set and drawnow commands. The code I gave causes the screen to only be updated every 4 frames. If you change the 4 to a bigger number you can make the code go faster, but the "movie" will be jumpy.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!