home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / r / rlab / CTB / cloop < prev    next >
Encoding:
Text File  |  1995-11-15  |  4.4 KB  |  156 lines

  1. //-----------------------------------------------------------------------
  2. //
  3. // cloop
  4. //
  5. // Syntax: G = cloop(a,b,c,d,e,f)
  6. //
  7. // This routine closes the loop of either a state-space system or
  8. // a transfer function system for either a continuous or discrete system,
  9. // using unity feedback. Three cases of input arguments are possible:
  10. //
  11. //     State Space Case    
  12. // ------------------------
  13. //
  14. // Calling the routine with G=cloop(a,b,c,d,sign) will produce a
  15. // state-space model of the closed loop system obtained by feeding
  16. // all of the outputs to all of the inputs. If sign = 1 then positive
  17. // feedback is used. If sign = -1 then negative feedback is used.
  18. // In either case, the resulting system will have all of the same inputs
  19. // and outputs of the original model.
  20. //   Note: A list is returned containing the closed loop model.
  21. //         G.ac = Closed Loop A matrix
  22. //         G.bc = Closed Loop B matrix
  23. //         G.cc = Closed Loop C matrix
  24. //         G.dc = Closed Loop D matrix
  25. //
  26. //     State Space Case with Specified Inputs and Outputs    
  27. // ----------------------------------------------------------
  28. //
  29. // Calling the routine with G=cloop(a,b,c,d,outputs,inputs) will
  30. // produce a state-space model of the closed loop system which
  31. // is obtained by feeding back the specified outputs to the
  32. // specified inputs. The vector "outputs" contains indices of the
  33. // desired outputs and the vector "inputs" contains indices of the
  34. // desired inputs. Positive feedback is assumed in this case.
  35. // If negative feedback is desired, then the vector "inputs" needs
  36. // to have negative values.
  37. //   Note: A list is returned containing the closed loop model.
  38. //         G.ac = Closed Loop A matrix
  39. //         G.bc = Closed Loop B matrix
  40. //         G.cc = Closed Loop C matrix
  41. //         G.dc = Closed Loop D matrix
  42. //
  43. //     Transfer Function Case    
  44. // ------------------------------
  45. //
  46. // Calling the routine with G=cloop(num,den,sign) produces the SISO
  47. // model of the closed loop system obtained by unity feedback with
  48. // the sign "sign" (positive or negative feedback).
  49. //   Note: A list is returned containing the closed loop model.
  50. //         G.ac = Closed loop numerator
  51. //         G.bc = Closed Loop denominator
  52. //         G.cc = empty
  53. //         G.dc = empty
  54. //
  55. // Copyright (C), by Jeffrey B. Layton, 1994-95
  56. // Version JBL 950106
  57. //-----------------------------------------------------------------------
  58.  
  59. rfile tfchk
  60. rfile abcdchk
  61.  
  62. cloop = function(a,b,c,d,e,f)
  63. {
  64.    local(A,sgn2,term1,term2,term3,narg,msg,inputs,outputs,nout,dtil,...
  65.          dbar,bbar,ctil,dtilbar,ac,bc,cc,dc,estr)
  66.  
  67. // Count number of input arguments
  68.    narg=0;
  69.    if (exist(a)) {narg=narg+1;}
  70.    if (exist(b)) {narg=narg+1;}
  71.    if (exist(c)) {narg=narg+1;}
  72.    if (exist(d)) {narg=narg+1;}
  73.    if (exist(e)) {narg=narg+1;}
  74.    if (exist(f)) {narg=narg+1;}
  75.  
  76. // Determine which syntax is being used.
  77. // -------------------------------------
  78.  
  79.    if (narg == 2) {
  80. // transfer function without sign on feedback
  81.        A=tfchk(a,b);
  82.  
  83.        ac=A.numc;
  84.        bc=A.denc-A.numc;
  85.        cc=[];
  86.        dc=[];
  87.    }
  88.    if (narg == 3) {
  89. // transfer function with sign on feedback
  90.        A=tfchk(a,b);
  91.  
  92.        ac=A.numc;
  93.        bc=A.denc-sign(c)*A.numc;
  94.        cc=[];
  95.        dc=[];
  96.    }
  97.  
  98.   if (narg >= 4) {
  99. // State Space model
  100.       msg="";
  101.       msg=abcdchk(a,b);
  102.       if (msg != "") {
  103.           estr="CLOOP: "+msg;
  104.           error(estr);
  105.       }
  106.  
  107. // define "input" and "outputs"
  108.       if (narg == 4) {
  109.           outputs=1:c.nr;
  110.           inputs=1:b.nc;
  111.           sgn2=-1;
  112.       }
  113.       if (narg == 5) {
  114.           outputs=1:c.nr;
  115.           inputs=[1:b.nc];
  116.           sgn2=sign(e);
  117.       }
  118.       if (narg == 6) {
  119.           outputs=e;
  120.           inputs=abs(f);
  121.           if (sign(f[1]) > 0 ) {
  122.               sgn2=1;
  123.           else
  124.               sgn2=-1;
  125.           }
  126.       }
  127.  
  128. // Form Closed Loop State-space System 
  129.       nout=length(outputs);
  130.       if (length(inputs) != length(outputs)) {
  131.           error("CLOOP: The number of feedback inputs and outputs are not equal");
  132.       }
  133.  
  134.       dtil=d[outputs;];
  135.       dbar=d[;inputs];
  136.       bbar=b[;inputs];
  137.       ctil=c[outputs;];
  138.  
  139.       dtilbar=dtil[;inputs];
  140.       if (sgn2 > 0) {
  141.           term1=eye(nout,nout)-dtilbar;
  142.       else
  143.           term1=eye(nout,nout)+dtilbar;
  144.       }
  145.  
  146.       term2=solve(term1',dbar')';
  147.       term3=solve(term1',bbar')';
  148.       dc=d+sgn2*term2*dtil;
  149.       bc=b+sgn2*term3*dtil;
  150.       cc=c+sgn2*term2*ctil;
  151.       ac=a+sgn2*term3*ctil;
  152.    }
  153.  
  154.    return << ac=ac; bc=bc; cc=cc; dc=dc >>
  155. };
  156.