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] = ...
采纳的回答
更多回答(1 个)
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 个评论
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;
G Guerrer
2015-8-7
G Guerrer
2015-8-7
Matt J
2015-8-7
Try this simple experiment at the command line.
>> beta=0; sin(beta)/beta
ans =
NaN
类别
在 帮助中心 和 File Exchange 中查找有关 Linear Least Squares 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!