# Maximum volume inscribed ellipsoid in a polyhedron

    % 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 the ellipsoid E of maximum volume that lies inside of
    % a polyhedra C described by a set of linear inequalities.
    %
    % C = { x | a_i^T x <= b_i, i = 1,...,m } (polyhedra)
    % E = { Bu + d | || u || <= 1 } (ellipsoid)
    %
    % This problem can be formulated as a log det maximization
    % which can then be computed using the det_rootn function, ie,
    %     maximize     log det B
    %     subject to   || B a_i || + a_i^T d <= b,  for i = 1,...,m

    % problem data
    n = 2;
    px = [0 .5 2 3 1];
    py = [0 1 1.5 .5 -.5];
    m = size(px,2);
    pxint = sum(px)/m; pyint = sum(py)/m;
    px = [px px(1)];
    py = [py py(1)];

    % generate A,b
    A = zeros(m,n); b = zeros(m,1);
    for i=1:m
      A(i,:) = null([px(i+1)-px(i) py(i+1)-py(i)])';
      b(i) = A(i,:)*.5*[px(i+1)+px(i); py(i+1)+py(i)];
      if A(i,:)*[pxint; pyint]-b(i)>0
        A(i,:) = -A(i,:);
        b(i) = -b(i);
      end
    end

    % formulate and solve the problem
    cvx_begin
        variable B(n,n) symmetric
        variable d(n)
        maximize( det_rootn( B ) )
        subject to
           for i = 1:m
               norm( B*A(i,:)', 2 ) + A(i,:)*d <= b(i);
           end
    cvx_end

    % make the plots
    noangles = 200;
    angles   = linspace( 0, 2 * pi, noangles );
    ellipse_inner  = B * [ cos(angles) ; sin(angles) ] + d * ones( 1, noangles );
    ellipse_outer  = 2*B * [ cos(angles) ; sin(angles) ] + d * ones( 1, noangles );

    clf
    plot(px,py)
    hold on
    plot( ellipse_inner(1,:), ellipse_inner(2,:), 'r--' );
    plot( ellipse_outer(1,:), ellipse_outer(2,:), 'r--' );
    axis square
    axis off
    hold off

    Calling SeDuMi: 30 variables (2 free), 21 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
    Split 2 free variables
    eqs m = 21, order n = 21, dim = 40, blocks = 8
    nnz(A) = 65 + 0, nnz(ADA) = 301, nnz(L) = 181
     it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
      0 :            1.09E+000 0.000
      1 : -1.14E+000 2.86E-001 0.000 0.2627 0.9000 0.9000   2.64  1  1  8.2E-001
      2 : -7.37E-001 7.53E-002 0.000 0.2630 0.9000 0.9000   1.53  1  1  1.8E-001
      3 : -9.44E-001 5.13E-003 0.000 0.0681 0.9900 0.9900   0.89  1  1  1.2E-002
      4 : -9.52E-001 1.90E-004 0.000 0.0371 0.9900 0.9900   1.01  1  1  4.4E-004
      5 : -9.52E-001 4.50E-006 0.151 0.0237 0.9900 0.9900   1.00  1  1  1.1E-005
      6 : -9.52E-001 3.41E-007 0.273 0.0757 0.9900 0.9166   1.00  1  1  8.9E-007
      7 : -9.52E-001 1.74E-008 0.384 0.0510 0.9900 0.9906   1.00  1  1  5.1E-008
      8 : -9.52E-001 4.33E-009 0.000 0.2494 0.9028 0.9000   1.00  2  2  1.2E-008

    iter seconds digits       c*x               b*y
      8      0.1   8.3 -9.5230751330e-001 -9.5230751803e-001
    |Ax-b| =  3.3e-008, [Ay-c]_+ =  1.0E-009, |x|= 5.7e+000, |y|= 1.8e+000

    Detailed timing (sec)
       Pre          IPM          Post
    0.000E+000    8.012E-002    0.000E+000
    Max-norms: ||b||=2.474874e+000, ||c|| = 1,
    Cholesky |add|=0, |skip| = 0, ||L.L|| = 759.486.
    ------------------------------------------------------------------------
    Status: Solved
    Optimal value (cvx_optval): +0.952308