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 |
|
6 ## under the terms of the GNU General Public License as published by the |
|
7 ## Free Software Foundation; either version 2, or (at your option) any |
|
8 ## later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 ## 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, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301 USA. |
3424
|
19 |
|
20 ## -*- texinfo -*- |
5016
|
21 ## @deftypefn {Function File} {} polyout (@var{c}, @var{x}) |
3457
|
22 ## Write formatted polynomial |
5016
|
23 ## @iftex |
|
24 ## @tex |
|
25 ## $$ c(x) = c_1 x^n + \ldots + c_n x + c_{n+1} $$ |
|
26 ## @end tex |
|
27 ## @end iftex |
|
28 ## @ifinfo |
3424
|
29 ## @example |
|
30 ## c(x) = c(1) * x^n + ... + c(n) x + c(n+1) |
|
31 ## @end example |
5016
|
32 ## @end ifinfo |
3457
|
33 ## and return it as a string or write it to the screen (if |
|
34 ## @var{nargout} is zero). |
5016
|
35 ## @var{x} defaults to the string @code{"s"}. |
3426
|
36 ## @seealso{polyval, polyvalm, poly, roots, conv, deconv, residue, |
3424
|
37 ## filter, polyderiv, and polyinteg} |
5642
|
38 ## @end deftypefn |
3424
|
39 |
|
40 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu> |
|
41 ## Created: May 1995 |
|
42 ## Nov 1998: Correctly handles complex coefficients |
3426
|
43 |
3424
|
44 function y = polyout (c, x) |
|
45 |
3457
|
46 if (nargin < 1) || (nargin > 2) || (nargout < 0) || (nargout > 1) |
6046
|
47 print_usage (); |
3424
|
48 endif |
|
49 |
4030
|
50 if (! isvector (c)) |
3424
|
51 error("polyout: first argument must be a vector"); |
|
52 endif |
3426
|
53 |
3424
|
54 if (nargin == 1) |
|
55 x = "s"; |
5443
|
56 elseif (! ischar(x)) |
3424
|
57 error("polyout: second argument must be a string"); |
|
58 endif |
|
59 |
|
60 n = length(c); |
|
61 if(n > 0) |
|
62 n1 = n+1; |
|
63 |
|
64 |
4898
|
65 tmp = coeff (c(1)); |
3457
|
66 for ii = 2:n |
|
67 if (real (c(ii)) < 0) |
|
68 ns = " - "; |
|
69 c(ii) = -c(ii); |
|
70 else |
|
71 ns = " + "; |
|
72 endif |
3424
|
73 |
4898
|
74 tmp = sprintf ("%s*%s^%d%s%s", tmp, x, n1-ii, ns, coeff (c(ii))); |
3426
|
75 |
3424
|
76 endfor |
|
77 else |
|
78 tmp = " "; |
|
79 endif |
|
80 |
|
81 if(nargout == 0) |
|
82 disp(tmp) |
|
83 else |
|
84 y = tmp; |
|
85 endif |
|
86 |
|
87 endfunction |
4898
|
88 |
|
89 function str = coeff(c) |
|
90 if (imag (c)) |
|
91 if (real (c)) |
|
92 str = sprintf ("(%s)", com2str(c)); |
|
93 else |
|
94 str = com2str(c); |
|
95 endif |
|
96 else |
|
97 str = num2str(c); |
|
98 endif |
|
99 |