% Joelle Skaf - 11/06/05
% (a figure is generated)
%
% Finds a separating hyperplane between 2 ellipsoids {x| ||Ax+b||^2<=1} and
% {y | ||Cy + d||^2 <=1} by solving the following problem and using its
% dual variables:
%               minimize    ||w||
%                   s.t.    ||Ax + b||^2 <= 1       : lambda
%                           ||Cy + d||^2 <= 1       : mu
%                           x - y == w              : z
% the vector z will define a separating hyperplane because z'*(x-y)>0

% input data
n = 2;
A = eye(n);
b = zeros(n,1);
C = [2 1; -.5 1];
d = [-3; -3];

% solving for the minimum distance between the 2 ellipsoids and finding
% the dual variables
cvx_begin
    variables x(n) y(n) w(n)
    dual variables lam muu z
    minimize ( norm(w,2) )
    subject to
    lam:    square_pos( norm (A*x + b) ) <= 1;
    muu:    square_pos( norm (C*y + d) ) <= 1;
    z:      x - y == w;
cvx_end


t = (x + y)/2;
p=z;
p(1) = z(2); p(2) = -z(1);
c = linspace(-2,2,100);
q = repmat(t,1,length(c)) +p*c;

% figure
nopts = 1000;
angles = linspace(0,2*pi,nopts);
[u,v] = meshgrid([-2:0.01:4]);
z1 = (A(1,1)*u + A(1,2)*v + b(1)).^2 + (A(2,1)*u + A(2,2)*v + b(2)).^2;
z2 = (C(1,1)*u + C(1,2)*v + d(1)).^2 + (C(2,1)*u + C(2,2)*v + d(2)).^2;
contour(u,v,z1,[1 1]);
hold on;
contour(u,v,z2,[1 1]);
axis square
plot(x(1),x(2),'r+');
plot(y(1),y(2),'b+');
line([x(1) y(1)],[x(2) y(2)]);
plot(q(1,:),q(2,:),'k');
 
Calling sedumi: 21 variables, 8 equality constraints
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 8, order n = 17, dim = 24, blocks = 6
nnz(A) = 24 + 0, nnz(ADA) = 30, nnz(L) = 19
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            4.00E+00 0.000
  1 :  -5.32E-02 1.36E+00 0.000 0.3414 0.9000 0.9000   2.28  1  1  2.5E+00
  2 :   1.06E+00 3.17E-01 0.000 0.2324 0.9000 0.9000   1.18  1  1  5.3E-01
  3 :   1.19E+00 6.24E-02 0.000 0.1968 0.9000 0.9000   1.23  1  1  9.4E-02
  4 :   1.19E+00 4.54E-04 0.000 0.0073 0.9990 0.9990   1.02  1  1  6.7E-04
  5 :   1.19E+00 1.91E-05 0.000 0.0420 0.9900 0.9900   1.00  1  1  2.8E-05
  6 :   1.19E+00 5.96E-07 0.000 0.0313 0.9901 0.9900   1.00  1  1  8.1E-07
  7 :   1.19E+00 2.48E-08 0.466 0.0416 0.9900 0.9905   1.00  1  1  3.9E-08
  8 :   1.19E+00 4.88E-09 0.000 0.1967 0.9009 0.9000   1.00  2  2  7.3E-09

iter seconds digits       c*x               b*y
  8      0.1   8.3  1.1924413559e+00  1.1924413499e+00
|Ax-b| =   1.3e-08, [Ay-c]_+ =   0.0E+00, |x|=  3.9e+00, |y|=  1.7e+00

Detailed timing (sec)
   Pre          IPM          Post
1.000E-02    1.000E-01    2.000E-02    
Max-norms: ||b||=3, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.31165.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +1.19244