Figure 6.24: Fitting a convex function to given data

% Section 6.5.5
% Boyd & Vandenberghe "Convex Optimization"
% Original by Lieven Vandenberghe
% Adapted for CVX by Argyris Zymnis - 11/27/2005
%
% Here we find the convex function f that best fits
% some given data in the least squares sense.
% To do this we solve
%     minimize    ||yns - yhat||_2
%     subject to  yhat(j) >= yhat(i) + g(i)*(u(j) - u(i)), for all i,j

clear

% Noise level in percent and random seed.
rand('state',29);
noiseint=.05;

% Generate the data set
u = [0:0.04:2]';
m=length(u);
y = 5*(u-1).^4 + .6*(u-1).^2 + 0.5*u;
v1=u>=.2;
v2=u<=.6;
v3=v1.*v2;
dipvec=((v3.*u-.4*ones(1,size(v3,2))).^(2)).*v3;
y=y+40*(dipvec-((.2))^2*v3);

% add perturbation and plots the input data
randf=noiseint*(rand(m,1)-.5);
yns=y+norm(y)*(randf);
figure
plot(u,yns,'o');

% min. ||yns-yhat||_2
% s.t. yhat(j) >= yhat(i) + g(i)*(u(j) - u(i)), for all i,j
cvx_begin
    variables yhat(m) g(m)
    minimize(norm(yns-yhat))
    subject to
        yhat*ones(1,m) >= ones(m,1)*yhat' + (ones(m,1)*g').*(u*ones(1,m)-ones(m,1)*u');
cvx_end

nopts =1000;
t = linspace(0,2,nopts);
f = max(yhat(:,ones(1,nopts)) + ...
      g(:,ones(1,nopts)).*(t(ones(m,1),:)-u(:,ones(1,nopts))));
plot(u,yns,'o',t,f,'-');
axis off
%print -deps interpol_convex_function2.eps
 
Calling SeDuMi: 2702 variables (102 free), 2601 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 102 free variables
eqs m = 2601, order n = 2755, dim = 2805, blocks = 2
nnz(A) = 7699 + 10302, nnz(ADA) = 127551, nnz(L) = 65076
Handling 103 + 1 dense columns.
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            2.23E-002 0.000
  1 :  2.81E+000 6.78E-003 0.000 0.3039 0.9000 0.9000   0.33  1  1  1.8E+000
  2 :  4.38E+000 2.16E-003 0.000 0.3192 0.9000 0.9000   1.59  1  1  4.6E-001
  3 :  5.38E+000 5.45E-004 0.000 0.2517 0.9000 0.9000   1.42  1  1  8.8E-002
  4 :  5.51E+000 1.14E-004 0.000 0.2099 0.9000 0.9000   1.22  1  1  1.6E-002
  5 :  5.22E+000 4.07E-005 0.000 0.3559 0.9000 0.9000   1.05  1  1  5.8E-003
  6 :  4.90E+000 2.40E-006 0.000 0.0591 0.9040 0.9000   0.91  1  1  2.8E-003
  7 :  4.90E+000 3.71E-007 0.470 0.1542 0.9000 0.0000   0.86  1  1  2.5E-003
  8 :  4.69E+000 2.23E-007 0.229 0.6005 0.9563 0.9000   0.86  1  1  1.7E-003
  9 :  4.53E+000 1.49E-007 0.465 0.6704 0.9675 0.9000   0.85  1  1  1.3E-003
 10 :  4.38E+000 1.31E-007 0.000 0.8745 0.9000 0.9000   0.86  1  1  1.2E-003
 11 :  3.67E+000 8.83E-008 0.000 0.6761 0.9000 0.8368   0.77  1  1  7.6E-004
 12 :  3.48E+000 6.56E-008 0.000 0.7439 0.9321 0.9000   0.84  1  1  5.9E-004
 13 :  3.36E+000 4.93E-008 0.022 0.7503 0.9554 0.9000   0.86  1  1  4.7E-004
 14 :  3.28E+000 3.58E-008 0.443 0.7275 0.9731 0.9000   0.89  1  1  3.7E-004
 15 :  3.20E+000 3.12E-008 0.000 0.8692 0.9000 0.9000   0.93  1  1  3.2E-004
 16 :  2.76E+000 1.81E-008 0.000 0.5806 0.9000 0.8186   0.86  1  1  1.9E-004
 17 :  2.68E+000 1.14E-008 0.000 0.6282 0.9573 0.9000   0.95  1  1  1.3E-004
 18 :  2.49E+000 5.58E-009 0.000 0.4910 0.9000 0.9095   0.92  1  1  6.5E-005
 19 :  2.43E+000 3.51E-009 0.000 0.6287 0.9511 0.9000   0.96  1  1  4.0E-005
 20 :  2.39E+000 2.00E-009 0.000 0.5689 0.9545 0.9000   0.93  1  1  2.2E-005
 21 :  2.31E+000 5.30E-010 0.000 0.2655 0.9000 0.9248   0.89  1  1  6.9E-006
 22 :  2.29E+000 1.67E-010 0.000 0.3157 0.5554 0.9000   0.97  1  1  2.4E-006
 23 :  2.28E+000 5.64E-011 0.000 0.3373 0.9000 0.9000   0.99  1  1  8.3E-007
 24 :  2.28E+000 1.72E-011 0.000 0.3051 0.9000 0.9000   1.00  2  2  2.5E-007
 25 :  2.27E+000 1.63E-012 0.000 0.0950 0.9900 0.9900   1.00  2  2  2.4E-008
 26 :  2.27E+000 5.03E-013 0.000 0.3081 0.9000 0.9000   1.00  2  2  7.4E-009

iter seconds digits       c*x               b*y
 26      9.4   Inf  2.2743017410e+000  2.2743017789e+000
|Ax-b| =  1.6e-008, [Ay-c]_+ =  6.6E-009, |x|= 5.3e+002, |y|= 2.5e+000

Detailed timing (sec)
   Pre          IPM          Post
3.104E-001    9.414E+000    0.000E+000    
Max-norms: ||b||=7.026093e+000, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 49.9994.
------------------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +2.2743