function [D,SingularFlag] = feedback(D1,D2,indu,indy,sign)
% Feedback interconnection of LTI models (default implementation 
% in terms of feedbackG).
%
%                      +--------+
%          v --------->|        |--------> z
%                      |   D1   |
%          u --->O---->|        |----+---> y
%                |     +--------+    |
%                |                   |
%                +-----[   D2   ]<---+
%
%   Supported signatures:
%      feedback(D1,D2)                   (v=z=[], sign=-1)
%      feedback(D1,D2,indu,indy,sign)

%   Copyright 1986-2020 The MathWorks, Inc.
[ny1,nu1] = iosize(D1);
if isfinite(D1) && isfinite(D2)
   % Process inputs
   if nargin<3
      sign = -1;
      indu = 1:nu1; indy = 1:ny1;
   end
   % Compute closed-loop model
   [D,SingularFlag] = feedbackG(D1,D2,...
      indu,indy,1:numel(indy),1:numel(indu),sign,+1);
   % Eliminate I/Os of D2
   D = getsubsys(D,1:ny1,1:nu1);
else
   % One model is NaN
   [ny,nu] = iosize(D1);
   D = createGain(D1,NaN(ny,nu));
   SingularFlag = false;
end

