% 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: 23 variables, 10 equality constraints
------------------------------------------------------------

 num. of constraints = 10
 dim. of sdp    var  =  4,   num. of sdp  blk  =  2
 dim. of socp   var  =  9,   num. of socp blk  =  3
 dim. of linear var  =  6
 dim. of free   var  =  2 *** convert ublk to lblk
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|6.7e+00|1.7e+01|1.4e+03| 3.464102e+00  0.000000e+00| 0:0:00| chol  1  1 
 1|0.836|0.635|1.1e+00|6.2e+00|2.7e+02| 6.825272e+00 -2.359565e+01| 0:0:00| chol  1  1 
 2|1.000|0.954|5.7e-07|3.0e-01|3.1e+01| 4.435320e+00 -2.473466e+01| 0:0:00| chol  1  1 
 3|0.977|0.862|3.0e-06|4.2e-02|4.2e+00| 2.926933e+00 -1.120011e+00| 0:0:00| chol  1  1 
 4|0.768|0.466|6.2e-07|2.2e-02|2.4e+00| 1.754830e+00 -5.541660e-01| 0:0:00| chol  1  1 
 5|1.000|0.587|5.4e-08|9.2e-03|9.1e-01| 1.347425e+00  4.799600e-01| 0:0:00| chol  1  1 
 6|0.997|0.949|2.1e-08|4.7e-04|4.3e-02| 1.204087e+00  1.163167e+00| 0:0:00| chol  1  1 
 7|0.975|0.984|9.7e-10|7.5e-06|7.8e-04| 1.192715e+00  1.191959e+00| 0:0:00| chol  1  1 
 8|0.959|0.979|9.6e-11|1.7e-05|5.6e-05| 1.192453e+00  1.192431e+00| 0:0:00| chol  1  1 
 9|0.973|0.969|4.1e-12|1.2e-06|3.3e-06| 1.192442e+00  1.192441e+00| 0:0:00| chol  1  1 
10|1.000|0.974|1.8e-13|7.2e-08|2.4e-07| 1.192441e+00  1.192441e+00| 0:0:00| chol  1  1 
11|1.000|0.987|1.8e-11|5.1e-09|1.4e-08| 1.192441e+00  1.192441e+00| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 11
 primal objective value =  1.19244136e+00
 dual   objective value =  1.19244136e+00
 gap := trace(XZ)       = 1.37e-08
 relative gap           = 4.04e-09
 actual relative gap    = 9.86e-10
 rel. primal infeas     = 1.77e-11
 rel. dual   infeas     = 5.14e-09
 norm(X), norm(y), norm(Z) = 4.1e+00, 2.2e+00, 3.2e+00
 norm(A), norm(b), norm(C) = 7.0e+00, 5.7e+00, 2.0e+00
 Total CPU time (secs)  = 0.31  
 CPU time per iteration = 0.03  
 termination code       =  0
 DIMACS: 2.5e-11  0.0e+00  5.1e-09  0.0e+00  9.9e-10  4.0e-09
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +1.19244