% L1_fit_test

% independent variables
 x = [ -1.2 : 0.05 : 1.2 ]';  m = length(x);   

% design matrix for a power-polynomial fit ...
  X = [ ones(m,1) x x.^2 x.^3 x.^4 x.^5 x.^6 x.^7 x.^8 x.^9 ];
% X = [  x x.^2 x.^3 x.^5 x.^7 ];

% the actual data is not polynomial
%y = x.^2 + tanh(5*x); 
%y = 1 - x.^2 + sin(pi*x);        
 y = 1 - x + exp(-(2*x).^2);

 alpha = 1e-2;            % L1 regularization parameter

 [a, mu, nu, cvg_hst] = L1_fit( X, y, alpha );

 [ n, MaxIter ] = size(cvg_hst);

 n = n/5;        % number of parameteters

 disp(' parameters:'); disp(a')
 disp(' lagrange multipliers mu'); disp(mu')
 disp(' lagrange multipliers nu'); disp(nu')

 i = [1:MaxIter];

 figure(1)
  clf
  plot(x,y,'o', x,X*a,'-')

 ylab = ['a'; 'p'; 'q'; '\mu'; '\nu' ];
 figure(2)
  for ii=1:5
    subplot(5,1,ii)
      plot(i,cvg_hst( 5*(ii-1)+1:5*ii , : ))
       ylabel(ylab(ii,:))
  end


 figure(3)
   plot( X\y,'+', a, 'o','MarkerSize',9,[0,12],[0,0], '--k')
    xlabel('i')
    ylabel('a_i')
    legend('\alpha = 0', sprintf('\\alpha = %8.2e',alpha) )
