Mercurial > hg > octave-lyh
annotate scripts/plot/fplot.m @ 14327:4d917a6a858b stable
doc: Use Octave coding conventions in @example blocks of docstrings.
* accumarray.m, accumdim.m, bar.m, base2dec.m, bincoeff.m, bitcmp.m, bitset.m,
celldisp.m, chop.m, clabel.m, cloglog.m, colon.m, compass.m, computer.m,
contour3.m, contourc.m, corr.m, cstrcat.m, ctime.m, cylinder.m, date.m,
dec2base.m, demo.m, dir.m, dlmwrite.m, expm.m, ezcontourf.m, ezcontour.m,
ezmeshc.m, ezmesh.m, ezplot.m, ezsurfc.m, ezsurf.m, feather.m, findobj.m,
flipdim.m, fplot.m, genvarname.m, getfield.m, hankel.m, hilb.m, hist.m,
idivide.m, index.m, int2str.m, interp1.m, is_leap_year.m, ismember.m,
isocolors.m, isonormals.m, isosurface.m, kurtosis.m, legendre.m, linkprop.m,
logit.m, logm.m, __makeinfo__.m, __marching_cube__.m, median.m, mkoctfile.m,
moment.m, mpoles.m, orderfields.m, pcg.m, pcr.m, plot3.m, plotmatrix.m,
polyaffine.m, polygcd.m, poly.m, polyout.m, print.m, qp.m, quadgk.m, qzhess.m,
randi.m, rat.m, refreshdata.m, residue.m, rose.m, rot90.m, saveas.m, saveobj.m,
shiftdim.m, skewness.m, spaugment.m, spdiags.m, sqp.m, stem.m, str2num.m,
strcat.m, strjust.m, strread.m, strsplit.m, structfun.m, subplot.m,
subsindex.m, substruct.m, surfl.m, surfnorm.m, svds.m, uimenu.m, union.m,
voronoi.m, warning_ids.m, wblpdf.m: Use Octave coding conventions in
@example blocks of docstrings.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sat, 04 Feb 2012 22:12:50 -0800 |
parents | 72c96de7a403 |
children | ce2b59a6d0e5 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14092
diff
changeset
|
1 ## Copyright (C) 2005-2012 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 |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
36 ## fplot ("cos", [0, 2*pi]) |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
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 | |
14092
22c50cbad2ce
Add clf() to all plot demos.
Ben Abbott <bpabbott@mac.com>
parents:
12632
diff
changeset
|
131 %! clf |
7245 | 132 %! fplot ("cos", [0, 2*pi]) |
133 | |
134 %!demo | |
14092
22c50cbad2ce
Add clf() to all plot demos.
Ben Abbott <bpabbott@mac.com>
parents:
12632
diff
changeset
|
135 %! clf |
7245 | 136 %! fplot ("[cos(x), sin(x)]", [0, 2*pi]) |