% Compute identification/verification error

function Totalresult = Inputiderror_acc(YUperpM,Yopt,ymeasi,ymeasv,uv,nvec,r,Nid,Nv,m,p,lamb)
% Notations:
% YUperpM - Vectorized YUperp for each lambda
% Yopt    - Vectorized Y optimal for each lambda
% ymeasi  - Measurement matrix for identification  
% ymeasv  - Measurement matrix for verification.
% uv      - Input matrix for id and valid
% nvec    - vector of ranks for different lambdas 
% r       - as in H_r
% Nid     - # ident measurements
% Nv      - # valid measurements
% m       - number of inputs
% p       - number of outputs
% lamb    - set of lambdas
                                        
tic         
l1 = (r+1)*p; % #rows in YUperp
l2 = size(YUperpM,1)/l1; % #cols in YUperp
alp1 = size(lamb,1); % # lambdas

errorid = zeros(alp1,1);
errorv = zeros(alp1,1);
normfro = zeros(alp1,1);
opterror = zeros(alp1,1);
  
for i = 1:alp1
  YUperp = reshape(YUperpM(:,i),l1,l2);
  yopt = reshape(Yopt(:,i),p,Nid);
  n = nvec(i,1); % rank of YUperp
  
  [errorid(i,1),errorv(i,1)] = iderror_acc(YUperp,ymeasi,ymeasv,uv,n,r,Nid,Nv,m,p);
  
  normfro(i,1) = sum(svd(YUperp));  % nuclear norm of YUperp
  opterror(i,1) = norm(yopt-ymeasi,'fro');  % error between optimal Y and measurement matrix bx
end

Totalresult = [1./lamb nvec opterror errorid errorv normfro];
toc
            
            


