1## Copyright (C) 2005, 2006, 2007, 2008, 2009 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 the defined limits. @var{fn}
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},
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
30## number points to use in the plot. The @var{fmt} argument is passed
31## to the plot command.
35## fplot ("cos", [0, 2*pi])
36## fplot ("[cos(x), sin(x)]", [0, 2*pi])
42## Author: Paul Kienzle <pkienzle@users.sf.net>
44function fplot (fn, limits, n, linespec)
45 if (nargin < 2 || nargin > 4)
55 have_linespec = false;
64 if (strcmp (typeinfo (fn), "inline function"))
67 elseif (isa (fn, "function_handle"))
69 elseif (all (isalnum (fn)))
72 fn = vectorize (inline (fn));
78 x0 = linspace (limits(1), limits(2), 5)';
82 x = linspace (limits(1), limits(2), n)';
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)
95 x = linspace (limits(1), limits(2), n)';
99 x = linspace (limits(1), limits(2), n)';
104 plot (x, y, linespec);
109 if (length (limits) > 2)
116 for i = 1:columns (y)
117 nams{i} = sprintf ("%s(:,%i)", nam, i);
124%! fplot ("cos", [0, 2*pi])
127%! fplot ("[cos(x), sin(x)]", [0, 2*pi])