lsqcurvefit : undefined values at initial point

6 ビュー (過去 30 日間)
G Guerrer
G Guerrer 2015 年 8 月 7 日
編集済み: Matt J 2019 年 2 月 19 日
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).
G Guerrer
G Guerrer 2015 年 8 月 7 日
Hi Matt, its a 960x1280 uint16 array. Ive attached the file.

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2015 年 8 月 7 日
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 件のコメント
Star Strider
Star Strider 2019 年 2 月 18 日
@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 日
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 件のコメント
G Guerrer
G Guerrer 2015 年 8 月 7 日
Tried to cast a to double. Still not working.
Matt J
Matt J 2015 年 8 月 7 日
Try this simple experiment at the command line.
>> beta=0; sin(beta)/beta
ans =
NaN

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLinear Least Squares についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by