Drexel dragonThe Math ForumDonate to the Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Fitting to a Gaussian using nlinfit
Replies: 5   Last Post: May 23, 2013 1:10 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Tom Lane

Posts: 854
Registered: 12/7/04
Re: Fitting to a Gaussian using nlinfit
Posted: Jan 10, 2013 10:52 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Ben, nlinfit is doing a couple of things that are difficult in your problem.
First, it is trying to compute derivatives numerically by perturbing the
parameters a bit. Second, it is trying to measure convergence using the
change in parameters from one iteration to another. This is hard because you
have one parameter of the order of 1e12, and others of the order of 1e-8. A
simple way to avoid the problem is to build these scale factors into your
model, and make sure the parameters being estimated are of comparable
magnitude. The following shows how to do this on either the original or log

init_guesses = [1 3 1];
norm_func = @(p,dp) 2e12*p(1).*exp(-.5.*((dp-1e-8*p(2))/(p(3)*1e-8)).^2);
xx = linspace(min(dp_data),max(dp_data));
plot(dp_data, log(N_data),'bo', xx,log(norm_func(bestfit,xx)),'r-',

[bestfit,resid]=nlinfit(dp_data,N_data,@(p,x) norm_func(p,x),init_guesses);
plot(dp_data, N_data,'bo', xx,norm_func(bestfit,xx),'r-',

Further hint: when you post code, make sure it runs. You had multiple
undefined variables in your code. That's why I supplied my own init_guesses,
bypassing that part of your code.

-- Tom

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.