home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 2.ddi / MUTOOLS1.DI$ / SYSIC.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  10.0 KB  |  309 lines

  1. % SYSIC is a M-file used to form interconnections of SYSTEM
  2. %   matrices (it also works for interconnections of VARYING
  3. %   matrices). A number of variables need to be in the workspace
  4. %   to run SYSIC. These are explained in the manual.
  5. %
  6. %   See also: ABV, MADD, DAUG, MMULT, SBS and STARP.
  7.  
  8. %   mod 8/17  10:06pm
  9.  
  10. if ~exist('systemnames')
  11.   disp(['error_in_SYSIC: need SYSTEMNAMES to be set']);
  12.   return
  13. end
  14. if ~exist('inputvar')
  15.   disp(['error_in_SYSIC: need INPUTVAR to be set']);
  16.   return
  17. end
  18. if ~exist('outputvar')
  19.   disp(['error_in_SYSIC: need OUTPUTVAR to be set']);
  20.   return
  21. end
  22. if ~exist('cleanupsysic')
  23.   cleanupsysic = 'no';
  24. end
  25.  
  26. [numxinp_ms,xndf_ms,xipnt_ms,xinames_ms,xinlen_ms,...
  27.      ximaxl_ms,err_ms] = inpstuff(inputvar);
  28. if err_ms == 1
  29.   disp(['error_in_SYSIC: problem in INPUTVAR']);
  30.   return
  31. end
  32.  
  33. [numsys_ms,names_ms,namelen_ms,maxnamelen_ms] = namstuff(systemnames);
  34.  
  35. if ximaxl_ms > maxnamelen_ms
  36.   maxxxx_ms = ximaxl_ms;
  37. else
  38.   maxxxx_ms = maxnamelen_ms;
  39. end
  40. para_ms = [];
  41. sysdata_ms= [];
  42. inpoint_ms = zeros(1,numsys_ms+xndf_ms);
  43. outpoint_ms = zeros(1,numsys_ms+xndf_ms);
  44. sysdata_ms = zeros(3,numsys_ms+xndf_ms);
  45. systype_ms = [];
  46. varyflg_ms = 0;
  47. systflg_ms = 0;
  48. for i_ms=1:numsys_ms
  49.   tmp_ms = names_ms(i_ms,1:namelen_ms(i_ms));
  50.   extmp_ms = exist(tmp_ms);
  51.   if extmp_ms ~= 1
  52.     disp(['error_in_SYSIC:']);
  53.     disp(['systemnames variable    "' tmp_ms '"   not found in workspace']);
  54.     return
  55.   end
  56.   eval(['[mtype_ms,mrows_ms,mcols_ms,mnum_ms] = minfo(' tmp_ms ');']);
  57.   if mtype_ms == 'vary' & systflg_ms == 1
  58.     error('VARYING matrices and SYSTEM matrices not both allowed');
  59.     return
  60.   elseif mtype_ms == 'syst' & varyflg_ms == 1
  61.     error('VARYING matrices and SYSTEM matrices not both allowed');
  62.     return
  63.   elseif mtype_ms == 'syst'
  64.     systflg_ms = 1;
  65.   elseif mtype_ms == 'vary'
  66.     varyflg_ms = 1;
  67.   end
  68. %
  69. % SYSDATA is (3 by NUMSYS+XNDF), it has states (or points),
  70. %                  outputs (rows), and inputs (columns). Note
  71. %                  that it will have data for the XTERNAL INPUTS
  72. % SYSTYPE is (NUMSYS by 4) character string. it DOES NOT have
  73. %                                         the external inputs
  74. %
  75.   sysdata_ms(1,i_ms) = mnum_ms;
  76.   sysdata_ms(2,i_ms) = mrows_ms;
  77.   sysdata_ms(3,i_ms) = mcols_ms;
  78.   systype_ms = [systype_ms ; mtype_ms];
  79. %
  80. % INPOINT(i)+j points to the j'th input of SYSTEM_i.  it does
  81. %      have the external inputs included at the end (numsys+xndf)
  82. % OUTPOINT(i)+k points to the k'th output of SYSTEM_i. it does
  83. %      have the external inputs included at the end (numsys+xndf)
  84. %
  85.   tmp2_ms = ['para_ms = daug(para_ms,' tmp_ms ');'];
  86.   eval(tmp2_ms);
  87.   if i_ms > 1
  88.     inpoint_ms(i_ms) = sysdata_ms(3,i_ms-1) + inpoint_ms(i_ms-1);
  89.     outpoint_ms(i_ms) = sysdata_ms(2,i_ms-1) + outpoint_ms(i_ms-1);
  90.   end
  91. end
  92.  
  93.  
  94. inpoint_ms(numsys_ms+1) = inpoint_ms(numsys_ms) + sysdata_ms(3,numsys_ms);
  95. outpoint_ms(numsys_ms+1) = outpoint_ms(numsys_ms) + sysdata_ms(2,numsys_ms);
  96. sysdata_ms(:,numsys_ms+1) = [0; xipnt_ms(1,2) ; xipnt_ms(1,2)];
  97. for i_ms=2:xndf_ms
  98.   inpoint_ms(numsys_ms+i_ms)=inpoint_ms(numsys_ms+i_ms-1)+xipnt_ms(i_ms-1,2);
  99.   outpoint_ms(numsys_ms+i_ms)=outpoint_ms(numsys_ms+i_ms-1)+xipnt_ms(i_ms-1,2);
  100.   sysdata_ms(:,numsys_ms+i_ms)=[0; xipnt_ms(i_ms,2) ; xipnt_ms(i_ms,2)];
  101. end
  102.  
  103. nameds_ms = [];
  104. for i_ms=1:numsys_ms
  105.   padd_ms = mtblanks(maxxxx_ms-namelen_ms(i_ms));
  106.   nameds_ms = [nameds_ms ; names_ms(i_ms,1:namelen_ms(i_ms)) padd_ms];
  107. end
  108. for i_ms=1:xndf_ms
  109.   padd_ms = mtblanks(maxxxx_ms-xinlen_ms(i_ms));
  110.   nameds_ms = [nameds_ms ; xinames_ms(i_ms,1:xinlen_ms(i_ms)) padd_ms];
  111. end
  112. names_ms = nameds_ms;
  113.  
  114. % all names are called NAME_MS with length NAMELEN
  115.  
  116. namelen_ms = [namelen_ms ; xinlen_ms];
  117.  
  118. % add the identity at the bottom for the external inputs
  119.  
  120. para_ms = daug(para_ms,eye(numxinp_ms));
  121. [alltype_ms,allrows_ms,allcols_ms,allnum_ms] = minfo(para_ms);
  122.  
  123. % DETERMINE NUMBER OF OUTPUTS
  124.  
  125. numout_ms = 0;
  126. [ard_ms,arl_ms,er_ms] = pass1(outputvar);
  127. if er_ms ~= 0
  128.   disp(['error_in_SYSIC: problem in outputvar']);
  129.   return
  130. end
  131. for i_ms=1:length(arl_ms)
  132.   [od_ms,odl_ms,fsys_ms,gains_ms,er_ms] = ...
  133.        pass2(i_ms,ard_ms,arl_ms,names_ms,namelen_ms,sysdata_ms);
  134.   if er_ms ~= 0
  135.     disp(['error_in_SYSIC: problem in outputvar']);
  136.     return
  137.   end
  138.   sz_ms = 0;
  139.   for j_ms = 1:length(odl_ms)
  140.     [out_ms,er_ms] = pass3(j_ms,od_ms,odl_ms);
  141.     if er_ms ~= 0
  142.       disp(['error_in_SYSIC: problem with commas or colons in outputvar']);
  143.       return
  144.     end
  145.     if sz_ms == 0
  146.       sz_ms = length(out_ms);
  147.     else
  148.       if sz_ms ~= length(out_ms)
  149.         disp(['error_in_SYSIC: inconsistent number of signals,']);
  150.         disp(['CHECK: outputvar portion  ' ''''...
  151.             ard_ms(i_ms,1:arl_ms(i_ms)) '''']);
  152.         return
  153.       end
  154.     end
  155.   end
  156.   numout_ms = numout_ms + length(out_ms);
  157. end
  158.  
  159. % Feedback interconnection matrix starts as EMPTY
  160. myk_ms = [];
  161.  
  162. % MAIN LOOP TO DETERMINE INTERCONNECTING FEEDBACK
  163.  
  164. for k_ms=1:numsys_ms+1
  165.   location_ms = 0;  %  location in feedback matrix for input_to_sysi
  166.   if k_ms <= numsys_ms
  167.     tmp_ms = names_ms(k_ms,1:namelen_ms(k_ms));
  168.     invar_ms = ['input_to_' tmp_ms ' = '];
  169.     eval(['ex_ms = exist(''input_to_' tmp_ms ''');']);
  170.     if ex_ms ~= 1
  171.       disp(['warning_in_SYSIC: INPUTS TO ' tmp_ms ' NOT BEING USED']);
  172.     else
  173.       eval(['var_ms = input_to_' tmp_ms ';']);
  174.     end
  175.     ff_ms = zeros(sysdata_ms(3,k_ms),allrows_ms); % feedback for input_to_sysi
  176.   else
  177.     invar_ms = ['outputvar = '];
  178.     var_ms = outputvar;
  179.     ff_ms = zeros(numout_ms,allrows_ms); % feedback for outputvar
  180.   end
  181.  
  182. % length(arl) tell how many GROUPS of inputs (semicolons)
  183.  
  184.   [ard_ms,arl_ms,er_ms] = pass1(var_ms);
  185.   if er_ms == 1
  186.     disp(['error_in_SYSIC: expression lacks square brackets[],']);
  187.     disp(['   CHECK: ' invar_ms '''' var_ms '''']);
  188.     return
  189.   elseif er_ms == 2
  190.     disp(['error_in_SYSIC: inconsistent number of parenthesis,']);
  191.     disp(['   CHECK: ' invar_ms '''' var_ms '''']);
  192.     return
  193.   elseif er_ms == 3 | er_ms == 4
  194.     disp(['error_in_SYSIC: right paren before left paren,']);
  195.     disp(['   CHECK: ' invar_ms '''' var_ms '''']);
  196.     return
  197.   elseif er_ms == 5
  198.     disp(['error_in_SYSIC: square brackets inside parenthesis,']);
  199.     disp(['   CHECK: ' invar_ms '''' var_ms '''']);
  200.     return
  201.   else
  202.     szin_ms = 0;
  203.     for i_ms=1:length(arl_ms)
  204.  
  205. %      for each group, pass2 splits ARD into the summation
  206. %      of its parts - each part has the same number of signals,
  207. %      just from different components (also have constant
  208. %      scaling factors)
  209.  
  210.       [od_ms,odl_ms,fsys_ms,gains_ms,er_ms] = ...
  211.            pass2(i_ms,ard_ms,arl_ms,names_ms,namelen_ms,sysdata_ms);
  212.       if er_ms == 1
  213.         disp('error_in_SYSIC: too many scalar multiplies')
  214.         disp(['   CHECK: ' invar_ms '''' var_ms '''']);
  215.         return
  216.       elseif er_ms == 2
  217.         disp('error_in_SYSIC: poorly placed parenthesis')
  218.         disp(['   CHECK: ' invar_ms '''' var_ms '''']);
  219.         return
  220.       elseif er_ms == 3
  221.         disp('error_in_SYSIC: wrong number of parenthesis')
  222.         disp(['   CHECK: ' invar_ms '''' var_ms '''']);
  223.         return
  224.       elseif er_ms == 4
  225.         disp('error_in_SYSIC: scalar mult positioned incorrectly')
  226.         disp(['   CHECK: ' invar_ms '''' var_ms '''']);
  227.         return
  228.       elseif er_ms == 5
  229.         disp('error_in_SYSIC: unknown system specified')
  230.         disp(['   CHECK: ' invar_ms '''' var_ms '''']);
  231.         return
  232.       else
  233.         szout_ms = 0;
  234.         for j_ms=1:length(odl_ms)
  235.           [out_ms,er_ms] = pass3(j_ms,od_ms,odl_ms);
  236.           if szout_ms == 0
  237.             szout_ms = length(out_ms);
  238.           elseif szout_ms ~= length(out_ms)
  239.             disp('error_in_SYSIC: inconsistent combination of signals:')
  240.             disp(['   CHECK: ' invar_ms '''' var_ms '''']);
  241.             return
  242.           end
  243.           if er_ms == 1
  244.             disp('error_in_SYSIC: two or more commas in a row')
  245.             disp(['   CHECK: ' invar_ms '''' var_ms '''']);
  246.             return
  247.           elseif er_ms == 2
  248.             disp('error_in_SYSIC: too many colons between commas')
  249.             disp(['   CHECK: ' invar_ms '''' var_ms '''']);
  250.             return
  251.           else
  252.             sysloc_ms = outpoint_ms(fsys_ms(j_ms));
  253.             ff_ms = gaino(out_ms,gains_ms(j_ms),ff_ms,location_ms,sysloc_ms);
  254.           end
  255.         end
  256.         szin_ms = szin_ms + szout_ms;
  257.         location_ms = location_ms + length(out_ms);
  258.       end
  259.     end
  260.     if k_ms <= numsys_ms
  261.       if sysdata_ms(3,k_ms) ~= szin_ms
  262.         disp('error_in_SYSIC: wrong number of inputs to a system')
  263.         disp(['   CHECK: ' invar_ms '''' var_ms '''']);
  264.         return
  265.       end
  266.     end
  267.     myk_ms = [myk_ms ; ff_ms];
  268.   end
  269.  end
  270. % END OF MAIN LOOP
  271.  
  272. % WRAP IT UP
  273.  topd_ms = allcols_ms - numxinp_ms;
  274.  topid_ms = eye(topd_ms);
  275.  paraout_ms = mmult(myk_ms,para_ms);
  276.  if exist('sysoutname')
  277.    eval([sysoutname ' = starp(topid_ms,paraout_ms,topd_ms,topd_ms);']);
  278.  else
  279.    ic_ms = starp(topid_ms,paraout_ms,topd_ms,topd_ms);
  280.  end
  281.  
  282. % Cleanup
  283.  
  284. if strcmp(cleanupsysic,'yes')
  285.   if exist('sysoutname')
  286.     clear sysoutname
  287.   end
  288.   clear outputvar inputvar systemnames
  289.   for i_ms=1:numsys_ms
  290.     var_ms = ['clear input_to_' names_ms(i_ms,1:namelen_ms(i_ms)) ';'];
  291.     eval(var_ms);
  292.   end
  293. end
  294.  
  295. clear para_ms sysdata_ms inpoint_ms outpoint_ms
  296. clear maxnamelen_ms sysdata_ms systype_ms varyflg_ms systflg_ms
  297. clear topd_ms topid_ms mtype_ms mrows_ms mcols_ms mnum_ms
  298. clear extmp_ms ard_ms arl_ms names_ms
  299. clear allcols_ms allnum_ms allrows_ms alltype_ms
  300. clear myk_ms paraout_ms location_ms out_ms gains_ms ff_ms i_ms
  301. clear od_ms odl_ms fsys_ms er_ms k_ms j_ms tmp_ms padd_ms
  302. clear ex_ms feed_ms namelen_ms sysloc_ms var_ms argu_ms
  303. clear invar_ms sz_ms szin_ms szout_ms ximaxl_ms xinames_ms
  304. clear numxinp_ms numout_ms tmp2_ms numsys_ms
  305. clear err_ms maxxxx_ms szout_ms xinlen_ms xipnt_ms xndf_ms
  306. clear cleanupsysic nameds_ms
  307. %
  308. % Copyright MUSYN INC 1991,  All Rights Reserved
  309.