Mercurial > hg > octave-nkf
annotate scripts/plot/peaks.m @ 9245:16f53d29049f
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 22 May 2009 10:46:00 -0400 |
parents | f0c3d3fc4903 |
children | be55736a0783 |
rev | line source |
---|---|
9245 | 1 ## Copyright (C) 2007, 2009 Paul Kienzle |
6788 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
6788 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
6788 | 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{}) | |
9040
dbd0c77e575e
Cleanup documentation file plot.texi
Rik <rdrider0-list@yahoo.com>
parents:
7282
diff
changeset
|
25 ## Generate a function with lots of local maxima and minima. The function |
6788 | 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 | |
9040
dbd0c77e575e
Cleanup documentation file plot.texi
Rik <rdrider0-list@yahoo.com>
parents:
7282
diff
changeset
|
40 ## above function using @code{mesh}. If @var{n} is a scalar, the @code{peaks} |
7001 | 41 ## returns the values of the above function on a @var{n}-by-@var{n} mesh over |
9040
dbd0c77e575e
Cleanup documentation file plot.texi
Rik <rdrider0-list@yahoo.com>
parents:
7282
diff
changeset
|
42 ## the range @code{[-3,3]}. The default value for @var{n} is 49. |
6788 | 43 ## |
44 ## If @var{n} is a vector, then it represents the @var{x} and @var{y} values | |
9040
dbd0c77e575e
Cleanup documentation file plot.texi
Rik <rdrider0-list@yahoo.com>
parents:
7282
diff
changeset
|
45 ## of the grid on which to calculate the above function. The @var{x} and |
6788 | 46 ## @var{y} values can be specified separately. |
7282 | 47 ## @seealso{surf, mesh, meshgrid} |
6788 | 48 ## @end deftypefn |
49 | |
6895 | 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 | |
6788 | 53 function [X_out, Y_out, Z_out] = peaks (x, y) |
54 | |
7216 | 55 if (nargin == 0) |
56 x = y = linspace (-3, 3, 49); | |
57 elseif (nargin == 1) | |
6788 | 58 if length(x) > 1 |
59 y = x; | |
60 else | |
7216 | 61 x = y = linspace (-3, 3, x); |
6788 | 62 endif |
63 endif | |
64 | |
7216 | 65 if (isvector (x) && isvector (y)) |
6788 | 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 | |
7216 | 76 if (nargout == 0) |
7282 | 77 surf (x, y, Z); |
7216 | 78 elseif (nargout == 1) |
6788 | 79 X_out = Z; |
80 else | |
81 X_out = X; | |
82 Y_out = Y; | |
83 Z_out = Z; | |
84 endif | |
85 | |
86 endfunction |