Bestfit value doesn't match with the plotted value

I am using nlinfit to fit a gaussian square function in my data. My code is shown below:
initGuess=[max(g1),500,100]
g1 is my data that needs to be fitted.
gaussian_fn= @(q,x) (q(1)^2.*exp(-2.*((x-q(2)).^2)/q(3).^2));
[bestfit,resid]=nlinfit(x1,g1,gaussian_fn,initGuess);
scatter(x1,g1,'.');
hold on;
A2=gaussian_fn(bestfit,x1);
plot(x1,gaussian_fn(bestfit,x1),'r');
My code gives me the result shown above but the bestfit value shown in the workspace (maked with yellow highlighter ) is different from the value shown in the graph. In graph it shows me 0.9065 whereas bestfit value is 0.9522. Why it is different? why it is not giving me the samevalue shown as bestfit value?

 Risposta accettata

dpb
dpb il 23 Feb 2021
Modificato: dpb il 23 Feb 2021
Because you used
gaussian_fn= @(q,x) (q(1)^2.*exp(-2.*((x-q(2)).^2)/q(3).^2));
as your fitting function -- in which q(1) is squared --
>> q=[0.9522,559.2633,425]; % from your screenshot
>> q(1)^2
ans =
0.9067
>>
For comparison,
>> gaussian_fn(q,q(2)) % at peak center is max value
ans =
0.9067
>> gaussian_fn(q,555) % where you evaluated on plot
ans =
0.9065
>>
Use
gaussian_fn= @(q,x) (q(1).*exp(-2.*((x-q(2)).^2)/q(3).^2));
if you want the peak amplitude to be same as coefficient.

4 Commenti

Vaswati Biswas
Vaswati Biswas il 23 Feb 2021
Modificato: Vaswati Biswas il 23 Feb 2021
Thanks a lot. It solves my confusion regarding this one. But then if I want to show the difference between the gaussian fit and gaussian square fit in the same data then how can I do that? is it possible using nlinfit in matlab? I am trying to do the both by using the same (using the same code just changes the functions) data but I am not seeing any difference.fitting parameters turn out to be same for both the cases. But we can expect changes in these cases as one is exp and another one is exp^2 function. Kindly help me in this.
"...but we can expect changes in these cases as one is exp and another one is exp^2 function."
No. All you have done here is square the leading coefficient; the estimate will be the same excepting q(1) will be square (or square root) of the other.
A squared exponential function would be
gaussian_fnSQ= @(q,x) (q(1).*exp(-2.*((x-q(2)).^2)/q(3).^2)).^2;
Or, instead of squaring the function, fit the square of the data instead is more generally the way I've seen approached.
Are you working in some field that a squared exponential distribution is a common tactic? I've not run across that...then again, that doesn't necessarily prove anything--there's certainly more in the world I'm not familiar with than am. :)
Your answer helped me a lot to understand the thing. Thank you so much. I am trying to fit the function with the laser intensity data. So I simply applied the concept that intensity is proportional to E^2 and thus I just squared the function. But I should have thought a bit before doing that. Thanks again!
Glad to try to help...
I was going to comment that the Gaussian doesn't do a great job; your peak is flattened and broadened noticeably from a pure Gaussian altho it is pretty-good on a symmetry basis.
Some transformation or alternate distribution form would seem reasonable to use, agreed.

Accedi per commentare.

Più risposte (0)

Categorie

Richiesto:

il 23 Feb 2021

Commentato:

dpb
il 23 Feb 2021

Community Treasure Hunt

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

Start Hunting!

Translated by