function complexfcn(action)
%This function draws a standard plane using a spectrum of
%colors. The user can input a function, and it will draw
%another plane such that z is given the color of f(z) in the
%standard plane. Also a 3D version is drawn with the 3rd axis
%given by log|f(z)|. Using the tools menu, the 3D version can
%be rotated, zoomed in and out, and even a movie can be made.
%change resol below (default=.01) to increase or decrease the 
%resolution.  High resolution will slow down the process.
%D. Marshall 
global w
global x
global y
global z
global zeta
global fform
global Size
global hsize
global xcenter
global ycenter
global resol
global iterations
fname = mfilename;
%warning off
if nargin < 1
   resol=0.01;
   iterations=1;
   Size=4.0;
   hsize=Size/2.;
   deltax=resol*Size;
   xcenter=0.0;
   ycenter=0.0;
   X = -hsize+xcenter:deltax:hsize+xcenter;
   Y=X+ycenter-xcenter;
   [x,y]=meshgrid(X,Y);
   zeta = x+1i*y;
%   action='initialize';
   figure('Position',[100,100,600,600])
   set(gcf,'DoubleBuffer','on')
   messg1=' Type a new function in the box below,';
   messg2=' Then hit enter';
   messg=[messg1,messg2];
   uicontrol(...
        'Style','text',...
        'Units','normalized',...
         'Position', [.6 .1 .35 .4],...
        'String',messg,...
        'Callback','');
   messg1=' Some matlab operations (note the dot):';
   messg2=' a+b, a.*b, a./b, z.^n, exp(a), cos(a), log(a)';
   messg=[messg1,messg2];
   uicontrol(...
        'Style','text',...
        'Units','normalized',...
         'Position', [.6 .35 .35 .1],...
        'String',messg,...
        'Callback','');
   uicontrol(...
        'Style','text',...
        'Units','normalized',...
        'Position', [.6 .35 .05 .03],...
        'Background','white',...
        'Foreground','black',...
        'String',' f(z)=',...
        'Callback','');
   messg1=' To rotate the 3D plot, click and hold mb1';
   messg2=' then rotate wire frame. Try other options under the Tools menu';
   messg=[messg1,messg2];
   uicontrol(...
        'Style','text',...
        'Units','normalized',...
         'Position', [.6 .08 .35 .1],...
        'String',messg,...
        'Callback','');
   uicontrol(...
        'Style','text',...
        'Units','normalized',...
        'Position', [.6 .3 .1 .03],...
        'String',' xcenter  ',...
        'Callback','');
   uicontrol(...
        'Style','text',...
        'Units','normalized',...
        'Position', [.6 .27 .1 .03],...
        'String',' ycenter  ',...
        'Callback','');
   uicontrol(...
        'Style','text',...
        'Units','normalized',...
        'Position', [.6 .24 .1 .03],...
        'String',' resolution  ',...
        'Callback','');
   uicontrol(...
        'Style','text',...
        'Units','normalized',...
        'Position', [.6 .21 .1 .03],...
        'String','  iterations:  ',...
        'Callback','');
   uicontrol(...
        'Style','text',...
        'Units','normalized',...
        'Position', [.6 .18 .1 .03],...
        'String',' Iterate ',...
        'Callback','');
   uicontrol(...
        'Style','text',...
        'Units','normalized',...
        'Position', [.75 .18 .1 .03],...
        'String','more times ',...
        'Callback','');
   uicontrol(...
        'Style','text',...
           'Units','normalized',...
           'Position', [.7 .3 .2 .03],...
        'String',xcenter,...
        'Callback','');
   uicontrol(...
        'Style','text',...
           'Units','normalized',...
           'Position', [.7 .27 .2 .03],...
        'String',ycenter,...
        'Callback','');
   uicontrol(...
        'Style','text',...
           'Units','normalized',...
        'Position', [.7 .21 .033 .03],...
        'String',iterations,...
        'Callback','');
 %  messg=[messg1,messg2];
 %  uicontrol(... 'Units','normalized',...
 %       'Style','text',...
 %       'Position',[330 60 230 50],...
 %       'String',messg,...
 %       'Callback','');
 % uicontrol(...
 %       'Style','text',...
 %       'Position',[330 90 60 21],...
 %       'String',' center = ',...
 %       'Callback','');
 %  callbackStr=[fname '(''center'')'];
 %  uicontrol(...
 %       'Style','edit',...
 %       'Position',[450 90 50 21],...
 %       'Background','white',...
 %       'Foreground','black',...
 %       'String',' 0.  0.',...
 %       'tag','center',...
 %       'Callback',callbackStr);
 %  uicontrol(...
 %       'Style','text',...
 %       'Position',[330 60 100 21],...
 %       'String',' Zoom factor = ',...
 %       'Callback','');
 %  callbackStr=[fname '(''zoom'')'];
 %  uicontrol(...
 %       'Style','edit',...
 %       'Position',[450 60 50 21],...
 %       'Background','white',...
 %       'Foreground','black',...
 %       'String',' +1',...
 %       'tag','zoom',...\Windows\MATLAB\work\complexfcn.m

 %       'Callback',callbackStr);
   callbackStr=[fname '(''iterate'')'];
   uicontrol(...
        'Style','edit',...
        'Units','normalized',...
        'Position', [.7 .18 .033 .03],...
        'Background','white',...
        'Foreground','black',...
        'String',' 1',...
        'tag','noiters',...
        'Callback',callbackStr);
   callbackStr=[fname '(''funct'')'];
   uicontrol(...
        'Style','edit',...
        'Units','normalized',...
        'Position', [.65 .35 .3 .03],...
        'Background','white',...
        'Foreground','black',...
        'String','z+1./z',...
        'tag','fbox',...
        'Callback',callbackStr);
   callbackStr=[fname '(''resolution'')'];
   uicontrol(...
        'Style','edit',...
        'Units','normalized',...
        'Position', [.7 .24 .1 .03],...
        'Background','white',...
        'Foreground','black',...
        'String','.01',...
        'tag','resoli',...
        'Callback',callbackStr);
   callbackStr=[fname '(''zoomin'')'];
   uicontrol(...
        'Style','pushbutton',...
        'Units','normalized',...
        'Position', [.75 .05 .1 .03],...
        'Background','white',...
        'Foreground','black',...
        'String',' zoomin',...
        'tag','zoomin',...
        'Callback',callbackStr);
   callbackStr=[fname '(''zoomout'')'];
   uicontrol(...
        'Style','pushbutton',...
        'Units','normalized',...
        'Position', [.85 .05 .1 .03],...
        'Background','white',...
        'Foreground','black',...
        'String',' zoomout',...
        'tag','zoomout',...
        'Callback',callbackStr);
   %identity map
   %draw standard plane
   subplot(2,2,1)
   surf(x,y,angle(zeta))
   caxis([-pi,pi])
   view(0,90)
   caxis([-pi,pi])
   axis equal
   axis square
   xlabel('real'), ylabel('imaginary')
   shading interp
   colormap(hsv(256))
   title('Standard Plane')
   %now plot argument of f(z) at z via color.
   subplot(2,2,2)
   z=zeta;
   fform=['w=','z+1./z',';'];
   eval(fform)
   surf(x,y,angle(w))
   caxis([-pi,pi])
   view(0,90)
   %%get rid of mesh lines
   %set(h,'linestyle','none')
   %don't distort axes
   %set(gca,'PlotBoxAspectRatio',[1 1 1]);
   axis equal
   axis square
   title('Color = arg f(z)')
   xlabel('real'), ylabel('imaginary')
   shading interp
   colormap(hsv(256))
   %initial view is overhead (looking at x,y plane)
   %%%% uncomment for some interesting
   %%%%% color combinations
%   colormap(flag)
   subplot(2,2,3)
   surf(x,y,log(abs(w)),angle(w))
   caxis([-pi,pi])
   view(-37.5,30)
   rotate3d on
   axis equal
   axis square
   title('Color = arg f(z)  Height=log|f(z)|')
   xlabel('real'), ylabel('imaginary'), zlabel('log-modulus')
   shading interp
   colormap(hsv(256))
   iterations=1;
   drawnow
elseif strcmp(action,'funct')
   Size=4.0;
   hsize=Size/2.;
   deltax=resol*Size;
   xcenter=0.0;
   ycenter=0.0;
   X = -hsize+xcenter:deltax:hsize+xcenter;
   Y=X+ycenter-xcenter;
   [x,y]=meshgrid(X,Y);
   z = x+1i*y;
   iterations=1;
   hndl=findobj('tag','fbox');
   funct=get(hndl,'String');
   fform=['w=',funct,';'];
   eval(fform);
   subplot(2,2,2)
   surf(x,y,angle(w))
   caxis([-pi,pi])
   view(0,90)
   xlim([-hsize+xcenter xcenter+hsize])
   ylim([-hsize+ycenter ycenter+hsize])
   axis equal
   axis square
   title('Color = arg f(z)')
   xlabel('real'), ylabel('imaginary')
   shading interp
   colormap(hsv(256))
   subplot(2,2,3)
   surf(x,y,log(abs(w)),angle(w))
   caxis([-pi,pi])
   view(-37.5,30)
   rotate3d on
   xlim([-hsize+xcenter xcenter+hsize])
   ylim([-hsize+ycenter ycenter+hsize])
   axis equal
   axis square
   title('Color = arg f(z)     Height=log|f(z)|')
   xlabel('real'), ylabel('imaginary'), zlabel('log-modulus')
   shading interp
   colormap(hsv(256))
   uicontrol(...
        'Style','text',...
           'Units','normalized',...
           'Position', [.7 .3 .2 .03],...
        'String',xcenter,...
        'Callback','');
   uicontrol(...
        'Style','text',...
           'Units','normalized',...
           'Position', [.7 .27 .2 .03],...
        'String',ycenter,...
        'Callback','');
   uicontrol(...
        'Style','text',...
           'Units','normalized',...
        'Position', [.7 .21 .033 .03],...
        'String',iterations,...
        'Callback','');
elseif strcmp(action,'resolution')
   hndl=findobj('tag','resoli');
   funct=get(hndl,'String');
   fform3=['resol=',funct,';'];
   eval(fform3);
%   Size=4.0;
   hsize=Size/2.;
   deltax=resol*Size;
%   xcenter=0.0;
%   ycenter=0.0;
   X = -hsize+xcenter:deltax:hsize+xcenter;
   Y=X+ycenter-xcenter;
   [x,y]=meshgrid(X,Y);
   z = x+1i*y;
   eval(fform);
   iterations=1;
   subplot(2,2,2)
   surf(x,y,angle(w))
   caxis([-pi,pi])
   view(0,90)
   axis equal
   axis square
   title('Color = arg f(z)')
   xlabel('real'), ylabel('imaginary')
   xlim([-hsize+xcenter xcenter+hsize])
   ylim([-hsize+ycenter ycenter+hsize])
   shading interp
   colormap(hsv(256))
   drawnow
   subplot(2,2,3)
   surf(x,y,log(abs(w)),angle(w))
   caxis([-pi,pi])
   view(-37.5,30)
   rotate3d on
   xlim([-hsize+xcenter xcenter+hsize])
   ylim([-hsize+ycenter ycenter+hsize])
   axis equal
   axis square
   title('Color = arg f(z)     Height=log|f(z)|')
   xlabel('real'), ylabel('imaginary'), zlabel('log-modulus')
   shading interp
   colormap(hsv(256))
   uicontrol(...
        'Style','text',...
           'Units','normalized',...
           'Position', [.7 .3 .2 .03],...
        'String',xcenter,...
        'Callback','');
   uicontrol(...
        'Style','text',...
           'Units','normalized',...
           'Position', [.7 .27 .2 .03],...
        'String',ycenter,...
        'Callback','');
   uicontrol(...
        'Style','text',...
           'Units','normalized',...
        'Position', [.7 .21 .033 .03],...
        'String',iterations,...
        'Callback','');
elseif strcmp(action,'iterate')
   hndl=findobj('tag','noiters');
   niters=get(hndl,'String');
   nits=['nn=',niters,';'];
   eval(nits);
   for ntimes = 1:nn
      iterations=iterations+1;
      z=w;
      eval(fform);
      subplot(2,2,2)
      surf(x,y,angle(w))
      caxis([-pi,pi])
      view(0,90)
      axis equal
      axis square
      title('Color = arg f(z)')
      xlabel('real'), ylabel('imaginary')
      shading interp
      colormap(hsv(256))
      subplot(2,2,3)
      surf(x,y,log(abs(w)),angle(w))
      caxis([-pi,pi])
      view(-37.5,30)
      rotate3d on
      axis equal
      axis square
      xlim([-hsize+xcenter xcenter+hsize])
      ylim([-hsize+ycenter ycenter+hsize])
      title('Color = arg f(z)     Height=log|f(z)|')
      xlabel('real'), ylabel('imaginary'), zlabel('log-modulus')
      shading interp
      colormap(hsv(256))
      uicontrol(...
        'Style','text',...
           'Units','normalized',...
           'Position', [.7 .3 .2 .03],...
        'String',xcenter,...
        'Callback','');
      uicontrol(...
        'Style','text',...
           'Units','normalized',...
           'Position', [.7 .27 .2 .03],...
        'String',ycenter,...
        'Callback','');
      drawnow
      uicontrol(...
           'Style','text',...
           'Units','normalized',...
           'Position', [.7 .21 .033 .03],...
           'String',iterations,...
           'Callback','');
      drawnow
   end
elseif strcmp(action,'zoomin')
   iterations=1;
%   hndl=findobj('tag','centeri');
%   funct=get(hndl,'String');
%   fform3=['center=',funct,';'];
%   eval(fform3); 
   [xcenter,ycenter]=ginput(1);
   Size=Size/2;
   deltax=resol*Size;
   hsize=Size/2;
   X = -hsize+xcenter:deltax:hsize+xcenter;
   Y=X+ycenter-xcenter;
   [x,y]=meshgrid(X,Y);
   z = x+1i*y;
   eval(fform);
   subplot(2,2,2)
   surf(x,y,angle(w))
   caxis([-pi,pi])
   view(0,90)
   axis equal
   axis square
   xlim([-hsize+xcenter xcenter+hsize])
   ylim([-hsize+ycenter ycenter+hsize])
   title('Color = arg f(z)')
   xlabel('real'), ylabel('imaginary')
   shading interp
   colormap(hsv(256))
   subplot(2,2,3)
   surf(x,y,log(abs(w)),angle(w))
   caxis([-pi,pi])
   view(-37.5,30)
   rotate3d on
   axis equal
   axis square
   title('Color = arg f(z)     Height=log|f(z)|')
   xlabel('real'), ylabel('imaginary'), zlabel('log-modulus')
   shading interp
   colormap(hsv(256))
   uicontrol(...
        'Style','text',...
           'Position', [.65 .13 .033 .03],...
           'Units','normalized',...
           'Position', [.7 .21 .033 .03],...
        'String',iterations,...
        'Callback','');
   uicontrol(...
        'Style','text',...
           'Units','normalized',...
           'Position', [.7 .3 .2 .03],...
        'String',xcenter,...
        'Callback','');
   uicontrol(...
        'Style','text',...
           'Units','normalized',...
           'Position', [.7 .27 .2 .03],...
        'String',ycenter,...
        'Callback','');
elseif strcmp(action,'zoomout')
   iterations=1;
%   hndl=findobj('tag','Sizei');
%   funct=get(hndl,'String');
%   fform3=['size=',funct,';'];
%   eval(fform3); 
   Size=Size*2;
   deltax=resol*Size;
   hsize=Size/2;
   X = -hsize+xcenter:deltax:hsize+xcenter;
   Y=X+ycenter-xcenter;
   [x,y]=meshgrid(X,Y);
   z = x+1i*y;
   eval(fform);
   subplot(2,2,2)
   surf(x,y,angle(w))
   caxis([-pi,pi])
   view(0,90)
   axis equal
   axis square
   xlim([-hsize+xcenter xcenter+hsize])
   ylim([-hsize+ycenter ycenter+hsize])
   title('Color = arg f(z)')
   xlabel('real'), ylabel('imaginary')
   shading interp
   colormap(hsv(256))
   subplot(2,2,3)
   surf(x,y,log(abs(w)),angle(w))
   caxis([-pi,pi])
   view(-37.5,30)
   rotate3d on
   axis equal
   axis square
   title('Color = arg f(z)     Height=log|f(z)|')
   xlabel('real'), ylabel('imaginary'), zlabel('log-modulus')
   shading interp
   colormap(hsv(256))
   uicontrol(...
        'Style','text',...
           'Units','normalized',...
           'Position', [.7 .21 .033 .03],...
        'String',iterations,...
        'Callback','');
   uicontrol(...
        'Style','text',...
           'Units','normalized',...
           'Position', [.7 .3 .2 .03],...
        'String',xcenter,...
        'Callback','');
   uicontrol(...
        'Style','text',...
           'Units','normalized',...
           'Position', [.7 .27 .2 .03],...
        'String',ycenter,...
        'Callback','');
end
