function W = gram2(A,B,t);
% Wc=gram2(A,B,t)
% Purpose
% Compute the finite controllability gramian, Eq. (1.39) lecture notes,
% i.e.
% Wc= int_0^t (exp(A*tau)*B*B'*exp(A'*tau)*dtau 
% 

[n,r]=size(B);
M=[-A,B*B';zeros(n,n),A'];
E=expm(M*t);
E22=E(n+1:2*n,n+1:2*n); E12=E(1:n,n+1:2*n);
W=E22'*E12;