1 ## Copyright (C) 2005-2012 Paul Kienzle
3 ## This file is part of Octave.
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.
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.
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/>.
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.
36 ## fplot ("cos", [0, 2*pi])
37 ## fplot ("[cos(x), sin(x)]", [0, 2*pi])
43 ## Author: Paul Kienzle <pkienzle@users.sf.net>
45 function fplot (fn, limits, n, fmt)
46 if (nargin < 2 || nargin > 4)
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");
60 have_linespec = false;
69 if (strcmp (typeinfo (fn), "inline function"))
72 elseif (isa (fn, "function_handle"))
74 elseif (all (isalnum (fn)))
77 fn = vectorize (inline (fn));
80 error ("fplot: first input argument must be a function handle, inline function or string");
85 x0 = linspace (limits(1), limits(2), 5)';
89 x = linspace (limits(1), limits(2), n)';
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)
102 x = linspace (limits(1), limits(2), n)';
106 x = linspace (limits(1), limits(2), n)';
116 if (length (limits) > 2)
123 for i = 1:columns (y)
124 nams{i} = sprintf ("%s(:,%i)", nam, i);
132 %! fplot ("cos", [0, 2*pi])
136 %! fplot ("[cos(x), sin(x)]", [0, 2*pi])