function [out1,out2,out3] = pend_event(t,y,flag,w)

if nargin < 3 | isempty(flag),
   out1= [y(2); -w^2*sin(y(1)) ];

else
   switch flag
      case 'init'                   % Return [tspan,y0,options].
         out1=linspace(0,6*pi,200); % tspan
         out2=[0;1];                % y0=col of initial coditions
         out3=odeset('events','on');  % Turn on 'events' option.
       case 'events'
         out1=y(2); % The event is y(2)=0. Here, this means y(1) is a
                    % max or a min. One can have a vector of events.
		    
         out2=0;    % Values are 0 or 1. If this is 0, then we continue
                    % solving when an ``event'' occurs. If this is
                    % 1, then the solution stops at the first
                    % occurence of the event.
		    
         out3=0;    % Values are 1, 0, or -1. This indicates what
                    % direction to take for an event. If out3 is 1
                    % or -1, the event function is increasing or
                    % decreasing, respectively. If out3=0, all
                    % crossings of the events are reported.
      otherwise
         error(['Unknown flag ''' flag '''.']);
   end
end
