]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/fplot.m
update packages
[CreaPhase.git] / octave_packages / m / plot / fplot.m
1 ## Copyright (C) 2005-2012 Paul Kienzle
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 3 of the License, or (at
8 ## your option) 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, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {} fplot (@var{fn}, @var{limits})
21 ## @deftypefnx {Function File} {} fplot (@var{fn}, @var{limits}, @var{tol})
22 ## @deftypefnx {Function File} {} fplot (@var{fn}, @var{limits}, @var{n})
23 ## @deftypefnx {Function File} {} fplot (@dots{}, @var{fmt})
24 ## Plot a function @var{fn} within defined limits.
25 ## @var{fn} is a function handle, inline function, or string
26 ## containing the name of the function to evaluate.
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},
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
31 ## number points to use in the plot.  The @var{fmt} argument is passed
32 ## to the plot command.
33 ##
34 ## @example
35 ## @group
36 ## fplot ("cos", [0, 2*pi])
37 ## fplot ("[cos(x), sin(x)]", [0, 2*pi])
38 ## @end group
39 ## @end example
40 ## @seealso{plot}
41 ## @end deftypefn
42
43 ## Author: Paul Kienzle <pkienzle@users.sf.net>
44
45 function fplot (fn, limits, n, fmt)
46   if (nargin < 2 || nargin > 4)
47     print_usage ();
48   endif
49
50   if (!isreal (limits) || (numel (limits) != 2 && numel (limits) != 4))
51     error ("fplot: second input argument must be a real vector with 2 or 4 elements");
52   endif
53
54   if (nargin < 3)
55     n = 0.002;
56   endif
57
58   have_linespec = true;
59   if (nargin < 4)
60     have_linespec = false;
61   endif
62
63   if (ischar (n))
64     have_linespec = true;
65     fmt = n;
66     n = 0.002;
67   endif
68
69   if (strcmp (typeinfo (fn), "inline function"))
70     fn = vectorize (fn);
71     nam = formula (fn);
72   elseif (isa (fn, "function_handle"))
73     nam = func2str (fn);
74   elseif (all (isalnum (fn)))
75     nam = fn;
76   elseif (ischar (fn))
77      fn = vectorize (inline (fn));
78      nam = formula (fn);
79   else
80     error ("fplot: first input argument must be a function handle, inline function or string");
81   endif
82
83   if (floor(n) != n)
84     tol = n;
85     x0 = linspace (limits(1), limits(2), 5)';
86     y0 = feval (fn, x0);
87     err0 = Inf;
88     n = 8;
89     x = linspace (limits(1), limits(2), n)';
90     y = feval (fn, x);
91
92     while (n < 2 .^ 20)
93       y00 = interp1 (x0, y0, x, "linear");
94       err = 0.5 * max (abs ((y00 - y) ./ (y00 + y))(:));
95       if (err == err0 || 0.5 * max (abs ((y00 - y) ./ (y00 + y))(:)) < tol)
96         break;
97       endif
98       x0 = x;
99       y0 = y;
100       err0 = err;
101       n = 2 * (n - 1) + 1;
102       x = linspace (limits(1), limits(2), n)';
103       y = feval (fn, x);
104     endwhile
105   else
106     x = linspace (limits(1), limits(2), n)';
107     y = feval (fn, x);
108   endif
109
110   if (have_linespec)
111     plot (x, y, fmt);
112   else
113     plot (x, y);
114   endif
115
116   if (length (limits) > 2)
117     axis (limits);
118   endif
119
120   if (isvector (y))
121     legend (nam);
122   else
123     for i = 1:columns (y)
124       nams{i} = sprintf ("%s(:,%i)", nam, i);
125     endfor
126     legend (nams{:});
127   endif
128 endfunction
129
130 %!demo
131 %! clf
132 %! fplot ("cos", [0, 2*pi])
133
134 %!demo
135 %! clf
136 %! fplot ("[cos(x), sin(x)]", [0, 2*pi])