function z = discreptsvd(lam,S,Ghat,Va,Vb,A,B,G,target)

% Function called by fzero to determine TSVD regularization parameter
% Compare the norm-squared of the error using parameter lam
% to the target norm-squared error.
% DPO 04/2012

% Compute the approximate solution using parameter lam.

p = floor(lam);
[n,m] = size(S);

  s = reshape(S,n^2,1);
  [ss,prm] = sort(s);
  prm = flipud(prm);
  ss = flipud(ss);
  ls = length(s);
  iprm = zeros(ls,1);
  iprm(prm) = [1:ls]';


  ssnew = [ss(1:p); zeros(length(ss)-p,1)];
  Snew = reshape(ssnew(iprm),n,n);
  Fhat = Ghat ./ S;
  Fnew = Fhat .* (Snew>0);
  F = Vb*Fnew*Va';

% Compute the residual (difference between predicted blurred
% image and observed blurred image).  Compare to target.

  z = norm(B*F*A'-G,'fro')^2-target;


