Error in lsqcurvefit (line 253)

3 views (last 30 days)
liv
liv on 3 Mar 2014
Answered: John D'Errico on 3 Mar 2014
I try to fit my data to a non-linear equation using lsqcurvefit and I recieve the followong error:
Error using snls (line 182) Finite difference Jacobian undefined at initial point. lsqcurvefit cannot continue.
Error in lsqncommon (line 150) [xC,FVAL,LAMBDA,JACOB,EXITFLAG,OUTPUT,msgData]=...
Error in lsqcurvefit (line 253) [xCurrent,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...
This is the code I'm using ---
k_b=1.3806488*10^(-23);
F= @(x,h_data) (1-(exp(x(2).*h_data./(k_b*x(1)))+exp(-x(2).*h_data./(k_b*x(1)))-2)./((x(2).*h_data./(k_b*x(1)).*(exp(x(2).*h_data./(k_b*x(1)))-exp(-x(2).*h_data./(k_b*x(1)))))));
cos_theta_data=[0.703 0.796 0.897]; h_data=[0.00005 0.0005 0.003];
x0 = [298,10^(-17)]; [y]=F(x0,h_data);
[x,resnorm,~,exitflag,output]=lsqcurvefit(F,x0,h_data,cos_theta_data);
hold on plot(h_data,F(x,h_data)) hold off
Any ideas on what could be the problem?
Thanks!
  1 Comment
Star Strider
Star Strider on 3 Mar 2014
Plot it with your data and initial parameters. That may give you some insight into the problem.

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 3 Mar 2014
So many problems.
It is not an error in lsqcurvefit, but in what you are trying to do with that code.
Read the error message. At your starting point, is the function defined?
At your starting point, if the code tries to move slightly away form that point to compute a Jacobian, is it still defined in that local area?
If not, then what does it tell you? To me, it says you need to change your starting point to something more usable.
Next, you have THREE data points, and you are trying to fit a model with TWO parameters? Surely this is a waste of time, silly in the extreme?
Finally, your parameters vary by 19 powers of 10! This is a BAD thing to do to the linear algebra. It is a BAD thing to do for the finite difference code. Scale your parameters so they are both roughly the same order of magnitude. A few powers of 10 is irrelevant, but a dynamic range of 10^19 asking for trouble.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!