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

for i=1:M
    x(:,i)=round(rand(N,1));
end
for i=1:M
    if sum(x(:,i))==0;t(i)=1;end
end
%--------- adding second order nonlinear term-----------
N_nol=N+(N+1)*N/2;
x_nol=zeros(N_nol,M);
x_nol(1:N,:)=x;
in_token=N;
for in=1:N
    for inn=in:N
        in_token=in_token+1;
        x_nol(in_token,:)=x(in,:).*x(inn,:);
    end
end
x=x_nol; N=N_nol;

x=[ones(1,M);x];  N=N+1;        % add 1st row of x with 1's
%% ------------------------------------------
alpha=.001;         % step size
w=rand(N,1);        % starting model
lim=.00001;         % stopping criterion
nit=1000;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=.01*sqrt(grad'*grad);
    if abs(res(k)-res(k-1))<=.01;alpha=alpha*.5;end
    w=w-alpha*grad;  % Gradient descent update
    if res(k)<=lim;k=nit;end
    kk=kk+1
end
Display(M,res,nit,w,x,t)