function [D,SingularFlag] = lft(D1,D2,indu1,indy1,indu2,indy2)
% LFT interconnection of SS models.
%		
%                        +-------+
%            w1 -------->|       |-------> z1
%                        |   D1  |
%                  +---->|       |-----+
%                  |     +-------+     |
%                u |                   | y
%                  |     +-------+     |
%                  +-----|       |<----+
%                        |   D2  |
%           z2 <---------|       |-------- w2
%                        +-------+

%   Copyright 1986-2020 The MathWorks, Inc.

% Sizes
[rs1,cs1] = iosize(D1);
indw1 = 1:cs1; indw1(:,indu1) = [];
indz1 = 1:rs1; indz1(:,indy1) = [];
[rs2,cs2] = iosize(D2);
indw2 = 1:cs2; indw2(:,indu2) = [];
indz2 = 1:rs2; indz2(:,indy2) = [];

if isfinite(D1) && isfinite(D2)
   % Input and output delays in feedback path are consolidated and 
   % folded into D2 to minimize the size of InternalDelay. This 
   % differs from feedbackG and must be handled separately.
   D2.Delay.Input(indu2,:) = D2.Delay.Input(indu2,:) + D1.Delay.Output(indy1,:);
   D1.Delay.Output(indy1,:) = 0;
   D2.Delay.Output(indy2,:) = D2.Delay.Output(indy2,:) + D1.Delay.Input(indu1,:);
   D1.Delay.Input(indu1,:) = 0;
   [ny2,nu2] = iosize(D2);
   D2in = zeros(nu2,1);
   D2in(indu2,:) = D2.Delay.Input(indu2,:);
   D2out = zeros(ny2,1);
   D2out(indy2,:) = D2.Delay.Output(indy2,:);
   if any(D2in) || any(D2out)
      D2 = utFoldDelay(D2,D2in,D2out);
   end
   % Build closed-loop model
   [D,SingularFlag] = feedbackG(D1,D2,indu1,indy1,indu2,indy2,+1,+1);
   % Eliminate I/Os involved in feedback loop
   D = getsubsys(D,[indz1 rs1+indz2],[indw1 cs1+indw2]);
else
   % One model is NaN
   D = createGain(D1,NaN(numel(indz1)+numel(indz2),numel(indw1)+numel(indw2)));
   SingularFlag = false;
end
