function xp=fSys(t,x)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       
% function xp=fSys(t,x)
% Right hand side to the 
% differential equations 
% of the system
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%

global ti tf

global A B C Q0 Q R S

global p tp 

% Extract right riccati parameters by linear interpolation
tRev=tf-(t-ti);

pRicc_t=(interp1(tp,p,tRev))';

% The Riccati variables

n=length(A);
[P,q,c]=XtoP(pRicc_t,n);


Vx=2*(P*x+q);

u=(-1/2)*inv(R)*(B'*Vx+2*S'*x);

xp=A*x+B*u;

