# peak detection – Fitting data by linear conbination of distribution

I have data

``````test3={0.329727, 2.58106, 3.13688, 4.75399, 6.64089, 9.73916, 11.7021, 14.4481, 16.3523, 14.6448, 14.1997, 16.8602, 18.1041, 17.6997, 15.804, 13.9181, 12.5242, 10.8824, 8.61049, 4.43998, 2.78258, 0.329727}
``````

My aim is to fit this data by linear combination of normal distributions.(My graphycal image is below.)

``````a = Range(22);
b = N(Exp(-(a - 9)^2/8));
c = N(Exp(-(a - 14)^2/10));
d = N(Exp(-(a - 18)^2/3));
g = ListLinePlot({b*13, c*17, d*2}, PlotRange -> {All, {0, 20}});
data = ListLinePlot(test3, PlotMarkers -> {Automatic, 10});
Show({g, data})
``````

I want to find the best parameters by Least Squares, and also want to get the residuals.

That is why, I tried the code (Method1, Method2) below, but unfortunately, they did not work at all…

Method1:

``````ClearAll;
peakfunc(A1_, (Mu)1_, (Sigma)1_, xz_) =
A1^2 E^(-((xz - (Mu)1)^2/(2 (Sigma)1^2)));
ff = FindFit(test3,
peakfunc(AA1, uu1, ss1, xi1) + peakfunc(AA2, uu2, ss2, xi1) +
peakfunc(AA3, uu3, ss3, xi1), {AA, uu, ss, AA2, uu2, ss2, AA3, uu3,
ss3}, xi1)
``````

Method2:

``````fit2=LeastSquares(test3, I1*NormalDistribution((Mu)1, (Sigma)1) +
I2*NormalDistribution((Mu)2, (Sigma)2) +
I3*NormalDistribution((Mu)3, (Sigma)3))
``````

Does anyone tell me how to fit data by linear combination of distributions, using Least square? It is more helpful, if you give me the idea to get residuals.