home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / languages / rlab1_23a / CTB / printsys < prev    next >
Text File  |  1995-11-14  |  5KB  |  182 lines

  1. //----------------------------------------------------------------------------
  2. //
  3. // printsys
  4. //
  5. // Syntax: printsys(a,b,c,d,ulabels,ylabels,xlabels)
  6. //
  7. // This routine prints state space systems with labels above the matrices
  8. // and to the left of the matrix. It can also print a transfer function
  9. // model by printing the ratio of the polynomials.
  10. //
  11. // If it is called as printsys(a,b,c,d,ulabels,ylabels,xlabels) then the
  12. // state-space model given by a,b,c,d is printed with input, output, and
  13. // state labels contained in ulabels,ylabels,xlabels respectively. Note:
  14. // the labels are vectors of strings, such as:
  15. //
  16. //       ylabels[1]="phi"
  17. //       ylabels[2]="theta"
  18. //       ylabels[3]="psi"
  19. //
  20. // If it is called as printsys(a,b,c,d) then the system is written with
  21. // numerical labels for the matrices.
  22. //
  23. // If it is called for transfer functions such as printsys(num,den,"s")
  24. // then the transfer function is printed as a ration of two polynomials
  25. // in the transform variables "s"
  26. //
  27. // Copyright (C), by Jeffrey B. Layton, 1994
  28. // Version JBL 940513
  29. //----------------------------------------------------------------------------
  30.  
  31. rfile tfchk
  32. rfile abcdchk
  33. // rfile printmat
  34. rfile poly2str
  35.  
  36. printsys = function(a,b,c,d,ulab,ylab,xlab)
  37. {
  38.    local(A,tvar,msg,estr,B,s,dlen,t,nlen,itop,len,sdum,...
  39.          stmp,i,j,narg,num,nc,mn,nn,den,ulab,ylab,xlab)
  40.  
  41.    len = 12; // Label parameter.  Should match parameter in PRINTMAT.
  42.  
  43. // Count the number of input arguments
  44.    narg=0;
  45.    if (exist(a)) {narg=narg+1;}
  46.    if (exist(b)) {narg=narg+1;}
  47.    if (exist(c)) {narg=narg+1;}
  48.    if (exist(d)) {narg=narg+1;}
  49.    if (exist(ulab)) {narg=narg+1;}
  50.    if (exist(ylab)) {narg=narg+1;}
  51.    if (exist(xlab)) {narg=narg+1;}
  52.  
  53. // --- Determine which syntax is being used ---
  54.    if (narg == 2) {
  55.        A=tfchk(a,b);
  56.        num=A.numc;
  57.        den=A.denc;
  58.        tvar="s";
  59.    else if (narg == 3) {
  60.        A=tfchk(a,b);
  61.        num=A.numc;
  62.        den=A.denc;
  63.        if ( c.class != "string") {
  64.            error("printsys: Transfrom variable must be a string.");
  65.        }
  66.        tvar=c;
  67.    else if (narg == 4) {
  68.        msg="";
  69.        msg=abcdchk(a,b,c,d);
  70.        if ( msg != "" ) {
  71.            estr="printsys: "+msg;
  72.            error(estr);
  73.        }
  74.        if (a.nr > 0) {
  75.            nc=c.nc;
  76.        else
  77.        }
  78.  
  79.        for (i in 1:a.nr) {
  80.             sprintf(sdum,"%3.0f",i);
  81.             xlab[i]="x"+sdum;
  82.        }
  83.        printmat(a,"a",xlab,xlab);
  84.        for (i in 1:b.nc) {
  85.             sprintf(sdum,"%3.0f",i);
  86.             ylab[i]="y"+sdum;
  87.        }
  88.        printmat(b,"b",xlab,ulab);
  89.        for (i in 1:c.nr) {
  90.             sprintf(sdum,"%3.0f",i);
  91.             ulab[i]="u"+sdum;
  92.        }
  93.        printmat(c,"c",ylab,xlab);
  94.        printmat(c,"d",ylab,ulab);
  95.    else if ( (narg == 5) || (narg == 6) ) {
  96.         error("printsys: Wrong number of arguments.");
  97.    else
  98.        msg="";
  99.        msg=abcdchk(a,b,c,d);
  100.        if (msg != "") {
  101.            estr="printsys: "+msg;
  102.            error(estr);
  103.        }
  104.        if ( ulab.class != "string" ) {
  105.            error("printsys: ULAB must be a string.");
  106.        }
  107.        if ( ylab.class != "string" ) {
  108.            error("printsys: YLAB must be a string.");
  109.        }
  110.        if ( xlab.class != "string" ) {
  111.            error("printsys: XLAB must be a string.");
  112.        }
  113.  
  114.        // Put labels into format for PRINTMAT
  115.        printmat(a,"a",xlab,xlab);
  116.        printmat(b,"b",xlab,ulab);
  117.        printmat(c,"c",ylab,xlab);
  118.        printmat(d,"d",ylab,ulab);
  119.    }}}}
  120.  
  121.    if ( (narg == 2) || (narg == 3) ) {
  122.          nn=num.nr;
  123.          mn=num.nc;
  124.          B=poly2str(den,tvar);
  125.          s=B.s;
  126.          dlen=B.len;
  127.  
  128.          for (i in 1:nn) {
  129.               B=poly2str(num[i;],tvar);
  130.               t=B.s;
  131.               nlen=B.len;
  132.  
  133.     // Now print the polynomials
  134.               len=max([dlen,nlen])-3;
  135.               printf("%s"," \n");
  136.               if (nn == 1) {
  137.                   printf("%s"," num/den = \n");
  138.               else
  139.                   stmp="";
  140.                   stmp="num("+int2str(i)+"(/den = \n";
  141.                   printf("%s",stmp);
  142.               }
  143.               printf("%s"," \n");
  144.               if (length(t) < len+3) {
  145.                   stmp="";
  146.                   itop=(len+4-length(t))/2;
  147.                   for (j in 1:itop) {
  148.                        stmp=stmp+" ";
  149.                   }
  150.                   stmp=stmp+t;
  151.                   stmp=stmp+"\n";
  152.                   printf("%s",stmp);
  153.               else
  154.                   t=t+"\n";
  155.                   printf("%s",t);
  156.               }
  157.               stmp="  ";
  158.               for (j in 1:len) {
  159.                    stmp=stmp+"-";
  160.               }
  161.               stmp=stmp+"\n";
  162.               printf("%s",stmp);
  163.               if (length(s) < len+3) {
  164.                   itop=(len+4-length(s))/2;
  165.                   for (j in 1:itop) {
  166.                        stmp=stmp+" ";
  167.                   }
  168.                   stmp=stmp+s;
  169.                   stmp=stmp+"\n";
  170.                   printf("%s",stmp);
  171.               else
  172.                   printf("%s",s);
  173.                   printf("%s"," \n");
  174.               }
  175.          }
  176.    }
  177.  
  178.    printf("%s"," \n");
  179.  
  180.    return 0;
  181. };
  182.