clear all
close all

OPT=0;
n=128;

%Generate A, b and L by Hansen's toolbox.
[A,b,x] = shaw(n); exname='shaw'; disp('TEST shaw')
L=get_l(n,1);
A=A./norm(A,'fro');
b=A*x;
x=x/norm(b);
b=b/norm(b);
scale=.9;
delta=scale*norm(L*x)  %constraint for ||Lx||

%add noise to both A and b.
E=randn(size(A));   e=randn(size(b));
E=E/norm(E,'fro');  e=e/norm(e,2);
sigma=0.05;
F=sigma*A.*E;  g=sigma*b.*e;
b1=b+g;     A1=A+F;
%write data file for running C++ program
fd1=fopen('/home/hongbin/C/RTLS/A.txt','w+');
fprintf(fd1,'%8.7f ', A1); fseek(fd1,0,-1);[A1,count1]=fscanf(fd1,'%f ', n*n);
A1=reshape(A1,n,n); fclose(fd1);
fd1=fopen('/home/hongbin/C/RTLS/b.txt','w+');
fprintf(fd1,'%8.7f ', b1); fseek(fd1,0,-1);[b1,count2]=fscanf(fd1,'%f ', n);   fclose(fd1);
fd1=fopen('/home/hongbin/C/RTLS/L.txt','w+');
fprintf(fd1,'%8.7f ', full(L)); fseek(fd1,0,-1);[L,count3]=fscanf(fd1,'%f ',n*(n-1)); fclose(fd1);

L=reshape(L,n-1,n);
[xrt,iter]=rtlsfun(A1,b1,L, delta,1e-4);
relerr=norm(x-xrt)/norm(x);
plot(x)
hold;
plot(xrt,'r')

% run c++ program to solve this problem.
! ./rtls  -oxrtoutput.txt  A.txt 128 128    b.txt L.txt 127    0.067149559204502  1e-4
fp=fopen('/home/hongbin/C/RTLS/xrtoutput.txt');
xrt_cpp=fscanf(fp,'%f ', n);
plot(xrt_cpp,'bo')

legend('True', 'Matlab','C++ ')
H=gcf;
C=get(H,'children');
D=get(C,'children');
set(D{1},'LineWidth',2)
set(D{2},'LineWidth',2)
 H1=legend;
set(H1,'FontSize',18)
set(H1,'FontWeight','bold')
set(H1,'Position',[0.7    0.68    0.200    0.2200])
Ha=gca;
set(Ha,'FontSize',16)
set(Ha,'LineWidth',4)
set(Ha,'Position',[0.1300    0.1100    0.79   0.8150])
TEST shaw

delta =

   0.067149558965580

Current plot held