function spring_pendulum_anim2( varargin ) %integrate equations of motion [t , X ] = spring_pendulum_int ( varargin {:}); %extract length and angle in time r = X(: ,1); th = X(: ,3); %find position of the pendulum bob: rm = [ r .*sin( th ) , -r .*cos( th )]; %find bounds: maxr = max(abs( r )); xmin = min( rm(: ,1)) - maxr /10; xmax = max( rm(: ,1))+ maxr /10; ymin = min( rm(: ,2)) - maxr /10; ymax = max([0 ,max( rm(: ,2))])+ maxr /10; %create and clear figure h = figure(1); clf( h ); % create a circle and a square: a = 0:pi/100:2*pi; s = [ -1 -1 1 1 -1; -1 1 1 -1 -1]; xscirc = maxr /50*sin( a ); yscirc = maxr /50*cos( a ); xssquare = maxr /50* s(1 ,:); yssquare = maxr /50* s(2 ,:); %create the pendulum components %draw the pivot (centered at origin) and the mass: fill( xssquare , yssquare ,'g'); hold on bob = fill( xscirc + rm(1 ,1) , yscirc + rm(1 ,2) ,'b'); %connect mass and pivot: arm = plot([0 , rm(1 ,1)] ,[0 ,rm(1 ,2)] ,'r','LineWidth',2); %plot track so far: track = plot( rm(1 ,1) , rm(1 ,2) ,'k--'); %set axes to proper values: axis equal ; grid on; axis([ xmin xmax ymin ymax ]); %step in time: for i =1:length( t ) %update all moving elements set( bob ,'XData', xscirc + rm(i ,1) ,'YData', yscirc + rm(i ,2)); set( arm ,'XData',[0 , rm(i ,1)] ,'YData',[0 , rm(i ,2)]); set( track ,'XData', rm(max([ i -100 ,1]): i ,1) ,... 'YData', rm(max([ i -100 ,1]): i ,2)); %pause for length of time step if i < length( t ) pause( t( i +1) - t( i )); end end