Minimum volume ellipsoid covering union of ellipsoids

% Section 8.4.1, Boyd & Vandenberghe "Convex Optimization"
% Original version by Lieven Vandenberghe
% Updated for CVX by Almir Mutapcic - Jan 2006
% (a figure is generated)
%
% We find a smallest ellipsoid containing m ellipsoids
% { x'*A_i*x + 2*b_i'*x + c < 0 }, for i = 1,...,m
%
% Problem data:
% As = {A1, A2, ..., Am}:  cell array of m pos. def. matrices
% bs = {b1, b2, ..., bm}:  cell array of m 2-vectors
% cs = {c1, c2, ..., cm}:  cell array of m scalars

% ellipse data
As = {}; bs = {}; cs = {};
As{1} = [ 0.1355    0.1148;  0.1148    0.4398];
As{2} = [ 0.6064   -0.1022; -0.1022    0.7344];
As{3} = [ 0.7127   -0.0559; -0.0559    0.9253];
As{4} = [ 0.2706   -0.1379; -0.1379    0.2515];
As{5} = [ 0.4008   -0.1112; -0.1112    0.2107];
bs{1} = [ -0.2042  0.0264]';
bs{2} = [  0.8259 -2.1188]';
bs{3} = [ -0.0256  1.0591]';
bs{4} = [  0.1827 -0.3844]';
bs{5} = [  0.3823 -0.8253]';
cs{1} = 0.2351;
cs{2} = 5.8250;
cs{3} = 0.9968;
cs{4} = -0.2981;
cs{5} = 2.6735;

% dimensions
n = 2;
m = size(bs,2);    % m ellipsoids given

% construct and solve the problem as posed in the book
cvx_begin sdp
    variable Asqr(n,n) symmetric
    variable btilde(n)
    variable t(m)
    maximize( det_rootn( Asqr ) )
    subject to
        t >= 0;
        for i = 1:m
            [ -(Asqr - t(i)*As{i}), -(btilde - t(i)*bs{i}), zeros(n,n);
              -(btilde - t(i)*bs{i})', -(- 1 - t(i)*cs{i}), -btilde';
               zeros(n,n), -btilde, Asqr] >= 0;
        end
cvx_end

% convert to ellipsoid parametrization E = { x | || Ax + b || <= 1 }
A = sqrtm(Asqr);
b = A\btilde;

% plot ellipsoids using { x | || A_i x + b_i || <= alpha } parametrization
noangles = 200;
angles   = linspace( 0, 2 * pi, noangles );

clf
for i=1:m
  Ai = sqrtm(As{i}); bi = Ai\bs{i};
  alpha = bs{i}'*inv(As{i})*bs{i} - cs{i};
  ellipse  = Ai \ [ sqrt(alpha)*cos(angles)-bi(1) ; sqrt(alpha)*sin(angles)-bi(2) ];
  plot( ellipse(1,:), ellipse(2,:), 'b-' );
  hold on
end
ellipse  = A \ [ cos(angles) - b(1) ; sin(angles) - b(2) ];

plot( ellipse(1,:), ellipse(2,:), 'r--' );
axis square
axis off
hold off
 
Calling SeDuMi: 93 variables (0 free), 79 equality constraints
------------------------------------------------------------------------
SeDuMi 1.1 by AdvOL, 2005 and Jos F. Sturm, 1998, 2001-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 79, order n = 37, dim = 151, blocks = 8
nnz(A) = 161 + 0, nnz(ADA) = 2793, nnz(L) = 1580
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            1.87E+000 0.000
  1 : -6.85E-001 5.30E-001 0.000 0.2830 0.9000 0.9000   1.03  1  1  3.7E+000
  2 : -3.45E-001 1.70E-001 0.000 0.3214 0.9000 0.9000   1.82  1  1  9.0E-001
  3 : -1.20E-001 4.40E-002 0.000 0.2583 0.9000 0.9000   1.89  1  1  1.9E-001
  4 : -8.02E-002 8.07E-003 0.000 0.1833 0.9000 0.9000   1.35  1  1  3.3E-002
  5 : -7.96E-002 1.86E-003 0.000 0.2304 0.9000 0.9000   0.95  1  1  7.8E-003
  6 : -7.87E-002 5.35E-005 0.000 0.0288 0.9902 0.9900   0.99  1  1  3.2E-004
  7 : -7.87E-002 1.39E-006 0.000 0.0261 0.9900 0.9900   1.00  1  1  7.3E-006
  8 : -7.87E-002 1.21E-007 0.499 0.0866 0.9900 0.9900   1.00  1  1  6.5E-007
  9 : -7.87E-002 2.42E-008 0.000 0.2005 0.9000 0.8355   1.00  1  1  1.3E-007
 10 : -7.87E-002 1.57E-009 0.414 0.0647 0.9900 0.9901   1.00  2  2  7.7E-009

iter seconds digits       c*x               b*y
 10      0.1   8.0 -7.8681465304e-002 -7.8681466096e-002
|Ax-b| =  5.1e-009, [Ay-c]_+ =  6.8E-010, |x|= 6.3e+000, |y|= 2.2e+000

Detailed timing (sec)
   Pre          IPM          Post
4.006E-002    7.010E-002    1.001E-002    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 2030.47.
------------------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.0786815