function [sys,x0,str,ts] = pendulo(t,x,u,flag,x_0)

switch flag,

  case 0,                      % Initialization

    [sys,x0,str,ts]=mdlInitializeSizes(x_0);

  case 1,                      % Derivatives
    sys=mdlDerivatives(t,x,u);

  case 3,                      % Outputs
    sys=mdlOutputs(t,x,u);

  case{2,4,5,9}                % Unused flags
     sys = [];

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

end
% end pendulo

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

function [sys,x0,str,ts]=mdlInitializeSizes(x_0)

% 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  = 2;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 2;
sizes.NumInputs      = 1;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;   % at least one sample time is needed
sys = simsizes(sizes);

% initialize the initial conditions
x0=x_0;

% str is always an empty matrix
str = [];

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

% end mdlInitializeSizes

%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================

function sys=mdlDerivatives(t,x,u)

%u(1): Force (MV)

%x(1): angle
%x(2): velocity

sys(1) = x(2);
sys(2) = 9.81*sin(x(1))-2*x(2)+u(1);
         
% end mdlDerivatives

%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================

function sys=mdlOutputs(t,x,u)

sys = x;

% end mdlOutputs
