function ShowPts
%
global xm ym huex huey hndl1 hndl2 hndl3 bu x X L

xm=[-3,3]; ym=[-3,3]; bu=-1;L=10;t=0;h=0.1;
x=[0.1;0.0];X=x*ones(1,L);

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

while get(gca,'Userdata')==1,
   if bu==1,
      gcpnt
   end;
   [t, x]=RK(t,x,h);
   X=[x X(:,1: L-1)];
   set(hndl1,'xdata',X(1,1),'ydata',X(2,1));
   set(hndl2,'xdata',X(1,1:L-1),'ydata',X(2,1:L-1));
   set(hndl3,'xdata',X(1,L-1:L),'ydata',X(2,L-1:L));
   drawnow;
end
clear global xm ym huex huey hndl1 hndl2 hndl3 bu x X L;
closereq;
%end of main

% van der Pol equation
function dx=vP(t,x)
dx=[x(2); 0.5*(1.0-x(1)*x(1))*x(2)-x(1)];

% 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
global huex huey
hf=figure('Units','Normalized','Position',[.2 .2  .7 .7]);
set(hf,'DefaultUicontrolUnits','Normalized');
set(hf, 'Pointer','crosshair');
hupb1=uicontrol(gcf,'Style','Pushbutton','String','Close',...
   'Value',0,'Position',[0.775,0.15,0.1,0.1],...
   'Callback','set(gca,''Userdata'',-1)');
hupb2=uicontrol(gcf,'Style','Pushbutton','String','Clear',...
   'Value',0,'Position',[0.775,0.3,0.1,0.1],...
   'Callback','delete(line)');   
hupb3=uicontrol(gcf,'Style','Pushbutton','String','OK',...
   'Value',0,'Position',[0.775,0.55,0.1,0.1],...
   'Callback','PutDraw');

huex=uicontrol(gcf,'Style','Edit','String','0',...
   'Position',[.75 .8 .15 .05],'HorizontalAlignment','Left');
huey=uicontrol(gcf,'Style','Edit','String','0',...
   'Position',[.75 .7 .15 .05],'HorizontalAlignment','Left');
  
hutx=uicontrol(gcf,'Style','Text','String','x-axis:',...
   'Position',[.68 .8 .06 .05],'HorizontalAlignment','Center');
huty=uicontrol(gcf,'Style','Text','String','y-axis:',...
   'Position',[.68 .7 .06 .05],'HorizontalAlignment','Center');

% Graphics initialization
function InitG
global xm ym huex huey hndl1 hndl2 hndl3 x
hndl1=line('color','r','marker','.','markersize',20,'erase','xor','xdata',x(1),'ydata',x(2));
hndl2=line('color','r','linestyle','-','linewidth',1,'erase','none','xdata',[],'ydata',[]);
hndl3=line('color','b','linestyle','-','linewidth',1,'erase','none','xdata',[],'ydata',[]);
set(gca,'Units','Normalized','Position',[.05 .2 .7 .7]);
axis([xm(1) xm(2) ym(1) ym(2)]);
box on;
title('Rectangular Coordinate');
xlabel('x-axis');
ylabel('y-axis');
grid on;
axis square;
figure(1);

function gcpnt
global huex huey bu x X L
a=get(gca,'CurrentPoint');
x=[a(1,1); a(1,2)];
X=x*ones(1,L);
cx=num2str(x(1));
cy=num2str(x(2));
set(huex,'String',cx);
set(huey,'String',cy);
bu=-1;
  
function bdf
global bu
bu=1;

function PutDraw
global xm ym huex huey hndl1 hndl2 bu x X L
fx=get(huex,'String');
x(1)=str2num(fx);
if (x(1)<xm(1)),
   x(1)=xm(1);
elseif (x(1)>xm(2))
   x(1)=xm(2);
end;
fy=get(huey,'String');
x(2)=str2num(fy);
if (x(2)<ym(1)),
   x(2)=ym(1);
elseif (x(2)>ym(2))
   x(2)=ym(2);
end;
X=x*ones(1,L);
