% function mse = l_mse(y,u,a,n)
% Find the best theta for the Laguerre problem and compute
% the mse. 
%    y is the output of the true system
%    u is the input of the true system 
%    a is laguerre pole
%    n is the order of the laguerre system

% Create state transition matrix and input matrix
if n == 1,
  A = a;
 else
  A = [a , 0 ; (1-a^2) ,a];
  if n > 2,
   for i = 3:n,
    A = [A zeros(i-1,1) ; (-a)*A(i-1,1:i-2) (1-a^2) a];
   end
  end
 end
 B = zeros(n,1);
 for i = 1:n,
  B(i) = (-a)^(i-1);
 end
 B = B * sqrt(1-a^2);
 
 C=eye(n);

%Compute teta coefficients

% Compute the laguerre filter impulse responses
 L = dlsim(A,B,C,zeros(n,1),u);

 % Compute the optimal laguerre coefficients
 theta = inv(L'*L)*L'*y;

 % Compute the minimum MSE
 y_hat = dlsim(A,B,theta',y(1),u);
 s = size(y);
 mse = norm(y - y_hat)^2/s(1,1);
end
