function csfunc_msfcn(block)
%CSFUNC An example MATLAB file S-function for defining a continuous system.  
%   Example MATLAB file S-function implementing continuous equations: 
%      x' = Ax + Bu
%      y  = Cx + Du
  
%   Copyright 1990-2022 The MathWorks, Inc.
    setup(block);
end

function setup(block)
    A=[-0.09   -0.01
        1          0];

    B=[ 1   -7
        0   -2];
    
    C=[ 0    2
        1   -5];
    
    D=[-3    0
        1    0];

    % Register number of ports
    block.NumInputPorts = 1;
    block.NumOutputPorts = 1;
    
    % Setup port properties to be inherited or dynamic
    block.SetPreCompInpPortInfoToDynamic;
    block.SetPreCompOutPortInfoToDynamic;
    
    % Override port properties
    block.InputPort(1).DatatypeID = 0;
    block.InputPort(1).Dimensions = 2;
    block.InputPort(1).DirectFeedthrough = true;
    block.OutputPort(1).DatatypeID = 0;
    block.OutputPort(1).Dimensions = 2;

    % Register number of states
    block.NumContStates = 2;

    block.SimStateCompliance = 'DefaultSimState';
    
    % initialize the array of sample times
    block.SampleTimes = [0 0];

    % Register runtime methods
    block.RegBlockMethod('InitializeConditions',@mdlInitConditions);
    block.RegBlockMethod('Derivatives', @(block)mdlDerivatives(block, A, B));
    block.RegBlockMethod('Outputs', @(block)mdlOutputs(block, C, D));
  
end

%
%=============================================================================
% mdlInitConditions
% Initialize continuous states.
%=============================================================================
%
function mdlInitConditions(block)
    block.ContStates.Data = [0 0];
end

%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%
function mdlDerivatives(block, A, B)
    block.Derivatives.Data = A*block.ContStates.Data + B*block.InputPort(1).Data;
end

%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function mdlOutputs(block, C, D)
    block.OutputPort(1).Data = C*block.ContStates.Data + D*block.InputPort(1).Data;
end
