% Added pauses to inforce uniform frame rate. 27 Oct 09 JMH. duration = 10; samp_per_sec = 20; x = pendulum(duration,samp_per_sec); t = 0:1/samp_per_sec:duration; l = .25; n = 3; figure(1), clf set(gcf,'doublebuffer','on') % keep figure from flashing in older MATLABs for k=1:n fill([-.5 .5 .5 -.5],[0 0 -.5*l -.5*l],.5*[1 1 1]); hold on plot([0 0], l*[0 4],'-','color',.7*[1 1 1]) set(gca,'YDir','Reverse') axis equal, axis([-.5 .5 -.099 1]) end xlabel(sprintf('t = %f',0)) for j=1:length(t) tic; q = x(j,:)'; % horizontal displacements if j>1, delete(plt1(k)), delete(plt2(k)), end % delete old chain plt1(k) = plot([q;0],(n:-1:0)*l,'k-','linewidth',1); hold on % draw new one (black line) plt2(k) = plot(q, (n:-1:1)*l,'b.','markersize',40); % draw new one (blue masses) xlabel(sprintf('t = %f',t(j))) set(gca,'YDir','Reverse') axis equal, axis([-.5 .5 -.099 1]) drawnow if j==1, input('hit return to start'), end drawtime = toc; if (drawtime) < 1/samp_per_sec % kill time pause(1/samp_per_sec - drawtime) end end