Mercurial > hg > octave-lyh
annotate scripts/plot/fplot.m @ 12632:2dbac27e0e40 stable
doc: miscellaneous touch-ups to documentation strings
* debug.txi: Correct line number for debug example.
* func.txi: Correct directory structure of functions shipped with Octave distribution.
* edit.m: Eliminate reference to editor used by bug_report.
* fzero.m: Add inline function to list of possible inputs.
* fplot.m: Add comma to break list of possible inputs.
* qqplot.m: Correct example so that it runs under current Octave distributions.
* assert.m: Add seealso reference to fail.
* fail.m: Add seealso reference to assert. Add additional calling forms of function.
* load-path.cc (addpath): Don't use @samp which created double double quotes around option.
* ov-fcn-inline.cc (vectorize): Add example usage.
* variables.cc (exist): Add seealso link to file_in_loadpath.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 25 Apr 2011 09:24:33 -0700 |
parents | c792872f8942 |
children | 22c50cbad2ce |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2005-2011 Paul Kienzle |
5820 | 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. | |
5820 | 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/>. | |
5820 | 18 |
19 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {} fplot (@var{fn}, @var{limits}) |
6781 | 21 ## @deftypefnx {Function File} {} fplot (@var{fn}, @var{limits}, @var{tol}) |
5820 | 22 ## @deftypefnx {Function File} {} fplot (@var{fn}, @var{limits}, @var{n}) |
6895 | 23 ## @deftypefnx {Function File} {} fplot (@dots{}, @var{fmt}) |
12632
2dbac27e0e40
doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
24 ## Plot a function @var{fn} within defined limits. |
2dbac27e0e40
doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
25 ## @var{fn} is a function handle, inline function, or string |
2dbac27e0e40
doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
26 ## containing the name of the function to evaluate. |
5820 | 27 ## The limits of the plot are given by @var{limits} of the form |
28 ## @code{[@var{xlo}, @var{xhi}]} or @code{[@var{xlo}, @var{xhi}, | |
9040
dbd0c77e575e
Cleanup documentation file plot.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
29 ## @var{ylo}, @var{yhi}]}. @var{tol} is the default tolerance to use for the |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
30 ## plot, and if @var{tol} is an integer it is assumed that it defines the |
6895 | 31 ## number points to use in the plot. The @var{fmt} argument is passed |
32 ## to the plot command. | |
5820 | 33 ## |
34 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9040
diff
changeset
|
35 ## @group |
6699 | 36 ## fplot ("cos", [0, 2*pi]) |
37 ## fplot ("[cos(x), sin(x)]", [0, 2*pi]) | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9040
diff
changeset
|
38 ## @end group |
5820 | 39 ## @end example |
6781 | 40 ## @seealso{plot} |
5820 | 41 ## @end deftypefn |
42 | |
7017 | 43 ## Author: Paul Kienzle <pkienzle@users.sf.net> |
44 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
45 function fplot (fn, limits, n, fmt) |
8847 | 46 if (nargin < 2 || nargin > 4) |
6046 | 47 print_usage (); |
5820 | 48 endif |
49 | |
10400
b14fd5116c29
Ensure that 'limits' is a 2 or 4 vector, and that 'fn' is a function
Soren Hauberg <hauberg@gmail.com>
parents:
9051
diff
changeset
|
50 if (!isreal (limits) || (numel (limits) != 2 && numel (limits) != 4)) |
b14fd5116c29
Ensure that 'limits' is a 2 or 4 vector, and that 'fn' is a function
Soren Hauberg <hauberg@gmail.com>
parents:
9051
diff
changeset
|
51 error ("fplot: second input argument must be a real vector with 2 or 4 elements"); |
b14fd5116c29
Ensure that 'limits' is a 2 or 4 vector, and that 'fn' is a function
Soren Hauberg <hauberg@gmail.com>
parents:
9051
diff
changeset
|
52 endif |
b14fd5116c29
Ensure that 'limits' is a 2 or 4 vector, and that 'fn' is a function
Soren Hauberg <hauberg@gmail.com>
parents:
9051
diff
changeset
|
53 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
54 if (nargin < 3) |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
55 n = 0.002; |
5820 | 56 endif |
57 | |
6781 | 58 have_linespec = true; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
59 if (nargin < 4) |
6781 | 60 have_linespec = false; |
61 endif | |
5820 | 62 |
6781 | 63 if (ischar (n)) |
64 have_linespec = true; | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
65 fmt = n; |
6781 | 66 n = 0.002; |
67 endif | |
68 | |
6699 | 69 if (strcmp (typeinfo (fn), "inline function")) |
70 fn = vectorize (fn); | |
71 nam = formula (fn); | |
72 elseif (isa (fn, "function_handle")) | |
73 nam = func2str (fn); | |
5820 | 74 elseif (all (isalnum (fn))) |
6781 | 75 nam = fn; |
10400
b14fd5116c29
Ensure that 'limits' is a 2 or 4 vector, and that 'fn' is a function
Soren Hauberg <hauberg@gmail.com>
parents:
9051
diff
changeset
|
76 elseif (ischar (fn)) |
b14fd5116c29
Ensure that 'limits' is a 2 or 4 vector, and that 'fn' is a function
Soren Hauberg <hauberg@gmail.com>
parents:
9051
diff
changeset
|
77 fn = vectorize (inline (fn)); |
b14fd5116c29
Ensure that 'limits' is a 2 or 4 vector, and that 'fn' is a function
Soren Hauberg <hauberg@gmail.com>
parents:
9051
diff
changeset
|
78 nam = formula (fn); |
5820 | 79 else |
10400
b14fd5116c29
Ensure that 'limits' is a 2 or 4 vector, and that 'fn' is a function
Soren Hauberg <hauberg@gmail.com>
parents:
9051
diff
changeset
|
80 error ("fplot: first input argument must be a function handle, inline function or string"); |
6781 | 81 endif |
82 | |
83 if (floor(n) != n) | |
84 tol = n; | |
7280 | 85 x0 = linspace (limits(1), limits(2), 5)'; |
6781 | 86 y0 = feval (fn, x0); |
87 err0 = Inf; | |
7280 | 88 n = 8; |
6781 | 89 x = linspace (limits(1), limits(2), n)'; |
90 y = feval (fn, x); | |
91 | |
92 while (n < 2 .^ 20) | |
93 y00 = interp1 (x0, y0, x, "linear"); | |
94 err = 0.5 * max (abs ((y00 - y) ./ (y00 + y))(:)); | |
95 if (err == err0 || 0.5 * max (abs ((y00 - y) ./ (y00 + y))(:)) < tol) | |
10549 | 96 break; |
6781 | 97 endif |
98 x0 = x; | |
99 y0 = y; | |
100 err0 = err; | |
101 n = 2 * (n - 1) + 1; | |
102 x = linspace (limits(1), limits(2), n)'; | |
103 y = feval (fn, x); | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
104 endwhile |
6781 | 105 else |
106 x = linspace (limits(1), limits(2), n)'; | |
107 y = feval (fn, x); | |
5820 | 108 endif |
109 | |
6781 | 110 if (have_linespec) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
111 plot (x, y, fmt); |
6781 | 112 else |
113 plot (x, y); | |
114 endif | |
8145
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
115 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
116 if (length (limits) > 2) |
8145
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
117 axis (limits); |
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
118 endif |
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
119 |
8507 | 120 if (isvector (y)) |
6781 | 121 legend (nam); |
122 else | |
8507 | 123 for i = 1:columns (y) |
6781 | 124 nams{i} = sprintf ("%s(:,%i)", nam, i); |
125 endfor | |
126 legend (nams{:}); | |
127 endif | |
5820 | 128 endfunction |
7245 | 129 |
130 %!demo | |
131 %! fplot ("cos", [0, 2*pi]) | |
132 | |
133 %!demo | |
134 %! fplot ("[cos(x), sin(x)]", [0, 2*pi]) |