function status = stick_vis( t, x, flag, color) % stick_vis: draw an stick figure % stick_vis( [], [], 'init'): prepare a frame for animation % stick_vis( t, x, [], color): draw a stick figure % x = [ th1; th2; om1; om2] % stick_vis( [], [], 'done'): do nothing global E if ischar( t) flag = t; % use first arg as flag else if nargin<3, flag = ''; end % ode standard end figure( E.fig(1)); l = 1.2*sum(E.L); switch( flag) case 'init' % prepare animation frame E.hold = 0; % clear every frame cla; axis( 'equal'); axis( [ -l l -0.4*l l]); set( gca,'Box','on','XTick',[],'YTick',0,'YTickLabel','','YGrid','on'); case '' if E.hold == 0, cla; end px = cumsum( [ 0; E.L(1)*sin(x(1)); E.L(2)*sin(x(1)+x(2))]); py = cumsum( [ 0; E.L(1)*cos(x(1)); E.L(2)*cos(x(1)+x(2))]); % floor % line( [-l,l], [0,0]); % or use grid line % stick figure if nargin<4, color=[0.5,0.5,0.5]; end line( px, py, 'LineWidth', 5, 'Color', color); % time text( 0.4*l, -0.3*l, [ 't=', num2str(t)], 'FontSize', 18); status = E.stop; case 'done' otherwise error( [ ' invalid option: ', flag]); end drawnow;