Mercurial > hg > octave-nkf
annotate scripts/plot/fplot.m @ 9665:1dba57e9d08d
use blas_trans_type for xgemm
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Sat, 26 Sep 2009 10:41:07 +0200 |
parents | 1bf0ce0930be |
children | b14fd5116c29 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2005, 2006, 2007, 2008, 2009 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}, | |
9040
dbd0c77e575e
Cleanup documentation file plot.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
28 ## @var{ylo}, @var{yhi}]}. @var{tol} is the default tolerance to use for the |
6781 | 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 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9040
diff
changeset
|
34 ## @group |
6699 | 35 ## fplot ("cos", [0, 2*pi]) |
36 ## 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
|
37 ## @end group |
5820 | 38 ## @end example |
6781 | 39 ## @seealso{plot} |
5820 | 40 ## @end deftypefn |
41 | |
7017 | 42 ## Author: Paul Kienzle <pkienzle@users.sf.net> |
43 | |
6781 | 44 function fplot (fn, limits, n, linespec) |
8847 | 45 if (nargin < 2 || nargin > 4) |
6046 | 46 print_usage (); |
5820 | 47 endif |
48 | |
49 if (nargin < 3) | |
6781 | 50 n = 0.002; |
5820 | 51 endif |
52 | |
6781 | 53 have_linespec = true; |
54 if (nargin < 4) | |
55 have_linespec = false; | |
56 endif | |
5820 | 57 |
6781 | 58 if (ischar (n)) |
59 have_linespec = true; | |
60 linespec = n; | |
61 n = 0.002; | |
62 endif | |
63 | |
6699 | 64 if (strcmp (typeinfo (fn), "inline function")) |
65 fn = vectorize (fn); | |
66 nam = formula (fn); | |
67 elseif (isa (fn, "function_handle")) | |
68 nam = func2str (fn); | |
5820 | 69 elseif (all (isalnum (fn))) |
6781 | 70 nam = fn; |
5820 | 71 else |
6781 | 72 fn = vectorize (inline (fn)); |
73 nam = formula (fn); | |
74 endif | |
75 | |
76 if (floor(n) != n) | |
77 tol = n; | |
7280 | 78 x0 = linspace (limits(1), limits(2), 5)'; |
6781 | 79 y0 = feval (fn, x0); |
80 err0 = Inf; | |
7280 | 81 n = 8; |
6781 | 82 x = linspace (limits(1), limits(2), n)'; |
83 y = feval (fn, x); | |
84 | |
85 while (n < 2 .^ 20) | |
86 y00 = interp1 (x0, y0, x, "linear"); | |
87 err = 0.5 * max (abs ((y00 - y) ./ (y00 + y))(:)); | |
88 if (err == err0 || 0.5 * max (abs ((y00 - y) ./ (y00 + y))(:)) < tol) | |
89 break; | |
90 endif | |
91 x0 = x; | |
92 y0 = y; | |
93 err0 = err; | |
94 n = 2 * (n - 1) + 1; | |
95 x = linspace (limits(1), limits(2), n)'; | |
96 y = feval (fn, x); | |
97 endwhile | |
98 else | |
99 x = linspace (limits(1), limits(2), n)'; | |
100 y = feval (fn, x); | |
5820 | 101 endif |
102 | |
6781 | 103 if (have_linespec) |
104 plot (x, y, linespec); | |
105 else | |
106 plot (x, y); | |
107 endif | |
8145
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
108 |
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
109 if (length (limits) > 2) |
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
110 axis (limits); |
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
111 endif |
7ef5b1b4e029
fplot.m: call axis after plot
John W. Eaton <jwe@octave.org>
parents:
7280
diff
changeset
|
112 |
8507 | 113 if (isvector (y)) |
6781 | 114 legend (nam); |
115 else | |
8507 | 116 for i = 1:columns (y) |
6781 | 117 nams{i} = sprintf ("%s(:,%i)", nam, i); |
118 endfor | |
119 legend (nams{:}); | |
120 endif | |
5820 | 121 endfunction |
7245 | 122 |
123 %!demo | |
124 %! fplot ("cos", [0, 2*pi]) | |
125 | |
126 %!demo | |
127 %! fplot ("[cos(x), sin(x)]", [0, 2*pi]) |