lsqcurvefit : undefined values at initial point

6 views (last 30 days)
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
Matt J
Matt J on 7 Aug 2015
The meaning of the contents of a.mat is not clear (to me at least).
G Guerrer
G Guerrer on 7 Aug 2015
Hi Matt, its a 960x1280 uint16 array. Ive attached the file.

Sign in to comment.

Accepted Answer

Star Strider
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
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.
Matt J
Matt J on 19 Feb 2019
Edited: Matt J on 19 Feb 2019
@Josh Begale — Or better still, since you have the same issue, why don't you read the replies in this thread and apply them to your issue as well.

Sign in to comment.

More Answers (1)

Matt J
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
G Guerrer
G Guerrer on 7 Aug 2015
Tried to cast a to double. Still not working.
Matt J
Matt J on 7 Aug 2015
Try this simple experiment at the command line.
>> beta=0; sin(beta)/beta
ans =
NaN

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!