Mercurial > hg > octave-nkf
annotate scripts/plot/fplot.m @ 16651:4e50bd2946d8 ss-3-7-4
snapshot 3.7.4
* configure.ac (OCTAVE_VERSION): Bump to 3.7.4.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 13 May 2013 08:07:15 -0400 |
parents | 8aeb5d5c3747 |
children | 5ec3f4aea91c |
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 | |
16338
8aeb5d5c3747
fplot.m: Overhaul code to use modern coding practices.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
45 function fplot (fn, limits, n = 0.002, fmt = "") |
8aeb5d5c3747
fplot.m: Overhaul code to use modern coding practices.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
46 |
8847 | 47 if (nargin < 2 || nargin > 4) |
6046 | 48 print_usage (); |
5820 | 49 endif |
50 | |
16338
8aeb5d5c3747
fplot.m: Overhaul code to use modern coding practices.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
51 if (iscomplex (limits) || (numel (limits) != 2 && numel (limits) != 4)) |
8aeb5d5c3747
fplot.m: Overhaul code to use modern coding practices.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
52 error ("fplot: LIMITS must be a real vector with 2 or 4 elements"); |
6781 | 53 endif |
5820 | 54 |
6781 | 55 if (ischar (n)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
56 fmt = n; |
6781 | 57 n = 0.002; |
58 endif | |
59 | |
6699 | 60 if (strcmp (typeinfo (fn), "inline function")) |
61 fn = vectorize (fn); | |
62 nam = formula (fn); | |
63 elseif (isa (fn, "function_handle")) | |
64 nam = func2str (fn); | |
5820 | 65 elseif (all (isalnum (fn))) |
6781 | 66 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
|
67 elseif (ischar (fn)) |
16338
8aeb5d5c3747
fplot.m: Overhaul code to use modern coding practices.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
68 fn = vectorize (inline (fn)); |
8aeb5d5c3747
fplot.m: Overhaul code to use modern coding practices.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
69 nam = formula (fn); |
5820 | 70 else |
16338
8aeb5d5c3747
fplot.m: Overhaul code to use modern coding practices.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
71 error ("fplot: FN must be a function handle, inline function, or string"); |
6781 | 72 endif |
73 | |
16338
8aeb5d5c3747
fplot.m: Overhaul code to use modern coding practices.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
74 if (n != fix (n)) |
6781 | 75 tol = n; |
7280 | 76 x0 = linspace (limits(1), limits(2), 5)'; |
6781 | 77 y0 = feval (fn, x0); |
78 err0 = Inf; | |
7280 | 79 n = 8; |
6781 | 80 x = linspace (limits(1), limits(2), n)'; |
81 y = feval (fn, x); | |
82 | |
83 while (n < 2 .^ 20) | |
84 y00 = interp1 (x0, y0, x, "linear"); | |
85 err = 0.5 * max (abs ((y00 - y) ./ (y00 + y))(:)); | |
16338
8aeb5d5c3747
fplot.m: Overhaul code to use modern coding practices.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
86 if (err == err0 || err < tol) |
10549 | 87 break; |
6781 | 88 endif |
89 x0 = x; | |
90 y0 = y; | |
91 err0 = err; | |
92 n = 2 * (n - 1) + 1; | |
93 x = linspace (limits(1), limits(2), n)'; | |
94 y = feval (fn, x); | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
95 endwhile |
6781 | 96 else |
97 x = linspace (limits(1), limits(2), n)'; | |
98 y = feval (fn, x); | |
5820 | 99 endif |
100 | |
16338
8aeb5d5c3747
fplot.m: Overhaul code to use modern coding practices.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
101 plot (x, y, fmt); |
8145
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
102 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
103 if (length (limits) > 2) |
8145
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
104 axis (limits); |
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
105 endif |
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
106 |
8507 | 107 if (isvector (y)) |
6781 | 108 legend (nam); |
109 else | |
8507 | 110 for i = 1:columns (y) |
6781 | 111 nams{i} = sprintf ("%s(:,%i)", nam, i); |
112 endfor | |
113 legend (nams{:}); | |
114 endif | |
16338
8aeb5d5c3747
fplot.m: Overhaul code to use modern coding practices.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
115 |
5820 | 116 endfunction |
7245 | 117 |
118 | |
119 %!demo | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
120 %! clf; |
14245
4506eade9f04
Use Matlab coding conventions for demos in plot/ directory.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
121 %! fplot ('cos', [0, 2*pi]); |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
122 |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
123 %!demo |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
124 %! clf; |
14245
4506eade9f04
Use Matlab coding conventions for demos in plot/ directory.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
125 %! fplot ('[cos(x), sin(x)]', [0, 2*pi]); |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
126 |