Mercurial > hg > octave-lyh
annotate scripts/plot/fplot.m @ 8874:bd1b1fe9c6e9 ss-3-1-53
bump version info for snapshot
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 25 Feb 2009 18:35:47 -0500 |
parents | 4629e3925e75 |
children | eb63fbe60fab |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2005, 2006, 2007 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 -*- | |
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}) |
6699 | 24 ## Plot a function @var{fn}, within the defined limits. @var{fn} |
5820 | 25 ## an be either a string, a function handle or an inline function. |
26 ## The limits of the plot are given by @var{limits} of the form | |
27 ## @code{[@var{xlo}, @var{xhi}]} or @code{[@var{xlo}, @var{xhi}, | |
6781 | 28 ## @var{ylo}, @var{yhi}]}. @var{tol} is the default tolerance to use for the |
29 ## plot, and if @var{tol} is an integer it is assumed that it defines the | |
6895 | 30 ## number points to use in the plot. The @var{fmt} argument is passed |
31 ## to the plot command. | |
5820 | 32 ## |
33 ## @example | |
6699 | 34 ## fplot ("cos", [0, 2*pi]) |
35 ## fplot ("[cos(x), sin(x)]", [0, 2*pi]) | |
5820 | 36 ## @end example |
6781 | 37 ## @seealso{plot} |
5820 | 38 ## @end deftypefn |
39 | |
7017 | 40 ## Author: Paul Kienzle <pkienzle@users.sf.net> |
41 | |
6781 | 42 function fplot (fn, limits, n, linespec) |
8847 | 43 if (nargin < 2 || nargin > 4) |
6046 | 44 print_usage (); |
5820 | 45 endif |
46 | |
47 if (nargin < 3) | |
6781 | 48 n = 0.002; |
5820 | 49 endif |
50 | |
6781 | 51 have_linespec = true; |
52 if (nargin < 4) | |
53 have_linespec = false; | |
54 endif | |
5820 | 55 |
6781 | 56 if (ischar (n)) |
57 have_linespec = true; | |
58 linespec = n; | |
59 n = 0.002; | |
60 endif | |
61 | |
6699 | 62 if (strcmp (typeinfo (fn), "inline function")) |
63 fn = vectorize (fn); | |
64 nam = formula (fn); | |
65 elseif (isa (fn, "function_handle")) | |
66 nam = func2str (fn); | |
5820 | 67 elseif (all (isalnum (fn))) |
6781 | 68 nam = fn; |
5820 | 69 else |
6781 | 70 fn = vectorize (inline (fn)); |
71 nam = formula (fn); | |
72 endif | |
73 | |
74 if (floor(n) != n) | |
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))(:)); | |
86 if (err == err0 || 0.5 * max (abs ((y00 - y) ./ (y00 + y))(:)) < tol) | |
87 break; | |
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); | |
95 endwhile | |
96 else | |
97 x = linspace (limits(1), limits(2), n)'; | |
98 y = feval (fn, x); | |
5820 | 99 endif |
100 | |
6781 | 101 if (have_linespec) |
102 plot (x, y, linespec); | |
103 else | |
104 plot (x, y); | |
105 endif | |
8145
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
106 |
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
107 if (length (limits) > 2) |
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
108 axis (limits); |
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
109 endif |
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
110 |
8507 | 111 if (isvector (y)) |
6781 | 112 legend (nam); |
113 else | |
8507 | 114 for i = 1:columns (y) |
6781 | 115 nams{i} = sprintf ("%s(:,%i)", nam, i); |
116 endfor | |
117 legend (nams{:}); | |
118 endif | |
5820 | 119 endfunction |
7245 | 120 |
121 %!demo | |
122 %! fplot ("cos", [0, 2*pi]) | |
123 | |
124 %!demo | |
125 %! fplot ("[cos(x), sin(x)]", [0, 2*pi]) |