%%%%%%%%%%% curveplot %%%%%%%%%%%%%%%%%
%
%track trace constraint on feasible block of Levent-type problems
%as alpha decreases to 0

n = 10;
tn = n*(n+1)/2;     %triangular number 
In = eye(n);

%%%%%%%%%%%%%%%%%%%%%for ssMat/ssvec
n2=n^2;
svind = find(triu(ones(n),1)); %linear indices of the strictly upper triangle
sMind = zeros(n2,1); %matrix indices with respect to linear indices of the diagonal and the strictly upper triangle
sMind(svind) = 1:tn-n; %strictly upper triangle
sMind(1:n+1:n2) = tn-n+1:tn;%the diagonal
%calculate the linear indices of the strictly lower triangle
indM = zeros(n); %matrix indices
indM(:) = 1:n2;
indM = indM';
lsvind = indM(svind); %strictly lower triangular 
sMind(lsvind) = 1:tn-n; %strictly lower triangular


sdvec = 1:9;
%alphavec = 10.^(-14:2);
alphavec = 0;
stor = zeros(length(sdvec),length(alphavec));
for ii=1:length(sdvec)
    %construct problem
    [A,b,Xorig] = MakeProblemLevent(n,1,svind,sdvec(ii),In);
    bI = A*ssvec(In,svind);
    m = size(A,1);
    XO = Xorig;
    for jj=1:length(alphavec)
        
        data.A=A;
        data.b=b+alphavec(jj)*b;
        data.m=m;
        data.n=n;
        data.XO=XO;
        params.quiet=true;
        params.maxiter=63;
        %params.maxlastiter=-1;          %set to negative if no extra iters desired
        params.maxlastiter=10;          %set to negative if no extra iters desired
        params.svind=svind;
        params.sMind=sMind;
        params.tolalpha = 10^(-14);        %tolerance for alpha
        %params.tolResNorm = 1e-13;      %tolerance for residuals
        params.tolResNorm = 10^(-14);      %tolerance for residuals
        
        [output,psdflag] = IPmaxdetGN(data,params);
        alpha=output.alpha;
        X = output.X;
        
        stor(ii,jj) = trace(X(1:(n-sdvec(ii)),1:(n-sdvec(ii))));
    end
end


% figure(1)
% clf
% hold on
% for ii=1:length(sdvec)
%     plot(log(alphavec(1:end-2)),stor(ii,1:end-2),'.','MarkerSize',25,'Color','k');
% end
% title('Trace of Upper Left Block','Interpreter','latex','FontSize',17)
% xlabel('Singularity Degree','Interpreter','latex','FontSize',17)
% ylabel('$\trace(Y)$','Interpreter','latex','FontSize',17)
% hold off


figure(1)
clf
plot(sdvec,stor,'.','MarkerSize',25,'Color','k');
title('Trace of Upper Left Block of $\bar{X}$','Interpreter','latex','FontSize',17)
xlabel('Singularity Degree','Interpreter','latex','FontSize',17)
ylabel('$\langle I, \bar{Y} \rangle$','Interpreter','latex','FontSize',17)




