function Forced
% main program
global hndl1 hndl2 hndl3 x X bu

bu=-1; m=64; h=2.0*pi/m;
x=[0.0; 0.0]; X=[x x];
InitGUI;
InitG(x);

set(gca,'ButtonDownFcn','bdf')
set(gca,'Userdata',1);

while get(gca,'Userdata')==1,
   set(hndl1,'xdata',X(1,1),'ydata',X(2,1));
   drawnow;
   for j=0:m-1
         if bu==1,
            gcpnt;
            break;
         end;
      t=j*h;
      [t, x]=RK(t,x,h);
     X=[x X(:,1)];
     set(hndl2,'xdata',X(1,1:2),'ydata',X(2,1:2));
   %drawnow;
   end
end
%close(gcf);
closereq;
%end of main

% van der Pol equation
function dx=vP(t,x)
dx=[x(2); -0.1*x(2)-.9*x(1)+.1*cos(t)];

% Runge Kutta method
function [t, x]=RK(t,x,h)
   f1=vP(t,x);
   f2=vP(t+h/2,x+h*f1/2);
   f3=vP(t+h/2,x+h*f2/2);
   f4=vP(t+h,x+h*f3);
   t=t+h;
   x=x+h*(f1+2*(f2+f3)+f4)/6;
   
% Graphics initialization
function InitGUI
  set(gcf,'DefaultUicontrolUnits','Normalized');
  set(gcf, 'Pointer','crosshair');
  hd1=uicontrol(gcf,'Style','Pushbutton','String','Close','Value',0,...
     'Position',[0.85,0.15,0.1,0.1],'Callback','set(gca,''Userdata'',-1)');
  hd2=uicontrol(gcf,'Style','Pushbutton','String','Cls','Value',0,...
     'Position',[0.85,0.3,0.1,0.1],'Callback','delete(line)');
  
% Graphics initialization
function InitG(x)
global hndl1 hndl2 hndl3
  hndl1=line('color','r','marker','.','markersize',20,'erase','none','xdata',x(1),'ydata',x(2));
  hndl2=line('color','b','linestyle','-','linewidth',1,'erase','none','xdata',[],'ydata',[]);
  hndl3=line('color','b','linestyle','-','linewidth',1,'erase','none','xdata',[],'ydata',[]);
  axis([-2 2 -2 2]);
  box on;
  title('Phase Portrait');
  xlabel('x(t)');
  ylabel('x-dot');
  grid on;
  axis square;
  figure(1);

  function gcpnt
  global hndl1 hndl2 hndl3 x X bu
  a=get(gca,'CurrentPoint');
  x=[a(1,1); a(1,2)];
  X=[x x];
  bu=-1;
  
  function bdf
  global hndl x X bu
  bu=1;