Mercurial > hg > octave-nkf
annotate scripts/plot/peaks.m @ 9056:b06dc393ac42
print.m: For eps output the bounding box should represent the figure's position.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Sun, 29 Mar 2009 00:32:01 -0400 |
parents | dbd0c77e575e |
children | f0c3d3fc4903 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2007 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 ## @iftex | |
29 ## @tex | |
30 ## $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)}$ | |
31 ## @end tex | |
32 ## @end iftex | |
33 ## @ifnottex | |
34 ## @verbatim | |
35 ## f(x,y) = 3*(1-x)^2*exp(-x^2 - (y+1)^2) ... | |
36 ## - 10*(x/5 - x^3 - y^5)*exp(-x^2-y^2) ... | |
37 ## - 1/3*exp(-(x+1)^2 - y^2) | |
38 ## @end verbatim | |
39 ## @end ifnottex | |
40 ## | |
41 ## 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
|
42 ## above function using @code{mesh}. If @var{n} is a scalar, the @code{peaks} |
7001 | 43 ## 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
|
44 ## the range @code{[-3,3]}. The default value for @var{n} is 49. |
6788 | 45 ## |
46 ## 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
|
47 ## of the grid on which to calculate the above function. The @var{x} and |
6788 | 48 ## @var{y} values can be specified separately. |
7282 | 49 ## @seealso{surf, mesh, meshgrid} |
6788 | 50 ## @end deftypefn |
51 | |
6895 | 52 ## Expression for the peaks function was taken from the following paper: |
53 ## http://www.control.hut.fi/Kurssit/AS-74.115/Material/GENALGgoga.pdf | |
54 | |
6788 | 55 function [X_out, Y_out, Z_out] = peaks (x, y) |
56 | |
7216 | 57 if (nargin == 0) |
58 x = y = linspace (-3, 3, 49); | |
59 elseif (nargin == 1) | |
6788 | 60 if length(x) > 1 |
61 y = x; | |
62 else | |
7216 | 63 x = y = linspace (-3, 3, x); |
6788 | 64 endif |
65 endif | |
66 | |
7216 | 67 if (isvector (x) && isvector (y)) |
6788 | 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 | |
7216 | 78 if (nargout == 0) |
7282 | 79 surf (x, y, Z); |
7216 | 80 elseif (nargout == 1) |
6788 | 81 X_out = Z; |
82 else | |
83 X_out = X; | |
84 Y_out = Y; | |
85 Z_out = Z; | |
86 endif | |
87 | |
88 endfunction |