function [sys,x0] = HEXs(t,x,u,flag)

%  Simulink interface to HEX.m     
%
% Inputs:    t    - time in [s].
%            X    - States. A total of 3*N states.
%                   Temperatures of cold side gas, hot side gas and heat
%                   exchanger wall elements
%            U(1) - Inlet temperature hot side, Tc0 [C]
%            U(2) - Inlet hot gas temperature, Th0 [C]
%            U(3) - Mass flow of gas, m [kg/s]
%            U(4) - Mass flow of air fed to process, mair [kg/s]
%            U(5) - Hot side bypass fraction ( a=0 -> no bypass), a

% Outputs:   sys and x0 as described in the SIMULINK manual.
%            when flag is 0 sys contains sizes and x0 contains 
%            initial condition. 
%            when flag is 1, sys contains the derivatives,
%            and when flag is 3 sys contains outputs; 
%            y(1:N)     - Gas temperatures in elements 1-N,hot side [C]
%            y(N+1:2N)  - Gas temperatures in elements 1-N, cold side [C]
%            y(2N+1:3N) - Wall temperatures in elements 1-N [C]
%            y(3N+1)    - Hot gas outlet temp. after bypass [C]

global  T0HEX par

if abs(flag) == 1
  % Return state derivatives.
  sys = HEX(t,x,u);
  
elseif abs(flag) == 3
  % Return system outputs.
  N = par.Nhex;
  sys(1,1:3*N+1) = x(1:3*N+1);          
  
elseif flag == 0
  % Ininitalize the system
 % ssvaluesinit
  parameters
  x0 = T0HEX;
  N = par.Nhex;
  sys = [3*N+1, 0, 3*N+1, 5, 0, 0];
  
else
  sys = [];  
  
end
  
