# Figure 8.8: Simplest linear discrimination

    % Boyd & Vandenberghe "Convex Optimization"
    % Joelle Skaf - 10/16/05
    % (a figure is generated)
    %
    % The goal is to find a function f(x) = a'*x - b that classifies the points
    % {x_1,...,x_N} and {y_1,...,y_M}. a and b can be obtained by solving a
    % feasibility problem:
    %           minimize    0
    %               s.t.    a'*x_i - b >=  1     for i = 1,...,N
    %                       a'*y_i - b <= -1     for i = 1,...,M

    % data generation
    n = 2;
    randn('state',3);
    N = 10; M = 6;
    Y = [1.5+1*randn(1,M); 2*randn(1,M)];
    X = [-1.5+1*randn(1,N); 2*randn(1,N)];
    T = [-1 1; 1 1];
    Y = T*Y;  X = T*X;

    % Solution via CVX
    fprintf('Finding a separating hyperplane...');

    cvx_begin
        variables a(n) b(1)
        X'*a - b >= 1;
        Y'*a - b <= -1;
    cvx_end

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

    % Displaying results
    linewidth = 0.5;  % for the squares and circles
    t_min = min([X(1,:),Y(1,:)]);
    t_max = max([X(1,:),Y(1,:)]);
    t = linspace(t_min-1,t_max+1,100);
    p = -a(1)*t/a(2) + b/a(2);

    graph = plot(X(1,:),X(2,:), 'o', Y(1,:), Y(2,:), 'o');
    set(graph(1),'LineWidth',linewidth);
    set(graph(2),'LineWidth',linewidth);
    set(graph(2),'MarkerFaceColor',[0 0.5 0]);
    hold on;
    plot(t,p, '-r');
    axis equal
    title('Simple classification using an affine function');
    % print -deps lin-discr.eps

    Finding a separating hyperplane...
    Calling SeDuMi: 19 variables (3 free), 16 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 3 free variables
    eqs m = 16, order n = 23, dim = 23, blocks = 1
    nnz(A) = 112 + 0, nnz(ADA) = 256, nnz(L) = 136
     it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
      0 :            1.07E+000 0.000
      1 :  5.00E+000 3.75E-001 0.000 0.3524 0.9000 0.9000  -2.43  1  1  3.4E+000
      2 :  3.87E+000 8.61E-002 0.000 0.2294 0.9000 0.9000  -0.26  1  1  1.3E+000
      3 :  2.22E-001 2.78E-003 0.000 0.0323 0.9900 0.9900   0.62  1  1  5.0E-002
      4 :  7.52E-006 1.04E-007 0.000 0.0000 1.0000 1.0000   0.99  1  1
    iter seconds digits       c*x               b*y
      4      0.0   7.9  0.0000000000e+000 -3.2461697005e-019
    |Ax-b| =  4.1e-014, [Ay-c]_+ =  3.0E-019, |x|= 4.7e+001, |y|= 3.9e-019

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