home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / control / sysappend.m < prev    next >
Text File  |  1999-03-05  |  6KB  |  167 lines

  1. # Copyright (C) 1996,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. # Octave is distributed in the hope that it will be useful, but WITHOUT 
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  11. # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
  12. # for more details.
  13. # You should have received a copy of the GNU General Public License 
  14. # along with Octave; see the file COPYING.  If not, write to the Free 
  15. # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  16.  
  17. function retsys = sysappend(sys,b,c,d,outname,inname,yd)
  18.   # 
  19.   # function retsys = sysappend(sys,b[,c,d,outname,inname,yd])
  20.   #
  21.   # This function appends new inputs and/or outputs to a system
  22.   # Inputs:
  23.   #    sys:  system data structure
  24.   #    b: matrix to be appended to sys "B" matrix (empty if none)
  25.   #    c: matrix to be appended to sys "C" matrix (empty if none)
  26.   #     d: revised sys d matrix (can be passed as [] if the revised d is all 
  27.   #        zeros)
  28.   #     outname: names for new outputs
  29.   #     inname: names for new inputs
  30.   #     yd: indicator for which new outputs are continuous/discrete 
  31.   #         (yd(i) = 0 or , respectively)
  32.   # result:
  33.   #   sys.b := [sys.b , b]
  34.   #   sys.c := [sys.c  ]
  35.   #            [ c     ]
  36.   #   sys.d := [sys.d | D12 ]
  37.   #            [D21   | D22 ]
  38.   #         where D12, D21, and D22 are the appropriate dimensioned blocks
  39.   #         of the input parameter d.  The leading block D11 of d is ignored.
  40.   # If inname and outname are not given as arguments, the new inputs and 
  41.   # outputs are be assigned default names.  
  42.   # yd is a vector of length rows(c), and indicates which new outputs are
  43.   # discrete (yd(ii) = 1) and which are continuous (yd(ii) = 0).
  44.   # Default value for yd is:
  45.   #     sys = continuous or mixed: yd = zeros(1,rows(c))
  46.   #     sys = discrete:            yd = ones(1,rows(c))
  47.   
  48.   # written by John Ingram August 1996
  49.   
  50.   sav_implicit_str_to_num_ok = implicit_str_to_num_ok;    # save for later
  51.   sav_empty_list_elements_ok = empty_list_elements_ok;
  52.  
  53.   empty_list_elements_ok = 1;                implicit_str_to_num_ok = 1;
  54.   
  55.   # check input arguments
  56.   if ( (nargin < 2) | (nargin > 7) | (!is_struct(sys)))
  57.     usage("retsys = sysappend(sys,b,c[,d,outname,inname,yd]) ");
  58.   elseif(!is_struct(sys))
  59.     error("sys must be a system data structure");
  60.   endif
  61.   
  62.   # default system type must be state space form
  63.   [Aa,Ab,Ac,Ad,Ats,Ann,Anz,Ast,Ain,Aout,Ayd] = sys2ss(sys);
  64.   [Ann,Anz,Am,Ap] = sysdimensions(sys);
  65.  
  66.   #default c
  67.   if(nargin < 3)      c = [];                                endif
  68.   
  69.   #default d
  70.   if(nargin < 4)     make_d = 1;
  71.   elseif(isempty(d)) make_d = 1;
  72.   else               make_d = 0;                             endif
  73.   if(make_d)         d = zeros(rows(c)+Ap,columns(b) + Am);  endif
  74.  
  75.   #
  76.   # Append new input(s) if any
  77.   Bm = max(columns(d),columns(b)+Am);
  78.   if(Bm != Am)    
  79.     # construct new signal names
  80.     if(nargin >= 6)   # new names were passed
  81.       if(!isstr(inname))
  82.         error("inname must be a string");
  83.       elseif(rows(inname) != (Bm - Am))
  84.         error(sprintf("%d new inputs requested; inname(%dx%d)", ...
  85.       (Bm-Am),rows(inname),columns(inname)));
  86.       endif
  87.     else
  88.       inname = sysdefioname(Bm,"u",(Am+1));
  89.     endif
  90.     if(Am)   Ain = append(Ain,inname);
  91.     else     Ain = inname;        endif
  92.  
  93.     # default b matrix
  94.     if(isempty(b))     b  = zeros(Ann+Anz,(Bm-Am));          
  95.     elseif(rows(b) != Ann+Anz | columns(b) != (Bm-Am))
  96.         error(sprintf("b(%dx%d); should be (%dx%d)", rows(b), columns(b), ...
  97.           (Ann+Anz), (Bm-Am)));
  98.     endif
  99.  
  100.     # append new b matrix
  101.     Ab = [Ab,b];    # empty_list_elements_ok=1 makes this ok
  102.   endif
  103.  
  104.   #
  105.   # Append new output(s) if any
  106.   Bp = max(rows(d),rows(c)+Ap);
  107.   if(Bp != Ap)  
  108.  
  109.     # construct new signal names, output classification
  110.     if(nargin >= 5)  # new names were passed
  111.       if(!isstr(outname))
  112.         error("outname must be a string");
  113.       elseif(rows(outname) != (Bp - Ap))
  114.         error(sprintf("%d new outputs requested; outname(%dx%d)", ...
  115.           (Bp-Ap),rows(outname),columns(outname)));
  116.       endif
  117.     else
  118.       outname = sysdefioname(Bp,"y",(Ap+1));
  119.     endif
  120.     if(Ap)   Aout = append(Aout,outname);
  121.     else     Aout = outname;                endif
  122.  
  123.     # construct new yd entries
  124.     if(nargin == 7)
  125.       if(!is_vector(yd))
  126.         error(sprintf("yd(%dx%d) must be a vector",rows(yd),columns(yd)))
  127.       elseif(rows(c) != length(yd) & rows(d) != length(yd))
  128.         error(sprintf("length(yd) = %d; c(%dx%d), d(%dx%d); mismatch", ...
  129.       length(yd), rows(c), columns(c),rows(d),columns(d)));
  130.       endif
  131.     else
  132.       # default yd values
  133.       yd = ones(1,Bp)*( (Ats > 0) & (Ann == 0)  & isempty(find(Ayd == 0)) ) ;
  134.     endif
  135.     Ayd = [vec(Ayd);vec(yd)];
  136.  
  137.     # default c matrix
  138.     if(isempty(c))      c = zeros((Bp-Ap),Ann+Anz);          
  139.     elseif(columns(c) != Ann+Anz | rows(c) != (Bp-Ap))
  140.         error(sprintf("c(%dx%d); should be (%dx%d)", rows(c), columns(c), ...
  141.           (Bp-Ap), (Ann+Anz) ));
  142.     endif
  143.  
  144.     # append new c matrix
  145.     Ac = [Ac;c];    # empty_list_elements_ok=1 makes this ok
  146.   endif
  147.  
  148.   # check d matrix
  149.   if(isempty(d)) d = zeros(Bp,Bm);
  150.   elseif(rows(d) != Bp | columns(d) != Bm)
  151.     error(sprintf("d(%dx%d) should be (%dx%d)",rows(d), columns(d), Bp, Bp));
  152.   endif
  153.  
  154.   # Splice in original D matrix  
  155.   if(Am & Ap)          d(1:Ap, 1:Am) = Ad;       endif
  156.   Ad = d;
  157.   
  158.   # construct return system
  159.   retsys = ss2sys(Aa,Ab,Ac,Ad,Ats,Ann,Anz,Ast,Ain,Aout,find(Ayd == 1));
  160.   
  161.   implicit_str_to_num_ok = sav_implicit_str_to_num_ok;    # restore value
  162.   empty_list_elements_ok = sav_empty_list_elements_ok;
  163.  
  164. endfunction
  165.