1 ## Copyright (C) 2007-2012 Paul Kienzle
3 ## This file is part of Octave.
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.
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.
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/>.
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
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)}$
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)
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.
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}
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
53 function [X_out, Y_out, Z_out] = peaks (x, y)
56 x = y = linspace (-3, 3, 49);
61 x = y = linspace (-3, 3, x);
65 if (isvector (x) && isvector (y))
66 [X, Y] = meshgrid (x, y);
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);