How can I fit a Gaussian to data that only forms the peak
13 次查看(过去 30 天)
显示 更早的评论
I have some measurement data that shows a dip in the middle.
I'm trying to fit a Gaussian to that dip to characterize the width and height.
![dIdVplot_B_0.2T.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238762/dIdVplot_B_0.2T.png)
Right now I'm cutting my data down to the dip and trying to fit a gaussian with the following code:
gauss = 'a.*exp(-((x-b)./c).^2)+d';
f = fit(cutV,data,gauss);
coeffs = coeffvalues(f);
FWHM = coeffs(3)*2*sqrt(2*log(2));
height = coeffs(1);
figure;
subplot(2,1,1)
plot(cutV,data)
subplot(2,1,2)
plot(f,cutV,data);
While this does fit a Gaussian, it does so very poorly.
![failedfit.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238763/failedfit.png)
Instead of fitting the peak of the gaussian to the curve I have, it tries to fit the whole Gaussian function to the tiny dip I have.
How do I make the fit function only fit the peak of the Gaussian to my data?
0 个评论
采纳的回答
Johannes Fischer
2019-9-18
You should always help your fitting algorithm by setting upper and lower bounds for the parameters. Even more important are starting values for your parameters. Otherwise, your algorithm cant optimize the fits, if the initial guess is too far from the data. Have a look at the documentation for fit (https://www.mathworks.com/help/curvefit/fit.html) on how to do that. Additionaly, your data values are quite small (10^-6), that means that your parameters (e.g. a) are also very small. Usually a fitting algorithm stops, if the changes to the parameters calculated in one iteration is below a certain threshold (DiffMinChange). In that case you either need to adjust your thresholds or adjust your data before fitting (e.g. multiplying by 10^6) and then correctly interpret the resulting parameters (FWHM should not be affected, but height needs to be multiplied by 10^-6 again).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!