function [sys,x0] = hexch1(t,x,u,flag)
Indexch=1; %index of the heat exchanger 
N = 10; %Order of the conter-current heat exchanger model 
%
%  Simulink interface, exchanger consisting of three series of lump systems :
%                      Hot side, Wall, Cold side
%
%            t    - time in seconds
%            X    - State, the first N states are hot temperatures,
%                          the second N states are wall temperatures,
%                          the last N states are cold temperatures.
% Inputs: 
%            U(1) - Inlet hot temperature
%            U(2) - Inlet hot mass flowrate
%            U(3) - Inlet cold temperature
%            U(4) - Inlet cold mass flowrate

% Outputs:   When flag is 0 sys contains sizes and x0 contains initial condition. 
%            When flag is 1, sys contains the state derivatives.
%            When flag is 3 sys contains outputs; 
%            y(1)    - Oulet hot temperature
%            y(2)    - Oulet hot mass flowrate
%            y(3)    - Outlet cold temperature
%            y(4)    - Outlet cold mass flowrate

if abs(flag) == 1     % Return state derivatives.
  sys = mathisen(t,x,u,N,Indexch);
  
elseif abs(flag) == 3 % Return system outputs .
  sys(1,1) = x(N);       %  Oulet hot temperature                
  sys(2,1) = x(3*N);     % Outlet cold temperature
                     
  
elseif flag == 0      % Ininitalize the system
  x0 = stationary(Indexch,N);
  sys = [3*N,0,2,4,0,0]; %number of continuous states, discrete states, outputs, inputs
else
  sys = [];  
end
end
