function [sys,x0,str,ts] = s_slug(t,x,u,flag)
persistent data 
switch flag

  %%%%%%%%%%%%%%%%%%
  % Initialization %
  %%%%%%%%%%%%%%%%%%
  case 0,
    
    [sys,x0,str,ts,data]=InitializeSlug;


  %%%%%%%%%%%%%%%
  % Derivatives %
  %%%%%%%%%%%%%%%
  case 1,
   
    sys=slug_3D_sim(0,x,u,'derivatives',data);

  %%%%%%%%%%
  % Update %
  %%%%%%%%%%
  case 2,
    % sys=mdlUpdate(t,x,u);

  %%%%%%%%%%%
  % Outputs %
  %%%%%%%%%%%
  case 3,
   
    sys=slug_3D_sim(0,x,u,'measurements',data);


  %%%%%%%%%%%%%%%%%%%%%%%
  % GetTimeOfNextVarHit %
  %%%%%%%%%%%%%%%%%%%%%%%
  case 4,
    % sys=mdlGetTimeOfNextVarHit(t,x,u);

  %%%%%%%%%%%%%
  % Terminate %
  %%%%%%%%%%%%%
  case 9,
    % sys=mdlTerminate(t,x,u);

  %%%%%%%%%%%%%%%%%%%%
  % Unexpected flags %
  %%%%%%%%%%%%%%%%%%%%
  otherwise
    error(['Unhandled flag = ',num2str(flag)]);

end

% end sfuntmpl

%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%

function [sys,x0,str,ts,data]=InitializeSlug

%
% call simsizes for a sizes structure, fill it in and convert it to a
% sizes array.
%
% Note that in this example, the values are hard coded.  This is not a
% recommended practice as the characteristics of the block are typically
% defined by the S-function parameters.
%
sizes = simsizes;

sizes.NumContStates  = 3;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 6;
sizes.NumInputs      = 3;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;   % at least one sample time is needed

sys = simsizes(sizes);

%
% initialize the initial conditions
%
%INITIALISERER til biffurkasjonspunktet. Skal bare finne et startpunkt for
%simuleringen, så det spiller ingen rolle hvilket punkt dette er! Merk at
%initalize2 har satt konstantene K1, K2 og K3 (de blir ikke beregnet).

mG_in=0.362;
mL_in=8.64;
z=0.13;
P1_biff=71.7e5;
h1_biff=0.1;
P2_biff=53.5e5;
%Initialization

[x0,y_stasj,data]=initialize2(z,mG_in,mL_in,P1_biff,h1_biff,P2_biff);
%
% str is always an empty matrix
%
str = [];

%
% initialize the array of sample times
%
ts  = [0 0];
%data=initialize;
% end mdlInitializeSizes



