function [sys,x0,str,ts] = VisualPendul(t,x,u,flag)
% S-function graphique pour dessiner le pendule
% Bloc 2-entrées 0-sorties
%    u(1) = Absisse du chariot
%    u(2) = angle du pendule avec la verticale
%   (0 est la position instable, positif=sens horaire !)
switch flag,
  case 0
    [sys,x0,str,ts] = mdlInitializeSizes(t); % Initialization
  case 2
    sys = mdlUpdates(t,x,u); % Update discrete states
  case 3
    sys = mdlOutputs(t,x,u); % Calculate outputs
  case {1, 4, 9} % Unused flags
    sys = [];
  otherwise
    error(['unhandled flag = ',num2str(flag)]); % Error handling
end
% End of dsfunc.
%==============================================================
% Initialization
%==============================================================
function [sys,x0,str,ts] = mdlInitializeSizes(t)
% Call simsizes for a sizes structure, fill it in, and convert it 
% to a sizes array.
sizes = simsizes;
sizes.NumContStates  = 0;
sizes.NumDiscStates  = 2;
sizes.NumOutputs     = 0;
sizes.NumInputs      = 2;
sizes.DirFeedthrough = 0; % Matrix D is empty.
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
h=figure;
set(h,'Position',[8,41,588,316]);
axis([-0.2,3.2,-0.2,1.2]),
axis equal,
hold on,
p=plot(0,0,'.');
x0  = [h;p];      % Initialize the discrete states.
str = [];          % Set str to an empty matrix.
ts  = [0.1 0];       % sample time: [period, offset]
% End of mdlInitializeSizes.
%==============================================================
% Update the discrete states
%==============================================================
function sys = mdlUpdates(t,x,u)
h=x(1);
p=x(2);
figure(x(1)),
set(p,'Visible','Off'),
p=plot([u(1),u(1)+sin(u(2))],[0,cos(u(2))],'r');
set(p,'LineWidth',3),
drawnow,
pause(0.01);
sys =[h;p];
% End of mdlUpdate.
%==============================================================
% Calculate outputs
%==============================================================
function sys = mdlOutputs(t,x,u)
sys = [];
% End of mdlOutputs.