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) |
5820
|
43 if (nargin < 2 || nargin > 3) |
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; |
|
76 x0 = linspace (limits(1), limits(2), 3)'; |
|
77 y0 = feval (fn, x0); |
|
78 err0 = Inf; |
|
79 n = 5; |
|
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 |
|
101 if (length (limits) > 2) |
|
102 axis (limits); |
|
103 endif |
|
104 |
6781
|
105 if (have_linespec) |
|
106 plot (x, y, linespec); |
|
107 else |
|
108 plot (x, y); |
|
109 endif |
|
110 if (isvector(y)) |
|
111 legend (nam); |
|
112 else |
|
113 for i=1:columns(y) |
|
114 nams{i} = sprintf ("%s(:,%i)", nam, i); |
|
115 endfor |
|
116 legend (nams{:}); |
|
117 endif |
5820
|
118 endfunction |
7245
|
119 |
|
120 %!demo |
|
121 %! fplot ("cos", [0, 2*pi]) |
|
122 |
|
123 %!demo |
|
124 %! fplot ("[cos(x), sin(x)]", [0, 2*pi]) |