% Section 8.7.3, Boyd & Vandenberghe "Convex Optimization"
% Joelle Skaf - 10/24/05
%
% K fixed points x_1,...,x_K in R^2 are given and the goal is to place
% one additional point x such that the sum of the squares of the
% Euclidean distances to fixed points is minimized:
%           minimize    sum_{i=1}^K  ||x - x_i||^2
% The optimal point is the average of the given fixed points

% Data generation
n = 2;
K = 11;
randn('state',0);
P = randn(n,K);

% minimizing the sum of Euclidean distance
fprintf(1,'Minimizing the sum of the squares the distances to fixed points...');

cvx_begin
    variable x(2)
    minimize ( sum( square_pos( norms(x*ones(1,K) - P,2) ) ) )
cvx_end

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

% Displaying results
disp('------------------------------------------------------------------');
disp('The optimal point location is: ');
disp(x);
disp('The average location of the fixed points is');
disp(sum(P,2)/K);
disp('They are the same as expected!');
Minimizing the sum of the squares the distances to fixed points... 
Calling SDPT3: 88 variables, 46 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints = 46
 dim. of sdp    var  = 22,   num. of sdp  blk  = 11
 dim. of socp   var  = 33,   num. of socp blk  = 11
 dim. of linear var  = 22
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|1.0e+01|1.1e+01|4.6e+03| 1.100000e+02  0.000000e+00| 0:0:00| chol  1  1 
 1|0.854|0.860|1.5e+00|1.6e+00|9.5e+02| 8.596987e+01 -1.058415e+02| 0:0:00| chol  1  1 
 2|1.000|1.000|1.9e-06|1.0e-02|1.3e+02| 4.110764e+01 -8.490538e+01| 0:0:00| chol  1  1 
 3|0.978|0.982|9.4e-07|1.2e-03|2.4e+01|-7.230670e+00 -3.138282e+01| 0:0:00| chol  1  1 
 4|1.000|1.000|2.2e-07|1.0e-04|8.5e+00|-1.347951e+01 -2.199622e+01| 0:0:00| chol  1  1 
 5|0.912|0.910|3.5e-08|1.8e-05|1.1e+00|-1.633531e+01 -1.742632e+01| 0:0:00| chol  1  1 
 6|1.000|1.000|9.6e-09|1.0e-06|3.3e-01|-1.657765e+01 -1.691125e+01| 0:0:00| chol  1  1 
 7|0.956|0.956|3.9e-09|1.4e-07|1.7e-02|-1.667823e+01 -1.669482e+01| 0:0:00| chol  1  1 
 8|0.984|0.984|1.7e-09|1.3e-08|2.7e-04|-1.668304e+01 -1.668331e+01| 0:0:00| chol  1  1 
 9|0.989|0.989|3.3e-11|4.9e-10|3.1e-06|-1.668312e+01 -1.668312e+01| 0:0:00| chol  1  1 
10|0.996|0.995|2.0e-13|9.4e-12|4.0e-08|-1.668312e+01 -1.668312e+01| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 10
 primal objective value = -1.66831188e+01
 dual   objective value = -1.66831189e+01
 gap := trace(XZ)       = 4.01e-08
 relative gap           = 1.17e-09
 actual relative gap    = 1.14e-09
 rel. primal infeas     = 1.95e-13
 rel. dual   infeas     = 9.35e-12
 norm(X), norm(y), norm(Z) = 1.9e+01, 8.1e+00, 1.1e+01
 norm(A), norm(b), norm(C) = 1.1e+01, 4.3e+00, 6.3e+00
 Total CPU time (secs)  = 0.22  
 CPU time per iteration = 0.02  
 termination code       =  0
 DIMACS: 4.2e-13  0.0e+00  2.0e-11  0.0e+00  1.1e-09  1.2e-09
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +16.6831
 
Done! 
------------------------------------------------------------------
The optimal point location is: 
    0.0379
    0.0785

The average location of the fixed points is
    0.0379
    0.0785

They are the same as expected!