changelog shortlog tags changeset files revisions annotate raw

scripts/plot/fplot.m

changeset 10289: 4b124317dc38
parent:dbd0c77e575e
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (55 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2005, 2006, 2007, 2008, 2009 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 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.
32##
33## @example
34## @group
35## fplot ("cos", [0, 2*pi])
36## fplot ("[cos(x), sin(x)]", [0, 2*pi])
37## @end group
38## @end example
39## @seealso{plot}
40## @end deftypefn
41
42## Author: Paul Kienzle <pkienzle@users.sf.net>
43
44function fplot (fn, limits, n, linespec)
45 if (nargin < 2 || nargin > 4)
46 print_usage ();
47 endif
48
49 if (nargin < 3)
50 n = 0.002;
51 endif
52
53 have_linespec = true;
54 if (nargin < 4)
55 have_linespec = false;
56 endif
57
58 if (ischar (n))
59 have_linespec = true;
60 linespec = n;
61 n = 0.002;
62 endif
63
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);
69 elseif (all (isalnum (fn)))
70 nam = fn;
71 else
72 fn = vectorize (inline (fn));
73 nam = formula (fn);
74 endif
75
76 if (floor(n) != n)
77 tol = n;
78 x0 = linspace (limits(1), limits(2), 5)';
79 y0 = feval (fn, x0);
80 err0 = Inf;
81 n = 8;
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);
101 endif
102
103 if (have_linespec)
104 plot (x, y, linespec);
105 else
106 plot (x, y);
107 endif
108
109 if (length (limits) > 2)
110 axis (limits);
111 endif
112
113 if (isvector (y))
114 legend (nam);
115 else
116 for i = 1:columns (y)
117 nams{i} = sprintf ("%s(:,%i)", nam, i);
118 endfor
119 legend (nams{:});
120 endif
121endfunction
122
123%!demo
124%! fplot ("cos", [0, 2*pi])
125
126%!demo
127%! fplot ("[cos(x), sin(x)]", [0, 2*pi])