3424
|
1 ## Copyright (C) 1995, 1998 Auburn University. All rights reserved. |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
7016
|
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. |
3424
|
9 ## |
7016
|
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. |
3424
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3424
|
18 |
|
19 ## -*- texinfo -*- |
5016
|
20 ## @deftypefn {Function File} {} polyout (@var{c}, @var{x}) |
3457
|
21 ## Write formatted polynomial |
5016
|
22 ## @iftex |
|
23 ## @tex |
|
24 ## $$ c(x) = c_1 x^n + \ldots + c_n x + c_{n+1} $$ |
|
25 ## @end tex |
|
26 ## @end iftex |
|
27 ## @ifinfo |
3424
|
28 ## @example |
|
29 ## c(x) = c(1) * x^n + ... + c(n) x + c(n+1) |
|
30 ## @end example |
5016
|
31 ## @end ifinfo |
3457
|
32 ## and return it as a string or write it to the screen (if |
|
33 ## @var{nargout} is zero). |
5016
|
34 ## @var{x} defaults to the string @code{"s"}. |
3426
|
35 ## @seealso{polyval, polyvalm, poly, roots, conv, deconv, residue, |
3424
|
36 ## filter, polyderiv, and polyinteg} |
5642
|
37 ## @end deftypefn |
3424
|
38 |
|
39 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu> |
|
40 ## Created: May 1995 |
|
41 ## Nov 1998: Correctly handles complex coefficients |
3426
|
42 |
3424
|
43 function y = polyout (c, x) |
|
44 |
3457
|
45 if (nargin < 1) || (nargin > 2) || (nargout < 0) || (nargout > 1) |
6046
|
46 print_usage (); |
3424
|
47 endif |
|
48 |
4030
|
49 if (! isvector (c)) |
3424
|
50 error("polyout: first argument must be a vector"); |
|
51 endif |
3426
|
52 |
3424
|
53 if (nargin == 1) |
|
54 x = "s"; |
5443
|
55 elseif (! ischar(x)) |
3424
|
56 error("polyout: second argument must be a string"); |
|
57 endif |
|
58 |
|
59 n = length(c); |
|
60 if(n > 0) |
|
61 n1 = n+1; |
|
62 |
|
63 |
4898
|
64 tmp = coeff (c(1)); |
3457
|
65 for ii = 2:n |
|
66 if (real (c(ii)) < 0) |
|
67 ns = " - "; |
|
68 c(ii) = -c(ii); |
|
69 else |
|
70 ns = " + "; |
|
71 endif |
3424
|
72 |
4898
|
73 tmp = sprintf ("%s*%s^%d%s%s", tmp, x, n1-ii, ns, coeff (c(ii))); |
3426
|
74 |
3424
|
75 endfor |
|
76 else |
|
77 tmp = " "; |
|
78 endif |
|
79 |
|
80 if(nargout == 0) |
|
81 disp(tmp) |
|
82 else |
|
83 y = tmp; |
|
84 endif |
|
85 |
|
86 endfunction |
4898
|
87 |
|
88 function str = coeff(c) |
|
89 if (imag (c)) |
|
90 if (real (c)) |
|
91 str = sprintf ("(%s)", com2str(c)); |
|
92 else |
|
93 str = com2str(c); |
|
94 endif |
|
95 else |
|
96 str = num2str(c); |
|
97 endif |
|
98 |