%Exa = 1;
clf
switch Exa
case 1             % plot a curve in space
  t = 0:0.1:5*pi;
  x = cos(t);  y = 2*sin(t);  z = t/(2*pi);
  plot3(x,y,z)
  grid on
  view(25,45);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
case 2             % plot a surface in space
  [xx,yy] = meshgrid(1:6,-1:3)
  mesh(xx,yy,xx.^2 - yy.^2)
  xlabel('x'); ylabel('y');

case 3             % different forms for surfaces
  [xx,yy] = meshgrid(-2:0.1:2,-1:0.1:3);
  zz = exp(-xx.^2 - 0.3*yy.^2);

  figure(1)
  mesh(xx,yy,zz)
  xlabel('x'); ylabel('y');

  figure(2)
  meshc(xx,yy,zz)
  xlabel('x'); ylabel('y');
 
  figure(3)
  surf(xx,yy,zz)
  xlabel('x'); ylabel('y');

  figure(4)
  surfc(xx,yy,zz)
  xlabel('x'); ylabel('y');

  figure(5)
  contour(xx,yy,zz)
  xlabel('x'); ylabel('y');

  figure(6)
  cc = 0:0.1:1;
  [C,h] = contour(xx,yy,zz,cc);
  clabel(C,h,cc,'FontSize',10);
  xlabel('x'); ylabel('y');
  axis equal

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
case 4       % generate a vector field
  xvec = -1:0.2:1.5; yvec = -1:0.2:1.5;
  [x,y] = meshgrid(xvec,yvec);
  x = x(:); y = y(:);  % convert the matrix to a column vector.
  
  figure(1)
  quiver(x,y,y,-x,0)   % no scaling applied
  
  figure(2)
  scale = 1.5;
  quiver(x,y,y,-x,scale)
  axis([-1 1 -1 1])

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
case 5  % plot a gradient vector field
  xvec = linspace(-1,1,15); yvec = linspace(-1,1,15);
  [x,y] = meshgrid(xvec,yvec);
%  function z = f(x,y)
%     z = x.^2 - 2*y.^2;
%  endfunction
  f = @(x,y)x.^2-2*y.^2;

  figure(1)
  mesh(x,y,f(x,y))
  xlabel('x'); ylabel('y');

  figure(2)
  [dfdx,dfdy] = gradient(f(x,y));
  quiver(x,y,dfdx,dfdy)
  axis equal
  xlabel('x'); ylabel('y');

  % do it manually, by difference quotients
%  x = x(:); y = y(:);    % convert the matrix to a column vector. not required any more
  dx = 0.01; dy = 0.001;  % for the numerical derivatives
  dfdx = (f(x+dx/2,y)-f(x-dx/2,y))/dx; % partial derivative with respect to x
  dfdy = (f(x,y+dy/2)-f(x,y-dy/2))/dy; % partial derivative with respect to y 

  figure(3)
  quiver(x,y,dfdx,dfdy)
  axis equal
  xlabel('x'); ylabel('y');
end%switch
