clear; clc; close all;

% problem data 
A  = [1 1 1 ; 1 1 2 ; 1 2 2];      % counter-example matrix
A1 = A(:,1);  A2 = A(:,2);  A3 = A(:,3);
b  = zeros(3,1);                   % RHS
rho = 1;                           % penalty parameter  (any rho > 0)

% Pre-compute Gram constants (each subproblem is 1D)
c11 = A1'*A1;
c22 = A2'*A2;
c33 = A3'*A3;

% initial point (avoid the fixed half-space)
x = randn(3, 1);
x1 = x(1);
x2 = x(2);
x3 = x(3);
lambda = randn(3,1);               % dual variable  λ^0

% ADMM iterations
K    = 300;                        % number of iterations
hist = zeros(K,1);                 % ‖[x1;x2;x3]‖_2 history

for k = 1:K
    % x1 update
    d1 = A2*x2 + A3*x3 - b + lambda/rho;
    x1 = - (A1'*d1) / c11;
    
    % x2 update
    d2 = A1*x1 + A3*x3 - b + lambda/rho;
    x2 = - (A2'*d2) / c22;
    
    % x3 update
    d3 = A1*x1 + A2*x2 - b + lambda/rho;
    x3 = - (A3'*d3) / c33;
    
    % dual update
    r      = A1*x1 + A2*x2 + A3*x3 - b;   % primal residual
    
    if mod(k, 2) == 0 % Alternating stepsize
        lambda = lambda + rho*r;
    else
        lambda = lambda - rho*r;
    end % End if 
    
    hist(k) = norm([x1; x2; x3],2);       % track divergence
end

% visualize
figure
semilogy(1:K,hist,'LineWidth',1.2), grid on
xlabel('Iteration'), ylabel('$\log \|x\|_2$', 'Interpreter', 'latex', 'FontName', 'Avenir Next')
title('3-block ADMM converges with negative stepsize', 'FontWeight', 'normal');
set(gca, 'FontSize', 20);

exportgraphics(gcf, 'ADMM_negstep.pdf', 'ContentType', 'vector');