% Section 8.6.1, 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 a function f(x) = a'*x - b that classifies the points
% {x_1,...,x_N} and {y_1,...,y_M} with maximal 'gap'. a and b can be
% obtained by solving the following problem:
%           maximize    t
%               s.t.    a'*x_i - b >=  t     for i = 1,...,N
%                       a'*y_i - b <= -t     for i = 1,...,M
%                       ||a||_2 <= 1

% 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
cvx_begin
    variables a(n) b(1) t(1)
    maximize (t)
    X'*a - b >= t;
    Y'*a - b <= -t;
    norm(a) <= 1;
cvx_end

% Displaying results
linewidth = 0.5;  % for the squares and circles
t_min = min([X(1,:),Y(1,:)]);
t_max = max([X(1,:),Y(1,:)]);
tt = linspace(t_min-1,t_max+1,100);
p = -a(1)*tt/a(2) + b/a(2);
p1 = -a(1)*tt/a(2) + (b+t)/a(2);
p2 = -a(1)*tt/a(2) + (b-t)/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(tt,p, '-r', tt,p1, '--r', tt,p2, '--r');
axis equal
title('Robust linear discrimination problem');
% print -deps linsep.eps
 
Calling sedumi: 20 variables, 5 equality constraints
   For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 5, order n = 20, dim = 21, blocks = 2
nnz(A) = 68 + 0, nnz(ADA) = 21, nnz(L) = 13
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            4.69E+01 0.000
  1 :  -3.49E-01 1.71E+01 0.000 0.3637 0.9000 0.9000   2.59  1  1  9.9E+00
  2 :   7.65E-03 5.42E+00 0.000 0.3177 0.9000 0.9000   2.36  1  1  2.4E+00
  3 :   2.62E-01 1.27E+00 0.000 0.2336 0.9000 0.9000   0.23  1  1  1.0E+00
  4 :   4.73E-01 3.00E-01 0.000 0.2372 0.9000 0.9000   0.58  1  1  2.7E-01
  5 :   5.05E-01 2.91E-02 0.000 0.0969 0.9900 0.9900   0.98  1  1  2.7E-02
  6 :   5.11E-01 1.39E-05 0.000 0.0005 0.9999 0.9999   1.00  1  1  1.5E-05
  7 :   5.11E-01 1.04E-06 0.290 0.0748 0.9900 0.9900   1.00  1  1  1.1E-06
  8 :   5.11E-01 1.96E-07 0.000 0.1874 0.9000 0.9063   1.00  1  1  2.2E-07
  9 :   5.11E-01 2.87E-09 0.000 0.0147 0.9901 0.9900   1.00  1  1  3.3E-09

iter seconds digits       c*x               b*y
  9      0.0   8.6  5.1122989899e-01  5.1122989774e-01
|Ax-b| =   1.6e-09, [Ay-c]_+ =   3.3E-11, |x|=  1.1e+00, |y|=  1.2e+00

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