comparison scripts/plot/fplot.m @ 6781:3058060c560f

[project @ 2007-07-19 08:07:31 by dbateman]
author dbateman
date Thu, 19 Jul 2007 08:07:32 +0000
parents 3f4ccca05612
children 76e3d985ae56
comparison
equal deleted inserted replaced
6780:38bc358b6c9a 6781:3058060c560f
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 ## 02110-1301, USA. 18 ## 02110-1301, USA.
19 19
20 ## -*- texinfo -*- 20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {} fplot (@var{fn}, @var{limits}) 21 ## @deftypefn {Function File} {} fplot (@var{fn}, @var{limits})
22 ## @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 (@var{fn}, @var{limits}, @var{n})
24 ## @deftypefnx {Function File} {} fplot (@dots{}, @var{LineSpec})
23 ## Plot a function @var{fn}, within the defined limits. @var{fn} 25 ## Plot a function @var{fn}, within the defined limits. @var{fn}
24 ## an be either a string, a function handle or an inline function. 26 ## an be either a string, a function handle or an inline function.
25 ## The limits of the plot are given by @var{limits} of the form 27 ## The limits of the plot are given by @var{limits} of the form
26 ## @code{[@var{xlo}, @var{xhi}]} or @code{[@var{xlo}, @var{xhi}, 28 ## @code{[@var{xlo}, @var{xhi}]} or @code{[@var{xlo}, @var{xhi},
27 ## @var{ylo}, @var{yhi}]}. @var{n} is the number of points to use and 29 ## @var{ylo}, @var{yhi}]}. @var{tol} is the default tolerance to use for the
28 ## defaults to 100. 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{LineSpec} is passed to the plot
32 ## command.
29 ## 33 ##
30 ## @example 34 ## @example
31 ## fplot ("cos", [0, 2*pi]) 35 ## fplot ("cos", [0, 2*pi])
32 ## fplot ("[cos(x), sin(x)]", [0, 2*pi]) 36 ## fplot ("[cos(x), sin(x)]", [0, 2*pi])
33 ## @end example 37 ## @end example
38 ## @seealso{plot}
34 ## @end deftypefn 39 ## @end deftypefn
35 40
36 function fplot (fn, limits, n) 41 function fplot (fn, limits, n, linespec)
37 if (nargin < 2 || nargin > 3) 42 if (nargin < 2 || nargin > 3)
38 print_usage (); 43 print_usage ();
39 endif 44 endif
40 45
41 if (nargin < 3) 46 if (nargin < 3)
42 n = 100; 47 n = 0.002;
43 endif 48 endif
44 49
45 x = linspace (limits(1), limits(2), n)'; 50 have_linespec = true;
51 if (nargin < 4)
52 have_linespec = false;
53 endif
46 54
47 nam = fn; 55 if (ischar (n))
56 have_linespec = true;
57 linespec = n;
58 n = 0.002;
59 endif
60
48 if (strcmp (typeinfo (fn), "inline function")) 61 if (strcmp (typeinfo (fn), "inline function"))
49 fn = vectorize (fn); 62 fn = vectorize (fn);
50 y = fn (x);
51 nam = formula (fn); 63 nam = formula (fn);
52 elseif (isa (fn, "function_handle")) 64 elseif (isa (fn, "function_handle"))
53 y = fn (x);
54 nam = func2str (fn); 65 nam = func2str (fn);
55 elseif (all (isalnum (fn))) 66 elseif (all (isalnum (fn)))
67 nam = fn;
68 else
69 fn = vectorize (inline (fn));
70 nam = formula (fn);
71 endif
72
73 if (floor(n) != n)
74 tol = n;
75 x0 = linspace (limits(1), limits(2), 3)';
76 y0 = feval (fn, x0);
77 err0 = Inf;
78 n = 5;
79 x = linspace (limits(1), limits(2), n)';
56 y = feval (fn, x); 80 y = feval (fn, x);
81
82 while (n < 2 .^ 20)
83 y00 = interp1 (x0, y0, x, "linear");
84 err = 0.5 * max (abs ((y00 - y) ./ (y00 + y))(:));
85 if (err == err0 || 0.5 * max (abs ((y00 - y) ./ (y00 + y))(:)) < tol)
86 break;
87 endif
88 x0 = x;
89 y0 = y;
90 err0 = err;
91 n = 2 * (n - 1) + 1;
92 x = linspace (limits(1), limits(2), n)';
93 y = feval (fn, x);
94 endwhile
57 else 95 else
58 finl = vectorize (inline (fn)); 96 x = linspace (limits(1), limits(2), n)';
59 y = finl (x); 97 y = feval (fn, x);
60 endif 98 endif
61 99
62 if (length (limits) > 2) 100 if (length (limits) > 2)
63 axis (limits); 101 axis (limits);
64 endif 102 endif
65 103
66 plot (x, y, [";", nam, ";"]); 104 if (have_linespec)
67 105 plot (x, y, linespec);
106 else
107 plot (x, y);
108 endif
109 if (isvector(y))
110 legend (nam);
111 else
112 for i=1:columns(y)
113 nams{i} = sprintf ("%s(:,%i)", nam, i);
114 endfor
115 legend (nams{:});
116 endif
68 endfunction 117 endfunction