I've been trying to find a solution to what I think is a common problem. I have a list of Data like
data = {{0.05, 15}, {0.15, 51}, {0.25, 64}, {0.35, 107}, {0.45, 113}, {0.55, 162}, {0.65, 163}, {0.75, 167}, {0.85, 182}, {0.95, 187}, {1.05, 165}, {1.15, 168}, {1.25, 151}, {1.35, 143}, {1.45, 121}, {1.55, 130}, {1.65, 109}, {1.75, 91}, {1.85, 91}, {1.95, 63}, {2.05, 48}, {2.15, 34}, {2.25, 29}, {2.35, 24}, {2.45, 14}, {2.55, 11}, {2.65, 6}, {2.75, 6}, {2.85, 9}, {2.95, 4}, {3.05, 4}, {3.15, 2}, {3.25, 0}, {3.35, 1}, {3.45, 5}, {3.55, 3}, {3.65, 2}, {3.75, 1}, {3.85, 2}, {3.95, 1}, {4.05, 0}, {4.15, 0}}
All I want is a Gaussian Distribution to be put over my ListPlot. I tried this, but the result was not even close to satisfying.
As I generate the data myself, I can change the format of data, if needed.
What I found and tried so far:
FindFit[data, Gaussian[x, ampl, x0, sigma], x, {ampl, x0, sigma}]
There I don't fulfill the criteria for the FindFit function as my number of coordinates is not equal to the number of variables.
model = PDF[NormalDistribution[μ, σ]]
sol = FindFit[data, model[x], {μ, σ}, x]
Please enlighten me
Answer
Two problems: first, Gaussian is not a built-in function in Mathematica; you need to use the form involving PDF and NormalDistribution instead. Second, the arguments of FindFit need to be in the order data, model, parameters, variable; you have the last two switched. The correct version would be as follows:
model[x_] = ampl Evaluate[PDF[NormalDistribution[x0, sigma], x]];
fit = FindFit[data, model[x], {ampl, x0, sigma}, x]
(* {ampl -> 274.765, x0 -> 1.02404, sigma -> 0.614853} *)
Show[ListPlot[data], Plot[model[x] /. fit, {x, 0, 4.2}, PlotStyle -> Red]]
As you can see, the fit is not great at small $x$, largely because you have no data for negative $x$. You might want to think about whether this data is actually well-approximated by a Gaussian, or whether another distribution would be more appropriate.

Comments
Post a Comment