]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/peaks.m
update packages
[CreaPhase.git] / octave_packages / m / plot / peaks.m
1 ## Copyright (C) 2007-2012 Paul Kienzle
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {} peaks ()
21 ## @deftypefnx {Function File} {} peaks (@var{n})
22 ## @deftypefnx {Function File} {} peaks (@var{x}, @var{y})
23 ## @deftypefnx {Function File} {@var{z} =} peaks (@dots{})
24 ## @deftypefnx {Function File} {[@var{x}, @var{y}, @var{z}] =} peaks (@dots{})
25 ## Generate a function with lots of local maxima and minima.  The function
26 ## has the form
27 ##
28 ## @tex
29 ## $f(x,y) = 3 (1 - x) ^ 2 e ^ {\left(-x^2 - (y+1)^2\right)} - 10 \left({x \over 5} - x^3 - y^5)\right) - {1 \over 3} e^{\left(-(x+1)^2 - y^2\right)}$
30 ## @end tex
31 ## @ifnottex
32 ## @verbatim
33 ## f(x,y) = 3*(1-x)^2*exp(-x^2 - (y+1)^2) ...
34 ##          - 10*(x/5 - x^3 - y^5)*exp(-x^2-y^2) ...
35 ##          - 1/3*exp(-(x+1)^2 - y^2)
36 ## @end verbatim
37 ## @end ifnottex
38 ##
39 ## Called without a return argument, @code{peaks} plots the surface of the
40 ## above function using @code{mesh}.  If @var{n} is a scalar, the @code{peaks}
41 ## returns the values of the above function on a @var{n}-by-@var{n} mesh over
42 ## the range @code{[-3,3]}.  The default value for @var{n} is 49.
43 ##
44 ## If @var{n} is a vector, then it represents the @var{x} and @var{y} values
45 ## of the grid on which to calculate the above function.  The @var{x} and
46 ## @var{y} values can be specified separately.
47 ## @seealso{surf, mesh, meshgrid}
48 ## @end deftypefn
49
50 ## Expression for the peaks function was taken from the following paper:
51 ## http://www.control.hut.fi/Kurssit/AS-74.115/Material/GENALGgoga.pdf
52
53 function [X_out, Y_out, Z_out] = peaks (x, y)
54
55   if (nargin == 0)
56     x = y = linspace (-3, 3, 49);
57   elseif (nargin == 1)
58     if length(x) > 1
59       y = x;
60     else
61       x = y = linspace (-3, 3, x);
62     endif
63   endif
64
65   if (isvector (x) && isvector (y))
66     [X, Y] = meshgrid (x, y);
67   else
68     X = x;
69     Y = y;
70   endif
71
72   Z = 3 * (1 - X) .^ 2 .* exp(- X .^ 2 - (Y + 1) .^ 2) \
73       - 10 * (X / 5 - X .^ 3 - Y .^ 5) .* exp(- X .^ 2 - Y .^ 2) \
74       - 1 / 3 * exp(- (X + 1) .^ 2 - Y .^ 2);
75
76   if (nargout == 0)
77     surf (x, y, Z);
78   elseif (nargout == 1)
79     X_out = Z;
80   else
81     X_out = X;
82     Y_out = Y;
83     Z_out = Z;
84   endif
85
86 endfunction