home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / control / polyout.m < prev    next >
Text File  |  1999-04-29  |  2KB  |  76 lines

  1. # Copyright (C) 1995,1998 A. Scottedward Hodel
  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
  17. # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. function y = polyout(c,x)
  20. #
  21. # usage: [y=] polyout(c[,x])
  22. #
  23. # print formatted polynom 
  24. #   c(x) = c(1) * x^n + ... + c(n) x + c(n+1)
  25. # in a string or to the screen (if y is omitted)
  26. # x defaults to the string "s"
  27. #
  28. #  SEE ALSO: polyval, polyvalm, poly, roots, conv, deconv, residue, 
  29. #    filter, polyderv, polyintg
  30.  
  31. # Written by A. Scottedward Hodel (scotte@eng.auburn.edu) May 1995)
  32. # Nov 1998: Correctly handles complex coefficients
  33.   
  34.   if (nargin < 1 ) || (nargin > 2) || (nargout < 0 ) || (nargout > 1)
  35.     usage("[y = ] polyout(c,[x])");
  36.   endif
  37.  
  38.   if (!is_vec(c))
  39.     error("polyout: first argument must be a vector");
  40.   endif
  41.   
  42.   if (nargin == 1)
  43.     x = 's';
  44.   elseif( ! isstr(x) )
  45.     error("polyout: second argument must be a string");
  46.   endif
  47.  
  48.   n = length(c);
  49.   if(n > 0)
  50.     n1 = n+1;
  51.  
  52.     if( imag(c(1)) )     tmp = com2str(c(1))
  53.     else                 tmp = num2str(c(1));       endif
  54.  
  55.     for ii=2:n
  56.       if(real(c(ii)) < 0)     ns = ' - ';    c(ii) = -c(ii);
  57.       else                    ns = ' + ';                      endif
  58.  
  59.       if( imag(c(ii)) )       nstr = sprintf("(%s)",com2str(c(ii)) );
  60.       else                    nstr = num2str(c(ii));           endif
  61.  
  62.       tmp = sprintf("%s*%s^%d%s%s",tmp,x,n1-ii,ns,nstr);
  63.       
  64.     endfor
  65.   else
  66.     tmp = " ";
  67.   endif
  68.  
  69.   if(nargout == 0)
  70.     disp(tmp)
  71.   else
  72.     y = tmp;
  73.   endif
  74.  
  75. endfunction
  76.