home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / control / sysgroup.m < prev    next >
Encoding:
Text File  |  1999-12-15  |  4.5 KB  |  152 lines

  1. ## Copyright (C) 1996, 1998, 1999 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 
  17. ## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. 
  18.  
  19. ## -*- texinfo -*-
  20. ## @deftypefn {Function File } { @var{sys} =} sysgroup ( @var{Asys}, @var{Bsys})
  21. ## Combines two systems into a single system
  22. ## 
  23. ## @strong{Inputs}
  24. ## @var{Asys}, @var{Bsys}: system data structures
  25. ## 
  26. ## @strong{Outputs}
  27. ##  @math{sys = @r{block diag}(Asys,Bsys)}
  28. ## @example
  29. ## @group
  30. ##          __________________
  31. ##          |    ________    |
  32. ## u1 ----->|--> | Asys |--->|----> y1
  33. ##          |    --------    |
  34. ##          |    ________    |
  35. ## u2 ----->|--> | Bsys |--->|----> y2
  36. ##          |    --------    |
  37. ##          ------------------
  38. ##               Ksys
  39. ## @end group
  40. ## @end example
  41. ## The function also rearranges the internal state-space realization of @var{sys}
  42. ## so that the
  43. ##  continuous states come first and the discrete states come last.
  44. ##  If there are duplicate names, the second name has a unique suffix appended
  45. ##  on to the end of the name.
  46. ## 
  47. ## @end deftypefn
  48.  
  49. function sys = sysgroup (...)
  50.  
  51.   ## A. S. Hodel August 1995
  52.   ## modified by John Ingram July 1996
  53.   ## A. S. Hodel: modified for variable number of arguments 1999
  54.  
  55.   save_emp = empty_list_elements_ok;
  56.   empty_list_elements_ok = 1;
  57.  
  58.     
  59.   if(nargin < 1)
  60.     usage("sys = sysgroup(Asys{,Bsys,...})");
  61.   endif
  62.  
  63.   ## collect all arguments
  64.   arglist = list();
  65.   va_start();
  66.   for kk=1:nargin
  67.     arglist(kk) = va_arg();
  68.     if(!is_struct(nth(arglist,kk)))
  69.       error("sysgroup: argument %d is not a data structure",kk);
  70.     endif
  71.   endfor
  72.  
  73.   if(nargin == 2)
  74.     ## the usual case; group the two systems together
  75.     Asys = nth(arglist,1);
  76.     Bsys = nth(arglist,2);
  77.   
  78.     ## extract information from Asys, Bsys to consruct sys
  79.     Asys = sysupdate(Asys,"ss");
  80.     Bsys = sysupdate(Bsys,"ss");
  81.     [n1,nz1,m1,p1] = sysdimensions(Asys);
  82.     [n2,nz2,m2,p2] = sysdimensions(Bsys);
  83.     [Aa,Ab,Ac,Ad,Atsam,An,Anz,Ast,Ain,Aout,Ayd] = sys2ss(Asys);
  84.     [Ba,Bb,Bc,Bd,Btsam,Bn,Bnz,Bst,Bin,Bout,Byd] = sys2ss(Bsys);
  85.     nA = An + Anz;
  86.     nB = Bn + Bnz;
  87.   
  88.     if(p1*m1*p2*m2 == 0)
  89.       error("sysgroup: argument lacks inputs and/or outputs");
  90.   
  91.     elseif((Atsam + Btsam > 0) & (Atsam * Btsam == 0) )
  92.       warning("sysgroup: creating combination of continuous and discrete systems")
  93.   
  94.     elseif(Atsam != Btsam)
  95.       error("sysgroup: Asys.tsam=%e, Bsys.tsam =%e", Atsam, Btsam);
  96.     endif
  97.   
  98.     A = [Aa,zeros(nA,nB); zeros(nB,nA),Ba];
  99.     B = [Ab,zeros(nA,m2); zeros(nB,m1),Bb];
  100.     C = [Ac,zeros(p1,nB); zeros(p2,nA),Bc];
  101.     D = [Ad,zeros(p1,m2); zeros(p2,m1),Bd];
  102.     tsam = max(Atsam,Btsam);
  103.   
  104.     ## construct combined signal names; stnames must check for pure gain blocks
  105.     if(isempty(Ast))
  106.       stname = Bst;
  107.     elseif(isempty(Bst))
  108.       stname = Ast;
  109.     else
  110.       stname  = append(Ast, Bst);
  111.     endif
  112.     inname  = append(Ain, Bin);
  113.     outname = append(Aout,Bout);
  114.   
  115.     ## Sort states into continous first, then discrete
  116.     dstates = ones(1,(nA+nB));
  117.     if(An)
  118.       dstates(1:(An)) = zeros(1,An);
  119.     endif
  120.     if(Bn)
  121.       dstates((nA+1):(nA+Bn)) = zeros(1,Bn);
  122.     endif
  123.     [tmp,pv] = sort(dstates);
  124.     A = A(pv,pv);
  125.     B = B(pv,:);
  126.     C = C(:,pv);
  127.     stname = stname(pv);
  128.   
  129.     ## check for duplicate signal names
  130.     inname = sysgroupn(inname,"input");
  131.     stname = sysgroupn(stname,"state");
  132.     outname = sysgroupn(outname,"output");
  133.   
  134.     ## mark discrete outputs
  135.     outlist = find([Ayd, Byd]);
  136.   
  137.     ## build new system
  138.     sys = ss2sys(A,B,C,D,tsam,An+Bn,Anz+Bnz,stname,inname,outname);
  139.  
  140.   else
  141.     ## multiple systems (or a single system); combine together one by one
  142.     sys = nth(arglist,1);
  143.     for kk=2:length(arglist)
  144.       printf("sysgroup: kk=%d\n",kk);
  145.       sys = sysgroup(sys,nth(arglist,kk));
  146.     endfor
  147.   endif
  148.   
  149.   empty_list_elements_ok = save_emp;
  150.     
  151. endfunction
  152.