lsqcurvefit : undefined values at initial point
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Star Strider
le 7 Août 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 commentaires
Star Strider
le 18 Fév 2019
@Josh Begale — It would be best for you to post this as a new Question, then delete this Comment.
Plus de réponses (1)
Matt J
le 7 Août 2015
Modifié(e) : Matt J
le 7 Août 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 commentaires
Matt J
le 7 Août 2015
Try this simple experiment at the command line.
>> beta=0; sin(beta)/beta
ans =
NaN
Voir également
Catégories
En savoir plus sur Linear Least Squares dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!