How to speed up the plot?

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

답변 (1개)

Daniel Shub
Daniel Shub 2012년 3월 1일

0 개 추천

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

BabyMilo
BabyMilo 2012년 3월 1일
This is not homework :)
Im just learning matlab by myself at the moment.
I can make the plot quicker using your code but can you explain what does to?
Many thanks.
BabyMilo
BabyMilo 2012년 3월 1일
what does it do*
i mean
Daniel Shub
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.

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

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

질문:

2012년 3월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by