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 SDPT3: 17 variables, 9 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints =  9
 dim. of sdp    var  =  4,   num. of sdp  blk  =  2
 dim. of socp   var  =  9,   num. of socp blk  =  3
 dim. of linear var  =  2
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      mean(obj)   cputime
-------------------------------------------------------------------
 0|0.000|0.000|1.5e+00|1.9e+00|7.7e+01| 8.000000e+00| 0:0:00| chol  1  1 
 1|1.000|0.896|5.8e-07|2.4e-01|1.1e+01| 1.509951e+00| 0:0:00| chol  1  1 
 2|1.000|1.000|5.7e-07|5.3e-03|1.4e+00|-1.484236e+00| 0:0:00| chol  1  1 
 3|1.000|0.935|6.8e-07|8.4e-04|2.5e-01|-1.151154e+00| 0:0:00| chol  1  1 
 4|0.972|0.985|1.3e-07|6.4e-05|5.8e-03|-1.191287e+00| 0:0:00| chol  1  1 
 5|0.858|0.996|5.3e-08|5.5e-06|6.9e-04|-1.192349e+00| 0:0:00| chol  1  1 
 6|0.766|0.878|6.8e-08|1.1e-06|2.4e-04|-1.192440e+00| 0:0:00| chol  1  1 
 7|1.000|0.997|8.4e-09|1.7e-08|3.5e-05|-1.192446e+00| 0:0:00| chol  1  1 
 8|0.962|0.935|5.2e-10|2.8e-09|2.0e-06|-1.192442e+00| 0:0:00| chol  1  1 
 9|1.000|0.988|1.1e-10|1.4e-10|1.1e-07|-1.192441e+00| 0:0:00| chol  1  1 
10|0.995|0.994|4.4e-12|1.7e-11|2.2e-09|-1.192441e+00| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 10
 primal objective value = -1.19244136e+00
 dual   objective value = -1.19244136e+00
 gap := trace(XZ)       = 2.21e-09
 relative gap           = 6.54e-10
 actual relative gap    = 5.89e-10
 rel. primal infeas     = 4.40e-12
 rel. dual   infeas     = 1.75e-11
 norm(X), norm(y), norm(Z) = 2.9e+00, 2.4e+00, 3.9e+00
 norm(A), norm(b), norm(C) = 6.2e+00, 2.0e+00, 5.7e+00
 Total CPU time (secs)  = 0.3  
 CPU time per iteration = 0.0  
 termination code       =  0
 DIMACS: 4.4e-12  0.0e+00  2.5e-11  0.0e+00  5.9e-10  6.5e-10
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +1.19244