6788
|
1 ## Copyright (C) 2007 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 2, or (at your option) |
|
8 ## 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, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} peaks () |
|
22 ## @deftypefnx {Function File} {} peaks (@var{n}) |
|
23 ## @deftypefnx {Function File} {} peaks (@var{x}, @var{y}) |
|
24 ## @deftypefnx {Function File} {@var{z} =} peaks (@dots{}) |
|
25 ## @deftypefnx {Function File} {[@var{x}, @var{y}, @var{z}] =} peaks (@dots{}) |
|
26 ## Generate a function with lots of local maxima and minima. The function |
|
27 ## has the form |
|
28 ## |
|
29 ## @iftex |
|
30 ## @tex |
|
31 ## $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)}$ |
|
32 ## @end tex |
|
33 ## @end iftex |
|
34 ## @ifnottex |
|
35 ## @verbatim |
|
36 ## f(x,y) = 3*(1-x)^2*exp(-x^2 - (y+1)^2) ... |
|
37 ## - 10*(x/5 - x^3 - y^5)*exp(-x^2-y^2) ... |
|
38 ## - 1/3*exp(-(x+1)^2 - y^2) |
|
39 ## @end verbatim |
|
40 ## @end ifnottex |
|
41 ## |
|
42 ## Called without a return argument, @code{peaks} plots the surface of the |
|
43 ## above function using @code{mesh}. If @var{n} is a scalar, the @code{peaks} |
|
44 ## returns the values of the above funxtion on a @var{n}-by-@var{n} mesh over |
|
45 ## the range @code{[-3,3]}. The default value for @var{n} is 49. |
|
46 ## |
|
47 ## If @var{n} is a vector, then it represents the @var{x} and @var{y} values |
|
48 ## of the grid on which to calculate the above function. The @var{x} and |
|
49 ## @var{y} values can be specified separately. |
|
50 ## @seealso{mesh} |
|
51 ## @end deftypefn |
|
52 |
|
53 ## Expression for peaks function was taken from the following paper: |
|
54 ## http://www.control.hut.fi/Kurssit/AS-74.115/Material/GENALGgoga.pdf |
|
55 function [X_out, Y_out, Z_out] = peaks (x, y) |
|
56 |
|
57 if nargin == 0 |
|
58 x = y = linspace(-3,3,49); |
|
59 elseif nargin == 1 |
|
60 if length(x) > 1 |
|
61 y = x; |
|
62 else |
|
63 x = y = linspace(-3,3,x); |
|
64 endif |
|
65 endif |
|
66 |
|
67 if (isvector(x) && isvector(y)) |
|
68 [X, Y] = meshgrid (x, y); |
|
69 else |
|
70 X = x; |
|
71 Y = y; |
|
72 endif |
|
73 |
|
74 Z = 3 * (1 - X) .^ 2 .* exp(- X .^ 2 - (Y + 1) .^ 2) \ |
|
75 - 10 * (X / 5 - X .^ 3 - Y .^ 5) .* exp(- X .^ 2 - Y .^ 2) \ |
|
76 - 1 / 3 * exp(- (X + 1) .^ 2 - Y .^ 2); |
|
77 |
|
78 if nargout == 0 |
|
79 mesh (x, y, Z); |
|
80 elseif nargout == 1 |
|
81 X_out = Z; |
|
82 else |
|
83 X_out = X; |
|
84 Y_out = Y; |
|
85 Z_out = Z; |
|
86 endif |
|
87 |
|
88 endfunction |