%Backward integration of the Riccati equation and additional equation

fort=nbData-1:-1:1

P(:,:,t)=P(:,:,t+1)+model.dt*(A'*P(:,:,t+1) + P(:,:,t+1)*A - P(:,:,t+1)*B*(R\B')*P(:,:,t+1)+Q(:,:,t+1));%See Eq. (5.1.11) in doc/TechnicalReport.pdf

%Optional feedforward term computation

d(:,t)=d(:,t+1)+model.dt*((A'-P(:,:,t+1)*B*(R\B'))*d(:,t+1)+P(:,:,t+1)*dtar(:,t+1)-P(:,:,t+1)*A*tar(:,t+1));%See Eq. (5.1.29) in doc/TechnicalReport.pdf

end

%Computation of the feedback term L and feedforward term M in u=-LX+M

fort=1:nbData

L(:,:,t)=R\B'*P(:,:,t);%See Eq. (5.1.30) in doc/TechnicalReport.pdf

M(:,t)=R\B'*d(:,t);%Optional feedforward term computation (See Eq. (5.1.30) in doc/TechnicalReport.pdf)