MATLAB Answers

Obtaining the Gaussian equation for a distribution

조회 수: 6(최근 30일)
Ahmed Abdulla
Ahmed Abdulla 2021년 7월 28일
편집: Adam Danz 2021년 7월 28일
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
dpb
dpb 2021년 7월 28일
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.

댓글을 달려면 로그인하십시오.

답변(1개)

Adam Danz
Adam Danz 2021년 7월 28일
편집: Adam Danz 2021년 7월 28일
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]);
Local minimum possible. lsqnonlin stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
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))

태그

제품


릴리스

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by