# Quadratic discrimination (separating ellipsoid)

    % Section 8.6.2, Boyd & Vandenberghe "Convex Optimization"
    % Original by Lieven Vandenberghe
    % Adapted for CVX by Joelle Skaf - 10/16/05
    % (a figure is generated)
    %
    % The goal is to find an ellipsoid that contains all the points
    % x_1,...,x_N but none of the points y_1,...,y_M. The equation of the
    % ellipsoidal surface is: z'*P*z + q'*z + r =0
    % P, q and r can be obtained by solving the SDP feasibility problem:
    %           minimize    0
    %               s.t.    x_i'*P*x_i + q'*x_i + r >=  1   for i = 1,...,N
    %                       y_i'*P*y_i + q'*y_i + r <= -1   for i = 1,...,M
    %                       P <= -I

    % data generation
    n = 2;
    rand('state',0);  randn('state',0);
    N=50;
    X = randn(2,N);  X = X*diag(0.99*rand(1,N)./sqrt(sum(X.^2)));
    Y = randn(2,N);  Y = Y*diag((1.02+rand(1,N))./sqrt(sum(Y.^2)));
    T = [1 -1; 2 1];  X = T*X;  Y = T*Y;

    % Solution via CVX
    fprintf(1,'Find the optimal ellipsoid that seperates the 2 classes...');

    cvx_begin sdp
        variable P(n,n) symmetric
        variables q(n) r(1)
        P <= -eye(n);
        sum((X'*P).*X',2) + X'*q + r >= +1;
        sum((Y'*P).*Y',2) + Y'*q + r <= -1;
    cvx_end

    fprintf(1,'Done! \n');

    % Displaying results
    r = -r; P = -P; q = -q;
    c = 0.25*q'*inv(P)*q - r;
    xc = -0.5*inv(P)*q;
    nopts = 1000;
    angles = linspace(0,2*pi,nopts);
    ell = inv(sqrtm(P/c))*[cos(angles); sin(angles)] + repmat(xc,1,nopts);
    graph=plot(X(1,:),X(2,:),'o', Y(1,:), Y(2,:),'o', ell(1,:), ell(2,:),'-');
    set(graph(2),'MarkerFaceColor',[0 0.5 0]);
    set(gca,'XTick',[]); set(gca,'YTick',[]);
    title('Quadratic discrimination');
    % print -deps ellips.eps

    Find the optimal ellipsoid that seperates the 2 classes...
    Calling SeDuMi: 108 variables (5 free), 102 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 5 free variables
    eqs m = 102, order n = 113, dim = 115, blocks = 2
    nnz(A) = 1206 + 0, nnz(ADA) = 10404, nnz(L) = 5253
     it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
      0 :            2.17E-001 0.000
      1 :  5.73E+001 1.04E-001 0.000 0.4809 0.9000 0.9000  -4.15  1  1  2.4E+001
      2 :  5.84E+001 3.22E-002 0.000 0.3084 0.9000 0.9000  -2.53  1  1  6.3E+000
      3 :  3.83E+001 7.39E-003 0.000 0.2296 0.9000 0.9000  -0.07  1  1  2.3E+000
      4 :  2.16E+000 1.67E-004 0.000 0.0227 0.9900 0.9900   0.65  1  1  6.2E-002
      5 :  9.15E-005 5.20E-009 0.000 0.0000 1.0000 1.0000   0.99  1  1  1.9E-006
      6 :  2.42E-008 2.49E-012 0.000 0.0005 0.9999 0.9997   1.00  1  1  7.6E-010

    iter seconds digits       c*x               b*y
      6      0.1   Inf  0.0000000000e+000  2.4161599043e-008
    |Ax-b| =  4.2e-009, [Ay-c]_+ =  7.4E-011, |x|= 4.0e+002, |y|= 1.1e-008

    Detailed timing (sec)
       Pre          IPM          Post
    2.003E-002    5.007E-002    0.000E+000
    Max-norms: ||b||=1, ||c|| = 0,
    Cholesky |add|=0, |skip| = 0, ||L.L|| = 18.1252.
    ------------------------------------------------------------------------
    Status: Solved
    Optimal value (cvx_optval): +0
    Done!