function [input,mem] = dd_proper(A,B,Ft,h,N,xk,mem)
% dd_proper.m
%
%    state predictive controller by proper approximation 
%
%       [input,mem] = dd_proper(A,B,Ft,h,N,xk,mem)
%
%    input: A, B: system matrices    
%           Ft: state feedback gain
%           h: delay
%           N: division number of approximation
%           xk: state
%           mem: memory for current and past inputs
%    output: input: current input           
%             mem: memory for current and past inputs
%

% written by H.Ichihara   March 4th 2012

n = length(A);
m = size(B,2);

if isempty(mem)
 mem = zeros((N+1)*m,1);
end

% trapezoidal approximation
E = eye(n)/2;
for ii = 1:1:N-1
 E = [E expm(A*(h/N)*ii)];
end
E = [E expm(A*h)/2];
BB = kron(eye(N+1),B);
Dk = (h/N)*E*BB*mem;

% input
input = Ft*(xk+expm(-A*h)*Dk);

% memory
mem = [input;mem(1:(N*m))];

end
% EOF