Obtaining the Gaussian equation for a distribution
76 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have attached a picture where i have the points in red (array of x and y values) and I would like to fit those points using a gaussian distribution as shown in blue but i am finding it diccult to do so and obtain the equation of that distribution. Any help is appreviated
1 commentaire
dpb
le 28 Juil 2021
Those data points aren't even close to a normal (Gaussian) distribution overall; you could fit a gaussian + some form of a baseline (start with fixed mean, maybe, unless have some other knowledge).
If you have the Curve Fitting TB, see the examples for Gaussian fitting and custom model
You can try a two-term Gaussian fit of the prepackaged models, for some reason they diidn't include any with predefined background functions but show an example.
Réponses (1)
Adam Danz
le 28 Juil 2021
Modifié(e) : Adam Danz
le 28 Juil 2021
This demo creates a noisy gaussian with unknown random parameters and then fits the data using lsqnonlin.
1. Create noisy gaussian with unknown parameters. The gaussian function below includes terms for location of peak (mu), width (sig), amplitude (amp), and vertical offset (vo).
rng(95737) % for reproducible random values.
gaus = @(x,mu,sig,amp,vo)amp*exp(-(((x-mu).^2)/(2*sig.^2)))+vo;
x = 0:.15:10;
mu = rand(1)*randi(8) + 1; % unknown mean
sig = rand(1) + .2; % unknown width
amp = randi([3,10]); % unknown amplitude
vo = 0;
y = gaus(x,mu,sig,amp,vo);
y = y + rand(size(x)).*y; % add poisson noise
2. Fit the data using lsqnonlin. This requires estimating the parameters. The location of the peak and amplitude are estimated using max(). The width is assumed to be 1 but you may need to use a more rigorous estimation of the width. Vertical offset is assumed to be 0. This demo does not set the upper and lower bounds but you may want to include them.
FUN = @(p)gaus(x,p(1),p(2),p(3),p(4)) - y;
[ampHat, maxIdx] = max(y);
muHat = x(maxIdx);
pEst = lsqnonlin(FUN,[muHat, 1, ampHat, 0]);
3. Plot the raw data and the fit. Show the fit parameters in the figure title.
plot(x,y, 'o','DisplayName','raw data')
hold on
xFit = linspace(min(x),max(x),100);
yFit = gaus(xFit,pEst(1),pEst(2),pEst(3),pEst(4));
plot(xFit, yFit,'r-','DisplayName','fit')
legend()
title(sprintf('mean=%.2f, sigma=%.2f, amp=%.2f, voff=%.2f',pEst))
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!