function [ydot,isterminal,direction] = fkt(t,y,flag)
% FKT    system of differential equations 
%        for the jogger-dog problem
%        where the jogger runs with constant 
%        velocity on an ellipse 
global a b m c w
A =  cos(y(3)); B =  sin(y(3));
X = m(1) + a*A; Y = m(2) + b*B;
h = [X;Y] -y(1:2); nh = norm(h);
zs =  (w/nh)*h;

if nargin < 3 | isempty(flag) % normal output
   ydot = [zs;c/sqrt((a*B)^2+(b*A)^2)];
else
   switch(flag)
   case 'events' % at norm(h)=0 there is a singularity
       ydot= nh-(1e-3); % zero crossing at pos_dog=pos_jogger
       isterminal= 1;   % this is a stopping event 
       direction= 0;    % don't care if decrease or increase
   otherwise
       error(['Unknown flag ''' flag '''.']);
   end
end

