function y = cond_update_y(M,My,n,X,y,Q,beta,tol)

% parameters
tol = min(1e-6,tol);
sigma = 1e-4;
k = 10;

% initialization
v = X + Q/beta;
fval = norm(My - v,'fro')^2/2;
grad = M(reshape(My - v,n^2,1),2);
step_init = 1;

iter = 1;
f = -inf*ones(k,1);
f(1) = fval;
fmax = max(f);

while 1
  
  dtest = max(y - grad,0) - y;
  if norm(dtest)/max(1,abs(fval)) < tol
    break
  end
  
  d = max(y - step_init*grad,0) - y;
  Delta = grad'*d;
  
  stepsize = 1;
  ynew = y + stepsize*d;
  My = reshape(M(ynew,1),n,n);
  fval = norm(My - v,'fro')^2/2;
  
  while fval > fmax + sigma*stepsize*Delta && stepsize > 1e-10
    
    stepsize = stepsize/2;
    ynew = y + stepsize*d;
    My = reshape(M(ynew,1),n,n);
    fval = norm(My - v,'fro')^2/2;
    
  end
  
  if stepsize < 1e-10
    break
  end
  gradnew = M(reshape(My - v,n^2,1),2);
  
  dy = ynew - y;
  dg = gradnew - grad;
  step_init = min(max(norm(dy)^2/(dy'*dg),1e-8),1);
  
  y = ynew;
  grad = gradnew;
  f(mod(iter,k) + 1) = fval;
  fmax = max(f);
  
%   fprintf(' iter, fval = %g, %g\n', iter, fval)
  
  iter = iter + 1;
  
end