Mercurial > hg > octave-nkf
annotate scripts/plot/fplot.m @ 17190:df4c4b7708a4
Add titles and clean-up plotting %!demos.
* scripts/plot/area.m, scripts/plot/axis.m, scripts/plot/bar.m,
scripts/plot/barh.m, scripts/plot/clabel.m, scripts/plot/colorbar.m,
scripts/plot/comet.m, scripts/plot/comet3.m, scripts/plot/contour.m,
scripts/plot/contour3.m, scripts/plot/contourf.m, scripts/plot/cylinder.m,
scripts/plot/ellipsoid.m, scripts/plot/errorbar.m, scripts/plot/ezplot.m,
scripts/plot/ezplot3.m, scripts/plot/ezpolar.m, scripts/plot/feather.m,
scripts/plot/fplot.m, scripts/plot/hold.m, scripts/plot/isosurface.m,
scripts/plot/legend.m, scripts/plot/loglog.m, scripts/plot/loglogerr.m,
scripts/plot/mesh.m, scripts/plot/meshc.m, scripts/plot/meshz.m,
scripts/plot/patch.m, scripts/plot/pcolor.m, scripts/plot/pie.m,
scripts/plot/pie3.m, scripts/plot/plot.m, scripts/plot/plot3.m,
scripts/plot/polar.m, scripts/plot/rectangle.m, scripts/plot/ribbon.m,
scripts/plot/rose.m, scripts/plot/scatter.m, scripts/plot/scatter3.m,
scripts/plot/semilogx.m, scripts/plot/semilogxerr.m, scripts/plot/semilogy.m,
scripts/plot/semilogyerr.m, scripts/plot/sombrero.m, scripts/plot/stem3.m,
scripts/plot/surf.m, scripts/plot/surfc.m, scripts/plot/surfl.m,
scripts/plot/title.m, scripts/plot/waterfall.m: Add titles and clean-up
plotting %!demos.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 06 Aug 2013 14:34:20 -0700 |
parents | d74e2b5bdeb5 |
children | bdb237c7507c |
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}) |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
23 ## @deftypefnx {Function File} {} fplot (@var{fn}, @var{limits}, @var{fmt}) |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
24 ## @deftypefnx {Function File} {} fplot (@var{fn}, @var{limits}, @var{tol}, @var{n}, @var{fmt}) |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
25 ## @deftypefnx {Function File} {[@var{x}, @var{y}] =} fplot (@dots{}) |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
26 ## Plot a function @var{fn} within the range defined by @var{limits}. |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
27 ## |
12632
2dbac27e0e40
doc: miscellaneous touch-ups to documentation strings
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
28 ## @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
|
29 ## containing the name of the function to evaluate. |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
30 ## The limits of the plot are of the form @code{[@var{xlo}, @var{xhi}]} or |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
31 ## @code{[@var{xlo}, @var{xhi}, @var{ylo}, @var{yhi}]}. |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
32 ## The next three arguments are all optional and any number of them may be |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
33 ## given in any order. |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
34 ## @var{tol} is the relative tolerance to use for the plot and defaults |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
35 ## to 2e-3 (.2%). |
17122
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17026
diff
changeset
|
36 ## @var{n} is the minimum number of points to use. When @var{n} is |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
37 ## specified, the maximum stepsize will be |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
38 ## @code{@var{xhi} - @var{xlo} / @var{n}}. More than @var{n} points may still |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
39 ## be used in order to meet the relative tolerance requirement. |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
40 ## The @var{fmt} argument specifies the linestyle to be used by the plot |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
41 ## command. |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
42 ## |
17159
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
43 ## If the first argument @var{hax} is an axes handle, then plot into this axis, |
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
44 ## rather than the current axes returned by @code{gca}. |
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
45 ## |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
46 ## With no output arguments the results are immediately plotted. With two |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
47 ## output arguments the 2-D plot data is returned. The data can subsequently |
16951
a7c9be4a2c0f
fplot.m: Fix bad texinfo in docstring for changeset b34202b24212.
Rik <rik@octave.org>
parents:
16950
diff
changeset
|
48 ## be plotted manually with @code{plot (@var{x}, @var{y})}. |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
49 ## |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
50 ## Example: |
5820 | 51 ## |
52 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9040
diff
changeset
|
53 ## @group |
16951
a7c9be4a2c0f
fplot.m: Fix bad texinfo in docstring for changeset b34202b24212.
Rik <rik@octave.org>
parents:
16950
diff
changeset
|
54 ## fplot (@@cos, [0, 2*pi]) |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
55 ## 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
|
56 ## @end group |
5820 | 57 ## @end example |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
58 ## |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
59 ## Note: @code{fplot} works best with continuous functions. Functions with |
17122
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17026
diff
changeset
|
60 ## discontinuities are unlikely to plot well. This restriction may be removed |
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17026
diff
changeset
|
61 ## in the future. |
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17026
diff
changeset
|
62 ## @seealso{ezplot, plot} |
5820 | 63 ## @end deftypefn |
64 | |
7017 | 65 ## Author: Paul Kienzle <pkienzle@users.sf.net> |
66 | |
17159
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
67 function [X, Y] = fplot (varargin) |
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
68 |
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
69 [hax, varargin, nargin] = __plt_get_axis_arg__ ("fplot", varargin{:}); |
5820 | 70 |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
71 if (nargin < 2 || nargin > 5) |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
72 print_usage (); |
6781 | 73 endif |
74 | |
17159
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
75 fn = varargin{1}; |
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
76 limits = varargin{2}; |
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
77 varargin = varargin(3:end); |
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
78 |
6699 | 79 if (strcmp (typeinfo (fn), "inline function")) |
80 fn = vectorize (fn); | |
81 nam = formula (fn); | |
82 elseif (isa (fn, "function_handle")) | |
83 nam = func2str (fn); | |
5820 | 84 elseif (all (isalnum (fn))) |
6781 | 85 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
|
86 elseif (ischar (fn)) |
16338
8aeb5d5c3747
fplot.m: Overhaul code to use modern coding practices.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
87 fn = vectorize (inline (fn)); |
8aeb5d5c3747
fplot.m: Overhaul code to use modern coding practices.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
88 nam = formula (fn); |
5820 | 89 else |
16338
8aeb5d5c3747
fplot.m: Overhaul code to use modern coding practices.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
90 error ("fplot: FN must be a function handle, inline function, or string"); |
6781 | 91 endif |
92 | |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
93 if (iscomplex (limits) || (numel (limits) != 2 && numel (limits) != 4)) |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
94 error ("fplot: LIMITS must be a real vector with 2 or 4 elements"); |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
95 endif |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
96 |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
97 n = 5; |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
98 tol = 2e-3; |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
99 fmt = ""; |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
100 for i = 1:numel (varargin) |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
101 arg = varargin{i}; |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
102 if (ischar (arg)) |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
103 fmt = arg; |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
104 elseif (isnumeric (arg) && isscalar (arg) && arg > 0) |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
105 if (arg == fix (arg)) |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
106 n = arg; |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
107 else |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
108 tol = arg; |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
109 endif |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
110 else |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
111 error ("fplot: bad input in position %d", i+2); |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
112 endif |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
113 endfor |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
114 |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
115 if (n != 5) |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
116 ## n was specified |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
117 x0 = linspace (limits(1), limits(2), n/2 + 1)'; |
6781 | 118 y0 = feval (fn, x0); |
119 x = linspace (limits(1), limits(2), n)'; | |
120 y = feval (fn, x); | |
121 else | |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
122 x0 = linspace (limits(1), limits(2), 5)'; |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
123 y0 = feval (fn, x0); |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
124 n = 8; |
6781 | 125 x = linspace (limits(1), limits(2), n)'; |
126 y = feval (fn, x); | |
5820 | 127 endif |
128 | |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
129 if (rows (x0) != rows (y0)) |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
130 ## FN is a constant value function |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
131 y0 = repmat (y0, size (x0)); |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
132 y = repmat (y, size (x)); |
8145
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
133 endif |
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
134 |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
135 err0 = Inf; |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
136 |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
137 ## FIXME: This algorithm should really use adaptive scaling as the |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
138 ## the numerical quadrature algorithms do so that extra points are |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
139 ## used where they are needed and not spread evenly over the entire |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
140 ## x-range. Try any function with a discontinuity such as |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
141 ## fplot (@tan, [-2, 2]) or fplot ("1./x", [-3, 2]) to see the |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
142 ## problems with the current solution. |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
143 |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
144 while (n < 2^18) # Something is wrong if we need more than 250K points |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
145 yi = interp1 (x0, y0, x, "linear"); |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
146 ## relative error calculation using average of [yi,y] as reference |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
147 ## since neither estimate is known a priori to be better than the other. |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
148 err = 0.5 * max (abs ((yi - y) ./ (yi + y))(:)); |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
149 if (err < tol || abs (err - err0) < tol/2) |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
150 ## Either relative tolerance has been met OR |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
151 ## algorithm has stopped making any reasonable progress per iteration. |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
152 break; |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
153 endif |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
154 x0 = x; |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
155 y0 = y; |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
156 err0 = err; |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
157 n = 2 * (n - 1) + 1; |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
158 x = linspace (limits(1), limits(2), n)'; |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
159 y = feval (fn, x); |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
160 endwhile |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
161 |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
162 if (nargout == 2) |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
163 X = x; |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
164 Y = y; |
6781 | 165 else |
17159
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
166 if (isempty (hax)) |
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
167 hax = gca (); |
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
168 endif |
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
169 plot (hax, x, y, fmt); |
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
170 axis (hax, limits); |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
171 if (isvector (y)) |
17159
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
172 legend (hax, nam); |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
173 else |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
174 for i = 1:columns (y) |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
175 nams{i} = sprintf ("%s(:,%i)", nam, i); |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
176 endfor |
17159
d74e2b5bdeb5
fplot.m: Accept an axis handle as first input.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
177 legend (hax, nams{:}); |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
178 endif |
6781 | 179 endif |
16338
8aeb5d5c3747
fplot.m: Overhaul code to use modern coding practices.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
180 |
5820 | 181 endfunction |
7245 | 182 |
183 | |
184 %!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
|
185 %! clf; |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
186 %! fplot (@cos, [0, 2*pi]); |
17190
df4c4b7708a4
Add titles and clean-up plotting %!demos.
Rik <rik@octave.org>
parents:
17159
diff
changeset
|
187 %! title ('fplot() single function'); |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
188 |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
189 %!demo |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
190 %! clf; |
14245
4506eade9f04
Use Matlab coding conventions for demos in plot/ directory.
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
191 %! fplot ('[cos(x), sin(x)]', [0, 2*pi]); |
17190
df4c4b7708a4
Add titles and clean-up plotting %!demos.
Rik <rik@octave.org>
parents:
17159
diff
changeset
|
192 %! title ('fplot() multiple functions'); |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
193 |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
194 %!demo |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
195 %! clf; |
17190
df4c4b7708a4
Add titles and clean-up plotting %!demos.
Rik <rik@octave.org>
parents:
17159
diff
changeset
|
196 %! %% sinc function |
17026
be52288f827b
Tweak some plotting demos for messages and spelling.
Rik <rik@octave.org>
parents:
16951
diff
changeset
|
197 %! fh = @(x) sin (pi*x) ./ (pi*x); |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
198 %! fplot (fh, [-5, 5]); |
17190
df4c4b7708a4
Add titles and clean-up plotting %!demos.
Rik <rik@octave.org>
parents:
17159
diff
changeset
|
199 %! title ('fplot() sinc function'); |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
200 |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
201 %!test |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
202 %! [x, y] = fplot ("[cos(x), sin(x)]", [0, 2*pi]); |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
203 %! assert (columns (y) == 2); |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
204 %! assert (rows (x) == rows (y)); |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
205 %! assert (y, [cos(x), sin(x)], -2e-3); |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
206 |
16775
5ec3f4aea91c
fplot.m: Fix bug when FN is a constant function (bug #39287)
Rik <rik@octave.org>
parents:
16338
diff
changeset
|
207 %% Test input validation |
5ec3f4aea91c
fplot.m: Fix bug when FN is a constant function (bug #39287)
Rik <rik@octave.org>
parents:
16338
diff
changeset
|
208 %!error fplot (1) |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
209 %!error fplot (1,2,3,4,5,6) |
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
210 %!error <FN must be a function handle> fplot (1, [0 1]) |
16775
5ec3f4aea91c
fplot.m: Fix bug when FN is a constant function (bug #39287)
Rik <rik@octave.org>
parents:
16338
diff
changeset
|
211 %!error <LIMITS must be a real vector> fplot (@cos, [i, 2*i]) |
5ec3f4aea91c
fplot.m: Fix bug when FN is a constant function (bug #39287)
Rik <rik@octave.org>
parents:
16338
diff
changeset
|
212 %!error <LIMITS must be a real vector with 2 or 4> fplot (@cos, [1]) |
5ec3f4aea91c
fplot.m: Fix bug when FN is a constant function (bug #39287)
Rik <rik@octave.org>
parents:
16338
diff
changeset
|
213 %!error <LIMITS must be a real vector with 2 or 4> fplot (@cos, [1 2 3]) |
16950
b34202b24212
fplot.m: Overhaul function for Matlab compatibility and performance (bug #38961).
Rik <rik@octave.org>
parents:
16775
diff
changeset
|
214 %!error <bad input in position 3> fplot (@cos,[-1,1], {1}) |
16775
5ec3f4aea91c
fplot.m: Fix bug when FN is a constant function (bug #39287)
Rik <rik@octave.org>
parents:
16338
diff
changeset
|
215 |