```% Section 8.2.1, Boyd & Vandenberghe "Convex Optimization"
% Joelle Skaf - 10/09/05
%
% Given two polyhedra C = {x | A1*x <= b1} and D = {x | A2*x <= b2}, the
% distance between them is the optimal value of the problem:
%           minimize    || x - y ||_2
%               s.t.    A1*x <= b1
%                       A2*y <= b2

% Input data
randn('state',0);
rand('state',0);

n  = 5;
m1 = 2*n;
m2 = 3*n;
A1 = randn(m1,n);
A2 = randn(m2,n);
b1 = rand(m1,1);
b2 = rand(m2,1) + A2*randn(n,1);

% Solution via CVX
cvx_begin
variables x(n) y(n)
minimize (norm(x - y))
A1*x <= b1;
A2*y <= b2;
cvx_end

% Displaying results
disp('------------------------------------------------------------------');
disp('The distance between the 2 polyhedra C and D is: ' );
disp(['dist(C,D) = ' num2str(cvx_optval)]);
```
```
Calling sedumi: 31 variables, 11 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 = 11, order n = 28, dim = 32, blocks = 2
nnz(A) = 136 + 0, nnz(ADA) = 121, nnz(L) = 66
it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
0 :            1.15E+01 0.000
1 :   3.14E-02 5.51E+00 0.000 0.4801 0.9000 0.9000   3.34  1  1  3.4E+00
2 :  -5.66E-01 1.54E+00 0.000 0.2799 0.9000 0.9000   1.86  1  1  1.2E+00
3 :  -4.73E-01 4.06E-01 0.000 0.2631 0.9000 0.9000   1.31  1  1  2.9E-01
4 :  -5.06E-01 3.42E-02 0.000 0.0843 0.9900 0.9900   1.09  1  1  2.3E-02
5 :  -5.07E-01 1.24E-02 0.000 0.3638 0.9000 0.9000   1.03  1  1  8.3E-03
6 :  -5.08E-01 3.13E-04 0.000 0.0252 0.9900 0.8509   1.01  1  1  2.9E-04
7 :  -5.09E-01 2.63E-06 0.286 0.0084 0.9990 0.9931   1.00  1  1  2.4E-06
8 :  -5.09E-01 6.39E-09 0.248 0.0024 0.9990 0.9990   1.00  1  1  9.2E-09

iter seconds digits       c*x               b*y
8      0.0   Inf -5.0856705170e-01 -5.0856704680e-01
|Ax-b| =   2.1e-09, [Ay-c]_+ =   9.2E-09, |x|=  1.8e+00, |y|=  1.7e+00

Detailed timing (sec)
Pre          IPM          Post
1.000E-02    4.000E-02    1.000E-02
Max-norms: ||b||=1, ||c|| = 2.829728e+00,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 48.6227.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.508567
------------------------------------------------------------------
The distance between the 2 polyhedra C and D is:
dist(C,D) = 0.50857
```