home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 10.ddi / CONTROL.DI$ / POLY2STR.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  1.3 KB  |  56 lines

  1. function [s,len] = poly2str(den,tvar)
  2. %POLY2STR Return polynomial as string.
  3. %
  4. %    S = POLY2STR(P,'s') or S=POLY2STR(P,'z') returns a string S 
  5. %    consisting of the polynomial coefficients in the vector P 
  6. %    multiplied by powers of the transform variable 's' or 'z'.
  7. %
  8. %       Example: POLY2STR([1 0 2],'s') returns the string  's^2 + 2'. 
  9. %
  10. %    [S,LEN] = POLY2STR(P,'s') also returns the maximum wrapped length
  11. %    of the polynomial.
  12. %
  13. %    See also: PRINTSYS.
  14.  
  15. %    Clay M. Thompson  7-24-90
  16. %    Copyright (c) 1986-93 by the MathWorks, Inc.
  17.  
  18. nd = length(den);
  19.  
  20. % Form denominator
  21. dlen=0;   first_term = 1;  old = 0;
  22. for j=1:nd
  23.   term=den(j);
  24.   if first_term&(term~=0),
  25.     if (term==1)&(j~=nd),
  26.       s=['  '];
  27.     else
  28.       s=['   ',num2str(term)];
  29.     end
  30.     first_term=0;
  31.   else
  32.     if (term==1)&(j~=nd),
  33.       s=[s,' + '];
  34.     elseif (term==0)
  35.       % do nothing
  36.     elseif (term>=0), 
  37.       s=[s,' + ',num2str(term)];
  38.     else
  39.       s=[s,' - ',num2str(abs(term))];
  40.     end
  41.   end
  42.   if term~=0,
  43.     if (nd-j>1),
  44.       s=[s,' ',tvar,'^',num2str(nd-j)];
  45.     elseif (nd-j==1),
  46.       s=[s,' ',tvar];
  47.     end
  48.   end
  49.   if (length(s)-old>63)&(j~=nd), 
  50.     len=max([len,length(s)-old]); s=[s,13,10,'  ',];
  51.     old = length(s)-2;
  52.   end
  53. end
  54. if isempty(s), s=['   0']; end
  55. len = max([len,length(s)-old]);
  56.