lsqcurvefit : undefined values at initial point

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

Matt J
Matt J 2015년 8월 7일
The meaning of the contents of a.mat is not clear (to me at least).
Hi Matt, its a 960x1280 uint16 array. Ive attached the file.

댓글을 달려면 로그인하십시오.

 채택된 답변

Star Strider
Star Strider 2015년 8월 7일

2 개 추천

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

Thanks, it worked!
My pleasure!
I am having a similar issue with my code and was wondering if you could assist me? Here is my code and following errors:
F1 = @(x,xdata)(x(1)*(xdata.^2 - xdata.^(-1))); %% Neo-Hookean
F2 = @(x,xdata)(2*(x(1)*(xdata.^2 - xdata.^(-1)) + x(2)*(xdata - xdata.^(-2)))); %% Mooney-Rivlin
F3 = @(x,xdata)((x(1)/x(2))*(xdata.^x(2) - xdata.^(-x(2)/2))); % Ogden
NH1 = lsqcurvefit(F1,[1,1,1],S,MP);
% NH2 = lsqcurvefit(F1,[1,1,1],S,LP);
% MR1 = lsqcurvefit(F2,[1,1,1],S,MP);
% MR2 = lsqcurvefit(F2,[1,1,1],S,LP);
% Og1 = lsqcurvefit(F3,[1,1,1],S,MP);
% Og2 = lsqcurvefit(F3,[1,1,1],S,LP);
Error using snls (line 47)
Objective function is returning undefined values
at initial point. lsqcurvefit cannot continue.
Error in lsqncommon (line 166)
snls(funfcn,xC,lb,ub,flags.verbosity,options,defaultopt,initVals.F,initVals.J,caller,
...
Error in lsqcurvefit (line 257)
lsqncommon(funfcn,xCurrent,lb,ub,options,defaultopt,caller,...
Error in Matlab_Code (line 5)
NH1 = lsqcurvefit(F1,[1,1,1],S,MP);
@Josh Begale — It would be best for you to post this as a new Question, then delete this Comment.
Matt J
Matt J 2019년 2월 19일
편집: Matt J 2019년 2월 19일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2015년 8월 7일
편집: Matt J 2015년 8월 7일

0 개 추천

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

Matt J
Matt J 2015년 8월 7일
편집: Matt J 2015년 8월 7일
I now see that your xdata=1:1280
This means that xdata(600)=600. Since x(1)=600 at your initial point, this will give F(600)=0/0=NaN in your objective function.
Presumably you want F=0 when beta=0? If so, add some post-processing
F(beta==0)=0;
The function is defined for beta->0 since sinbeta also goes to 0. I can plot it and see it is continuous.
Tried to cast a to double. Still not working.
Try this simple experiment at the command line.
>> beta=0; sin(beta)/beta
ans =
NaN

댓글을 달려면 로그인하십시오.

카테고리

제품

질문:

2015년 8월 7일

편집:

2019년 2월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by