% GAUSSIAN_FIT2

close all
clear all

m = 0;  % Mean of the Gaussian curve
v = 18;  % Variance of the Gaussian curve

x = [-15:0.1:15]; % Abscissa points (i.e. points where the Gaussian is sampled)
fx = 1/sqrt(v*2*pi)*exp(-(x-m).^2/2/v); % Ordinate points (i.e. samples on Gaussian)
n = 1/sqrt(v*20*pi)*(rand(size(x))-0.5);
fx = fx+n;

figure(1)
plot(x,fx,'r'), hold on, shg
%return

a = [x; fx];
p0 = [0; 0.5]; % Initial values of the Guassian parameters
p = fminsearch(@(p) cost(p,a),p0); % Fit Gaussian to data points
me = p(1); % Mean estimated as obtained from the fitting
ve = p(2); % Variance estimated as obtained fromt the fitting

xx = [-15:0.1:15];
fxx = 1/sqrt(ve*2*pi)*exp(-(xx-me).^2/2/ve);

plot(xx,fxx,'b'), hold off, shg
