function varargout = code16a(t,y,flag)

%  this file modelled after "help odefile"

global A B period

switch flag
case ''                      % Return dy/dt = f(t,y).
   varargout{1} = f(t,y);
case 'events'                % Return [value,isterminal,direction].
  [varargout{1:3}] = events(t,y);
otherwise
   error(['Unknown flag ''' flag '''.']);
end

% -------------------------------------------------------------

function dydt = f(t,y)

global A B period

dydt = zeros(size(y));

dydt(1) = y(2);
dydt(2) = -A*y(1) + B*y(1)^3;
dydt(3) = y(1)*cos(2*pi*t/period);

% -------------------------------------------------------------

function [value,isterminal,direction] = events(t,y)
   value = y(2);   isterminal = 1;   direction = -1;
