% Least-norm squared solution to Ax = b
% with dual computations
clear all
clc
m = 4;
n = 5;
A = randn(m,n);
b = randn(m,1);
% Primal with implicit dual solution
cvx_begin quiet
variable x(n);
dual variable y;
minimize(x'*x);
% minimize(quad_form(x, eye(n))); Alternative form of the objective
y : b - A*x == 0; % If we use Ax - b = 0 the dual variable will be the
% negative of what is expected it to be. Notice that this convention is
% consistent with a Lagrangian defined with minus signs between the
% terms, which is opposite to the convention adopted in the Boyd's book.
% See e-mail from Michal Grant dated Oct. 8, 2013.
cvx_end
% Display results
disp( [ 'p* = x^T*x = ', sprintf( '%7.4f ', x'*x ), ] );
% Dual function value for the dual optimal point:
disp( [ 'd* = -(1/4)*y^T*A*A^T*y - b^T*y = ', sprintf( '%7.4f ', -(1/4)*y'*(A*A')*y - b'*y ), ] );
% Dual problem
cvx_begin quiet
variable z(m);
maximize (-(1/4)*z'*(A*A')*z - b'*z);
% subject to
% z >= 0; % Enable this if the equality becomes an inequality in A*x=b
cvx_end
disp( [ 'd* = sup{-(1/4)*z^T*A*A^T*z - b^T*z} = ', sprintf( '%7.4f ', cvx_optval ), ] );
% Cf. p. 227, strong duality applies