% Esempio di funzione con rumore
global X Y

clf
a = -6;
b = 6;
NG = 2;
w = [0.6, 0.4];
mu = [-1.1221, 0.9845];
sigma = [2, 0.5];

X = linspace(a,b)';
Y = zeros(size(X));
N = length(X);

for k=1:NG
    Y = Y + w(k)*fgauss(X,mu(k),sigma(k));
end
Y = Y + 0.1*(rand(size(X))-0.5);
plot(X,Y);
dsigma = 0.6;
xg = a:dsigma:b;
ng = length(xg);
PHI = zeros(N,ng);
for k = 1:ng
    PHI(:,k) = fgauss(X,xg(k),dsigma);
end
a = PHI\Y;
Yg = zeros(size(X));
for k=1:ng
    Yg = Yg + a(k)*fgauss(X,xg(k),dsigma);
end
hold on
plot(X,Yg,'r')
% Ricerca di minimo non lineare
c0 = [0.5 -2 2 0.5 2 1]';
c = fminsearch('ferror',c0);
Ym = c(1)*fgauss(X,c(2),c(3))+c(4)*fgauss(X,c(5),c(6));
plot(X,Ym,'g')
