function [input,mem] = dd_strictproper(A,B,Ft,h,N,xk,mem,del,tau)
% dd_strictproper.m
%
%    state predictive controller by strict proper approximation 
%
%       [input,mem] = dd_strictproper(A,B,Ft,h,N,xk,mem,del,tau)
%
%    input: A, B: system matrices    
%           Ft: state feedback gain
%           h: delay
%           N: division number of approximation
%           xk: state
%           mem: memory for current, past inputs and filter
%           del: hardware sampling time
%           tau: constant value of approximation
%    output: input: current input           
%             mem: memory for current, past inputs and filter
%

% written by H.Ichihara   March 4th 2012

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

if isempty(mem)
 mem.mem = zeros((N+1)*m,1);
 mem.Duk = zeros(n,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.mem;

% Guk
uk = mem.mem(1:m);
ukmN = mem.mem((N*m+1):((N+1)*m));
Guk = tau*(B*uk-expm(A*h)*B*ukmN)+(eye(n)+tau*A)*Dk;

% Duk
Duk =  exp(-del/tau)*mem.Duk+(1-exp(-del/tau))*Guk;

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

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

end
% EOF