rng('default')
x = [randn(30,1); 5+randn(30,1)];
[f,xi] = ksdensity(x);
fun = @(xx,t,y)rms(y-(xx(5)*1./sqrt(xx(1)^2*2*pi).*exp(-(t-xx(2)).^2/(2*xx(1)^2))+...
xx(6)*1./sqrt(xx(3)^2*2*pi).*exp(-(t-xx(4)).^2/(2*xx(3)^2)) ) );
[x,fval] = fminsearch(@(r)fun(r,xi,f),[2,0.5,2,4,0.5,0.5]);
x([1,3]) = abs(x([1,3]));
pd1 = makedist('Normal','mu',x(2),'sigma',x(1));
pd2 = makedist('Normal','mu',x(4),'sigma',x(3));
y1 = pdf(pd1,xi)*x(5);
y2 = pdf(pd2,xi)*x(6);
figure
plot(xi,f);
hold on;
plot(xi,y1);
plot(xi,y2);
plot(xi,y2+y1);
legend({'ksdensity',['\mu : ',num2str(x(2)),'. \sigma :',num2str(x(1))],...
['\mu : ',num2str(x(4)),'. \sigma :',num2str(x(3))],'pdf1+pdf2'})