lsqcurvefit : undefined values at initial point
6 views (last 30 days)
Show older comments
a.mat contains the output of a ccd camera.
y = transpose(mean(a));
x = transpose(1:1280);
x0 = [600 1e4 .01];
parfit = lsqcurvefit(@fraunhof,x0,x,y)
function F = fraunhof(x,xdata)
beta = x(3).*(xdata-x(1));
sinbeta = sin(beta);
F = x(2).*sinbeta.^2./beta.^2;
Running this code returns the following error:
Error using snls (line 47)
Objective function is returning undefined values 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] = ...
2 Comments
Accepted Answer
Star Strider
on 7 Aug 2015
You’re encountering a ‘sin(x)/x’ problem, where your function is undefined at 0. The solution is to have it defined at 0, although you have to fudge your function a bit to do it. I added 1E-8 to ‘beta’ to emulate L’Hospital’s rule, and got a good fit:
fraunhof = @(x,xdata) x(2).*sin(x(3).*(xdata-x(1)) + 1E-8).^2./(x(3).*(xdata-x(1)) + 1E-8).^2;
x0 = [600 1E5 0.01];
parfit = lsqcurvefit(fraunhof,x0,x,y)
figure(1)
plot(x, y)
hold on
plot(x, fraunhof(parfit,x))
hold off
grid
parfit =
649.6130e+000 47.9481e+003 10.3101e-003
5 Comments
Star Strider
on 18 Feb 2019
@Josh Begale — It would be best for you to post this as a new Question, then delete this Comment.
More Answers (1)
Matt J
on 7 Aug 2015
Edited: Matt J
on 7 Aug 2015
When xdata(i)=x(1) or x(3)=0, then beta(i) will be zero and your expression for F
F = x(2).*sinbeta.^2./beta.^2
is undefined.
Similarly, if xdata is being assigned the uint16 variable "a" in your a.mat file, then any operation xdata(i)-x(1) will evaluate to 0 when x(1)>xdata(i). My guess is that this is what's happening, in which case you should cast a to double precision.
4 Comments
Matt J
on 7 Aug 2015
Try this simple experiment at the command line.
>> beta=0; sin(beta)/beta
ans =
NaN
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!