MATLAB Answers

Bestfit value doesn't match with the plotted value

20 views (last 30 days)
Vaswati Biswas
Vaswati Biswas on 23 Feb 2021
Commented: dpb on 23 Feb 2021
I am using nlinfit to fit a gaussian square function in my data. My code is shown below:
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));
hold on;
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?

Accepted Answer

dpb on 23 Feb 2021
Edited: dpb on 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 =
For comparison,
>> gaussian_fn(q,q(2)) % at peak center is max value
ans =
>> gaussian_fn(q,555) % where you evaluated on plot
ans =
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.
dpb on 23 Feb 2021
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!