function [sys,x0]=pndanim2(t,x,u,flag,ts);
%PNDANIM2 S-function for animating the motion of a double pendulum.

%	Ned Gulley, 6-21-93
%	Copyright (c) 1984-94 by The MathWorks, Inc.
global PendAnim2

if flag==2,
    if any(get(0,'Children')==PendAnim2),
      if strcmp(get(PendAnim2,'Name'),'dblpend1 Animation'),
    	set(0,'currentfigure',PendAnim2);
    	hndl=get(gca,'UserData');
    	b=1; c=2;
    	xSFP=0; ySFP=0;
    	xBFP=xSFP+b*sin(u(1)); yBFP=ySFP-b*cos(u(1));
    	xMC=xBFP+c*sin(u(2)); yMC=yBFP-c*cos(u(2));
    	x=[xSFP xBFP NaN xBFP xMC];
    	y=[ySFP yBFP NaN yBFP yMC];
    	plot(xSFP,ySFP,'.','EraseMode','none','MarkerSize',20);
    	set(hndl,'XData',x,'YData',y);
    	drawnow;
      end
    end
    sys=[];

elseif flag == 4 % Return next sample hit
  
  % ns stores the number of samples
  ns = t/ts;

  % This is the time of the next sample hit.
  sys = (1 + floor(ns + 1e-13*(1+ns)))*ts;

elseif flag==0,

    % Initialize the figure for use with this simulation
    animinit('dblpend1 Animation');
	[flag,PendAnim2] = figflag('dblpend1 Animation');
    axis([-2 2 -3 2]);
    hold on;

    % Set up the geometry for the problem
    % SFP=Space Fixed Pivot
    % BFP=Body Fixed Pivot
    b=1; c=2;
    xSFP=0; ySFP=0;
    xBFP=xSFP; yBFP=ySFP-b;
    xMC=xBFP; yMC=yBFP-c;
    x=[xSFP xBFP NaN xBFP xMC];
    y=[ySFP yBFP NaN yBFP yMC];
    plot(xSFP,ySFP,'.','EraseMode','none','MarkerSize',25);
    hndl=plot(x,y,'EraseMode','background','LineWidth',5);
    set(gca,'AspectRatio',[1 1]);
    set(gca,'UserData',hndl);

    sys=[0 0 0 2 0 0];
    x0=[];

end;

