www.pudn.com > Closid30.rar > Clstack.m, change:1998-05-19,size:4311b


function [s,ns]=clstack(s1,ns1,s2,ns2,option,opt1) 
%[s,ns]=clstack(s1,ns1,s2,ns2,option,opt1) 
% 
% stack systems [S1,NS1] and [S2,NS2] together.  
% Use OPTION to determin how: 
% OPTION  'col'        S1, S2 share same inputs 
%         'row'        S1, S2 share same outputs 
%         'parallel'   S1, S2 have no inputs and outputs together 
%         'add'        S = S1 + S2  
%         'sub'        S = S1 - S2 
%         'series'     S = S2 * S1 
% optional OPT1 tolerance for minimal realization. 
 
% Functions called: clsplit, minreal 
% 
% 28-12-1991 (originally named sysstack.m from Peter Bongers) 
% (c) Paul Van den Hof, Peter Bongers 
% Mechanical Engineering Systems and Control Group 
% Delft University of Technology 
% Update: 16-06-1997 Corrected to deal with static systems also. 
% Last correction: 19-05-1998 
 
if (nargin < 5), 
  error('Number of input arguments is not correct'); 
end; 
 
opt=0; 
gain=1; 
tol=1e-6; 
 
x=abs(option); 
lx=length(x); 
 
for i=1:lx, 
  if (x(i) >= 97), 
    x(i)=x(i)-32; 
  end 
end 
option=setstr(x); 
 
for i=1:lx-2, 
  if (option(i:i+2) == 'COL'), 
    opt=1; 
  end 
end  
 
for i=1:lx-2, 
  if (option(i:i+2) == 'ROW'), 
    opt=2; 
  end 
end  
 
for i=1:lx-7, 
  if (option(i:i+7) == 'PARALLEL'), 
    opt=3; 
  end 
end  
 
for i=1:lx-2, 
  if (option(i:i+2) == 'ADD'), 
    opt=4; 
  end 
end  
 
for i=1:lx-2, 
  if (option(i:i+2) == 'SUB'), 
    opt=5; 
  end 
end  
 
for i=1:lx-5, 
  if (option(i:i+5) == 'SERIES'), 
    opt=6; 
  end 
end  
 
if (opt==0), 
  error('wrong option, need: col row parallel add sub series'); 
end 
 
if (nargin == 6), 
  tol = opt1; 
end 
 
% Check for nonzero dimension, added by Paul Van den Hof, 16-06-1997. 
if ns1==0, 
  a1=0;b1=0;c1=0;d1=s1;ns1=1; 
else 
  [a1,b1,c1,d1]=clsplit(s1,ns1); 
end 
if ns2==0, 
  a2=0;b2=0;c2=0;d2=s2;ns2=1; 
else 
  [a2,b2,c2,d2]=clsplit(s2,ns2); 
end 
[no1,ni1]=size(d1); 
[no2,ni2]=size(d2); 
 
 
if (opt==1), 
  if (ni1 ~= ni2), 
    error('input dimensions of the two systems do not match'); 
  else, 
    a=[a1 zeros(ns1,ns2); zeros(ns2,ns1) a2]; 
	b=[b1;b2]; 
	c=[c1 zeros(no1,ns2); zeros(no2,ns1)  c2]; 
	d=[d1;d2]; 
	[as,bs,cs,ds]=minreal(a,b,c,d,tol); 
	s=[as bs;cs ds];ns=length(as); 
%    s=[     a1      zeros(ns1,ns2) b1; 
%       zeros(ns2,ns1)  a2          b2; 
%	      c1      zeros(no1,ns2) d1; 
%       zeros(no2,ns1)  c2          d2]; 
%    ns=ns1+ns2; 
  end; 
%  disp('col');keyboard   
elseif (opt==2),  
  if (no1 ~= no2), 
    error('output dimensions of the two systems do not match'); 
  else, 
    a=[a1 zeros(ns1,ns2); zeros(ns2,ns1) a2]; 
	b=[b1 zeros(ns1,ni2);zeros(ns2,ni1) b2]; 
    c=[c1 c2]; 
	d=[d1 d2]; 
	[as,bs,cs,ds]=minreal(a,b,c,d,tol); 
	s=[as bs;cs ds];ns=length(as); 
%    s=[     a1      zeros(ns1,ns2)     b1   zeros(ns1,ni2); 
%       zeros(ns2,ns1)  a2          zeros(ns2,ni1)       b2; 
%	      c1         c2              d1               d2]; 
%    ns=ns1+ns2; 
  end; 
%  disp('row');keyboard 
elseif (opt==3), 
    a=[a1 zeros(ns1,ns2); zeros(ns2,ns1) a2]; 
	b=[b1 zeros(ns1,ni2); zeros(ns2,ni1) b2]; 
    c=[c1 zeros(no1,ns2); zeros(no2,ns1) c2]; 
	d=[d1 zeros(no1,ni2); zeros(no2,ni1) d2]; 
	[as,bs,cs,ds]=minreal(a,b,c,d,tol); 
	s=[as bs;cs ds];ns=length(as); 
%	s=[     a1      zeros(ns1,ns2)    b1        zeros(ns1,ni2); 
%       zeros(ns2,ns1)  a2        zeros(ns2,ni1)       b2; 
%	      c1      zeros(no1,ns2)    d1        zeros(no1,ni2); 
%       zeros(no2,ns1)  c2        zeros(no2,ni1)       d2]; 
%    ns=ns1+ns2; 
%  disp('parallel');keyboard   
elseif (opt==4 | opt==5), 
  if (opt==5), 
    gain=-1; 
  end; 
  if (ni1 ~= ni2), 
    error('input dimensions of the two systems do not match'); 
  elseif (no1~=no2), 
    error('output dimensions of the two systems do not match'); 
  else, 
    a=[a1 zeros(ns1,ns2); zeros(ns2,ns1) a2]; 
    b=[b1;b2]; 
    c=[c1 gain*c2]; 
    d=d1+gain*d2; 
    [a,b,c,d]=minreal(a,b,c,d,tol); 
    s=[a b;c d];ns=length(a); 
  end; 
%  disp('add sub');keyboard   
else, 
  if (ni2 ~= no1), 
    disp(['input dimension of S2 does not match output ',... 
	       dimension of S1']); 
  else, 
    a = [a1 zeros(ns1,ns2); b2*c1 a2]; 
    b = [b1; b2*d1]; 
    c = [d2*c1 c2]; 
    d = d2*d1; 
    [a,b,c,d]=minreal(a,b,c,d,tol); 
    s=[a b;c d];ns=length(a); 
  end; 
end;