home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / PLOTXYZ.DI$ / SPHERE.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  841 b   |  34 lines

  1. function [xx,yy,zz] = sphere(n)
  2. %SPHERE    Generate sphere.
  3. %    [X,Y,Z] = SPHERE(N) generates three (n+1)-by-(n+1)
  4. %    matrices so that SURF(X,Y,Z) produces a unit sphere.
  5. %
  6. %    [X,Y,Z] = SPHERE uses N = 20.
  7. %
  8. %    SPHERE(N) and just SPHERE graph the sphere as a SURFACE
  9. %    and do not return anything.
  10. %
  11. %    See also CYLINDER.
  12.  
  13. %    Clay M. Thompson 4-24-91, CBM 8-21-92.
  14. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  15.  
  16. if nargin == 0, n = 20; end
  17.  
  18. % -pi <= theta <= pi is a row vector.
  19. % -pi/2 <= phi <= pi/2 is a column vector.
  20. theta = (-n:2:n)/n*pi;
  21. phi = (-n:2:n)'/n*pi/2;
  22. cosphi = cos(phi); cosphi(1) = 0; cosphi(n+1) = 0;
  23. sintheta = sin(theta); sintheta(1) = 0; sintheta(n+1) = 0;
  24.  
  25. x = cosphi*cos(theta);
  26. y = cosphi*sintheta;
  27. z = sin(phi)*ones(1,n+1);
  28.  
  29. if nargout == 0
  30.    surf(x,y,z)
  31. else
  32.    xx = x; yy = y; zz = z;
  33. end
  34.