function [R,p]=hibaf(d,X,mu)
% Adaline hibafelulete 2 dimenzios bemenet eseten
% X: P × 2 meretu matrix, P(i,:) az i-edik tanitominta bemenete
% d: P × 1 -es oszlopvektor, d(i): i-edik minta besorolasa
% c(w)=Exp{(||d-X*w||_2)^2}
% grad descent batorsagi tenyezoje : mu/lm_max - konvergens, ha 0<mu<1

% Keszitette: Hadhazi Daniel - BME MIT 2018.02.13

M=size(d,1);
p=mean(X'.*(ones(2,1)*d'),2);
R=(X'*X)./M;
w_opt=R\p;
lm=eig(R); mu=mu/max(lm(:));

[xx,yy]=meshgrid(-100:5:100,-100:5:100);
xx=xx.*4+w_opt(1); yy=yy.*4+w_opt(2);

zz=zeros(size(xx,1),size(xx,2),'single');
zz=(d'*d)/M-(p(1).*xx+p(2).*yy).*2+(xx.^2).*R(1,1)+(yy.^2)*R(2,2)+(xx.*yy)*R(1,2)*2;

figure(1);
surf(xx,yy,zz)
figure(2);
contourf(xx,yy,zz)
title(['Lm_1=',num2str(lm(1)),'   ||   Lm_2=',num2str(lm(2))]);

[x,y]=ginput(1);
hold on;

w=zeros(100,2,'single');
w(1,1)=x; w(1,2)=y;

for j=2:100
 dw=(R*(w(j-1,:)')-p).*2*mu;
 w(j,:)=w(j-1,:)-dw';
end

ind=find((((w(:,1)-min(xx(:))).*(w(:,1)-max(xx(:))))<0) &...
         (((w(:,2)-min(yy(:))).*(w(:,2)-max(yy(:))))<0)); 

plot(w(ind,1),w(ind,2),'r-x');
hold off;