how to set nlinfit parameters ?J

3 views (last 30 days)
Anna Levy
Anna Levy on 21 Apr 2017
Commented: Star Strider on 22 Apr 2017
Hello
I would need some help for fitting my data. Let's assume that I want to fit data registred in the matrix "spe":
x=spe(N,1)
y=spe(N,2)
and that it should be fitted by a Gaussian function
I have defined the matlab function for the Gaussian :
function fun = gauss(b1,b2,b3,x)
fun = b3 * (1/(b1*sqrt(2*pi))) * exp(-(x-b2).^2/(2*(b1).^2));
end
and then, I want to use this function to fit my spe data using this expression in my main program using the nlinfit function.
As far as I understood, I have to create a function handle, which, by the way I am not understanding exactly what it is, in order to use nlinfit :
I tried :
mdl = @gauss
b0 = [1 1480 500];
b = nlinfit(spe(:,1), spe(:,2), mdlgausstest,b0);
but it is returning some error message.
Could you help me? Thank you very much !!

Accepted Answer

Star Strider
Star Strider on 22 Apr 2017
I’m in a context here that is very much less than clear.
The Gaussian function is defined as:
Gauss = @(a,b,c,x) a*exp(-((x-b)/(2*c)).^2);
or in terms that the nlinfit function will understand:
Gfcn = @(p,x) p(1).*exp(-((x-p(2))./(2*p(3))).^2);
Use the ‘Gfcn’ function as your objective function with nlinfit and all should be well.
Your ‘mdl’ function makes absolutely no sense to me, so I won’t essay to correct it.
So just use ‘Gfcn’, and — with the correct initial parameter estimates vector — you should have no problems fitting it to your data, if they are truly described by a Gaussian function.
Note I do not have your data to test this with, so I am listing it as UNTESTED CODE. It should work.

More Answers (1)

Anna Levy
Anna Levy on 22 Apr 2017
Thanks for your answer. I was trying to find a way to use nlinfit in a different way. But anyway, I found out my solution.
Sorry my question was really not clear.

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!