% x(N,M)  - input- M input feature vectors with size Nx1
% alpha   - input- step size
%
clear all
clc;
M=100;  % # of equations constraint
N=5;    % # of unknowns (w0, w1, wN-1)
x=zeros(N,M);

for i=1:M
    x(:,i)=round(rand(N,1));
end
% -- to balance the number of 0 examples and 1 exmaples --
is0_token=0;
for i=1:M
    if sum(x(:,i))==0
        is0_token=is0_token+1;
    end
end
x=[zeros(N,M-is0_token),x];
M=size(x,2);
rank = randperm(M); 
x=x(:,rank);
% --------------------------------------------------------------------------------
t=zeros(M,1);tp=t;
for i=1:M
    if sum(x(:,i))==0
        t(i)=1;
    end
end

x=[ones(1,M);x];  N=N+1;        % add 1st row of x with 1's

%% ------ single note------------------------------------
f=.01;         % step size
w=rand(N,1);        % starting model
lim=.00001;         % stopping criterion
nit=10000;res=zeros(nit,1);
kk=0; beta=1;ioption=0;
for k=2:nit     % Looping over iterations
    [grad,res]=gradient(M,N,x,t,w,beta,res,k,ioption);
    alpha=f;%*sqrt((grad'*grad)/N);
    %if abs(res(k)-res(k-1))<=.01;f=f*.5;end
    w=w-f*grad;  % Gradient descent update
    if res(k)<=lim;k=nit;end
    kk=kk+1
end
figure(1);Display(M,res,nit,w,x,t,beta)
%% ----------two note-------------------------
f=.01;         % step size
N1=10;
w1=rand(N,N1);
w2=rand(N1,1);        % starting model
lim=.00001;         % stopping criterion
nit=10000;res=zeros(nit,1);
kk=0; beta=1;ioption=0;
for k=2:nit     % Looping over iterations
    [grad1,grad2,res]=gradient2(M,x,t,w1,w2,k,res,ioption);
    alpha=f;%*sqrt((grad1(:)'*grad1(:)+grad2(:)'*grad2(:))/(N+N1*N));
    %if abs(res(k)-res(k-1))<=.01;f=f*.5;end
    w1=w1-alpha*grad1;  % Gradient descent update
     w2=w2-alpha*grad2;  % Gradient descent update
    if res(k)<=lim;k=nit;end
    kk=kk+1;
end
figure(2);Display2(M,res,nit,w1,w2,x,t)