5820
|
1 ## Copyright (C) 2005 Paul Kienzle <pkienzle@users.sf.net> |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} fplot (@var{fn}, @var{limits}) |
6781
|
22 ## @deftypefnx {Function File} {} fplot (@var{fn}, @var{limits}, @var{tol}) |
5820
|
23 ## @deftypefnx {Function File} {} fplot (@var{fn}, @var{limits}, @var{n}) |
6895
|
24 ## @deftypefnx {Function File} {} fplot (@dots{}, @var{fmt}) |
6699
|
25 ## Plot a function @var{fn}, within the defined limits. @var{fn} |
5820
|
26 ## an be either a string, a function handle or an inline function. |
|
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}, |
6781
|
29 ## @var{ylo}, @var{yhi}]}. @var{tol} is the default tolerance to use for the |
|
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 |
6699
|
35 ## fplot ("cos", [0, 2*pi]) |
|
36 ## fplot ("[cos(x), sin(x)]", [0, 2*pi]) |
5820
|
37 ## @end example |
6781
|
38 ## @seealso{plot} |
5820
|
39 ## @end deftypefn |
|
40 |
6781
|
41 function fplot (fn, limits, n, linespec) |
5820
|
42 if (nargin < 2 || nargin > 3) |
6046
|
43 print_usage (); |
5820
|
44 endif |
|
45 |
|
46 if (nargin < 3) |
6781
|
47 n = 0.002; |
5820
|
48 endif |
|
49 |
6781
|
50 have_linespec = true; |
|
51 if (nargin < 4) |
|
52 have_linespec = false; |
|
53 endif |
5820
|
54 |
6781
|
55 if (ischar (n)) |
|
56 have_linespec = true; |
|
57 linespec = n; |
|
58 n = 0.002; |
|
59 endif |
|
60 |
6699
|
61 if (strcmp (typeinfo (fn), "inline function")) |
|
62 fn = vectorize (fn); |
|
63 nam = formula (fn); |
|
64 elseif (isa (fn, "function_handle")) |
|
65 nam = func2str (fn); |
5820
|
66 elseif (all (isalnum (fn))) |
6781
|
67 nam = fn; |
5820
|
68 else |
6781
|
69 fn = vectorize (inline (fn)); |
|
70 nam = formula (fn); |
|
71 endif |
|
72 |
|
73 if (floor(n) != n) |
|
74 tol = n; |
|
75 x0 = linspace (limits(1), limits(2), 3)'; |
|
76 y0 = feval (fn, x0); |
|
77 err0 = Inf; |
|
78 n = 5; |
|
79 x = linspace (limits(1), limits(2), n)'; |
|
80 y = feval (fn, x); |
|
81 |
|
82 while (n < 2 .^ 20) |
|
83 y00 = interp1 (x0, y0, x, "linear"); |
|
84 err = 0.5 * max (abs ((y00 - y) ./ (y00 + y))(:)); |
|
85 if (err == err0 || 0.5 * max (abs ((y00 - y) ./ (y00 + y))(:)) < tol) |
|
86 break; |
|
87 endif |
|
88 x0 = x; |
|
89 y0 = y; |
|
90 err0 = err; |
|
91 n = 2 * (n - 1) + 1; |
|
92 x = linspace (limits(1), limits(2), n)'; |
|
93 y = feval (fn, x); |
|
94 endwhile |
|
95 else |
|
96 x = linspace (limits(1), limits(2), n)'; |
|
97 y = feval (fn, x); |
5820
|
98 endif |
|
99 |
|
100 if (length (limits) > 2) |
|
101 axis (limits); |
|
102 endif |
|
103 |
6781
|
104 if (have_linespec) |
|
105 plot (x, y, linespec); |
|
106 else |
|
107 plot (x, y); |
|
108 endif |
|
109 if (isvector(y)) |
|
110 legend (nam); |
|
111 else |
|
112 for i=1:columns(y) |
|
113 nams{i} = sprintf ("%s(:,%i)", nam, i); |
|
114 endfor |
|
115 legend (nams{:}); |
|
116 endif |
5820
|
117 endfunction |