Separating ellipsoids in 2D

% 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: 17 variables, 9 equality constraints
   For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
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 = 9, order n = 13, dim = 20, blocks = 6
nnz(A) = 19 + 0, nnz(ADA) = 55, nnz(L) = 32
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            4.34E+00 0.000
  1 :  -1.99E-01 1.38E+00 0.000 0.3184 0.9000 0.9000   2.23  1  1  1.6E+00
  2 :  -1.09E+00 1.26E-01 0.000 0.0914 0.9900 0.9900   1.22  1  1  1.5E-01
  3 :  -1.19E+00 3.13E-03 0.000 0.0247 0.9900 0.9900   1.11  1  1  3.0E-03
  4 :  -1.19E+00 8.33E-06 0.249 0.0027 0.9990 0.9990   1.00  1  1  8.0E-06
  5 :  -1.19E+00 5.75E-07 0.000 0.0691 0.9900 0.9900   1.00  1  1  5.6E-07
  6 :  -1.19E+00 8.33E-08 0.000 0.1448 0.9105 0.9000   1.00  1  1  8.4E-08
  7 :  -1.19E+00 3.15E-09 0.375 0.0378 0.9902 0.9900   1.00  1  1  3.2E-09

iter seconds digits       c*x               b*y
  7      0.0   8.9 -1.1924413509e+00 -1.1924413525e+00
|Ax-b| =   9.6e-10, [Ay-c]_+ =   3.2E-09, |x|=  2.9e+00, |y|=  3.0e+00

Detailed timing (sec)
   Pre          IPM          Post
1.000E-02    3.000E-02    0.000E+00    
Max-norms: ||b||=1, ||c|| = 3.750000e+00,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 14.478.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +1.19244