home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 3.ddi / DEMOS.DI$ / PEAKS.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  2.2 KB  |  75 lines

  1. function  [xz,y,z] = peaks(arg1,arg2);
  2. %PEAKS    A sample function of two variables.
  3. %    PEAKS is a function of two variables, obtained by translating and
  4. %    scaling Gaussian distributions, which is useful for demonstrating
  5. %    MESH, SURF, PCOLOR, CONTOUR, etc.
  6. %    There are several variants of the calling sequence:
  7. %
  8. %        Z = PEAKS;
  9. %        Z = PEAKS(N);
  10. %        Z = PEAKS(V);
  11. %        Z = PEAKS(X,Y);
  12. %
  13. %        PEAKS;
  14. %        PEAKS(N);
  15. %        PEAKS(V);
  16. %        PEAKS(X,Y);
  17. %
  18. %        [X,Y,Z] = PEAKS;
  19. %        [X,Y,Z] = PEAKS(N);
  20. %        [X,Y,Z] = PEAKS(V);
  21. %
  22. %    The first variant produces a 49-by-49 matrix.
  23. %    The second variant produces an N-by-N matrix.
  24. %    The third variant produces an N-by-N matrix where N = length(V).
  25. %    The fourth variant evaluates the function at the given X and Y,
  26. %    which must be the same size.  The resulting Z is also that size.
  27. %
  28. %    The next four variants, with no output arguments, do a SURF
  29. %    plot of the result.
  30. %
  31. %    The last three variants also produce two matrices, X and Y, for
  32. %    use in commands such as PCOLOR(X,Y,Z) or SURF(X,Y,Z,DEL2(Z)).
  33. %
  34. %    If not given as input, the underlying matrices X and Y are
  35. %        [X,Y] = MESHGRID(V,V) 
  36. %    where V is a given vector, or V is a vector of length N with
  37. %    elements equally spaced from -3 to 3.  If no input argument is
  38. %    given, the default N is 49.
  39.  
  40. %    CBM, 2-1-92, 8-11-92.
  41. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  42.  
  43. if nargin == 0
  44.     [x,y] = meshgrid(-3:1/8:3);
  45. elseif nargin == 1
  46.     if max(size(arg1)) == 1
  47.         [x,y] = meshgrid(-3:6/(arg1-1):3);
  48.     else
  49.         [x,y] = meshgrid(arg1,arg1);     
  50.     end
  51. else
  52.     x = arg1; y = arg2;
  53. end
  54.  
  55. z =  3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ...
  56.    - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ...
  57.    - 1/3*exp(-(x+1).^2 - y.^2);
  58.  
  59. if nargout > 1
  60.     xz = x;
  61. elseif nargout == 1
  62.     xz = z;
  63. else
  64.     % Self demonstration
  65.     disp(' ')
  66.     disp('z =  3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... ')
  67.     disp('   - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... ')
  68.     disp('   - 1/3*exp(-(x+1).^2 - y.^2) ')
  69.     disp(' ')
  70.     surf(x,y,z)
  71.     axis([min(min(x)) max(max(x)) min(min(y)) max(max(y)) ...
  72.           min(min(z)) max(max(z))])
  73.     xlabel('x'), ylabel('y'), title('Peaks')
  74. end
  75.