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"}. |
3424
|
36 ## @end deftypefn |
5053
|
37 ## |
3426
|
38 ## @seealso{polyval, polyvalm, poly, roots, conv, deconv, residue, |
3424
|
39 ## filter, polyderiv, and polyinteg} |
|
40 |
|
41 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu> |
|
42 ## Created: May 1995 |
|
43 ## Nov 1998: Correctly handles complex coefficients |
3426
|
44 |
3424
|
45 function y = polyout (c, x) |
|
46 |
3457
|
47 if (nargin < 1) || (nargin > 2) || (nargout < 0) || (nargout > 1) |
|
48 usage("polyout (c, x)"); |
3424
|
49 endif |
|
50 |
4030
|
51 if (! isvector (c)) |
3424
|
52 error("polyout: first argument must be a vector"); |
|
53 endif |
3426
|
54 |
3424
|
55 if (nargin == 1) |
|
56 x = "s"; |
3457
|
57 elseif (! isstr(x)) |
3424
|
58 error("polyout: second argument must be a string"); |
|
59 endif |
|
60 |
|
61 n = length(c); |
|
62 if(n > 0) |
|
63 n1 = n+1; |
|
64 |
|
65 |
4898
|
66 tmp = coeff (c(1)); |
3457
|
67 for ii = 2:n |
|
68 if (real (c(ii)) < 0) |
|
69 ns = " - "; |
|
70 c(ii) = -c(ii); |
|
71 else |
|
72 ns = " + "; |
|
73 endif |
3424
|
74 |
4898
|
75 tmp = sprintf ("%s*%s^%d%s%s", tmp, x, n1-ii, ns, coeff (c(ii))); |
3426
|
76 |
3424
|
77 endfor |
|
78 else |
|
79 tmp = " "; |
|
80 endif |
|
81 |
|
82 if(nargout == 0) |
|
83 disp(tmp) |
|
84 else |
|
85 y = tmp; |
|
86 endif |
|
87 |
|
88 endfunction |
4898
|
89 |
|
90 function str = coeff(c) |
|
91 if (imag (c)) |
|
92 if (real (c)) |
|
93 str = sprintf ("(%s)", com2str(c)); |
|
94 else |
|
95 str = com2str(c); |
|
96 endif |
|
97 else |
|
98 str = num2str(c); |
|
99 endif |
|
100 |