Error when using lsqnonlin
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Stine Larsen
am 1 Okt. 2014
Kommentiert: Stine Larsen
am 3 Okt. 2014
I use this function to approximate measured data with a given function. I have earlier with succes performed the same procedure with simulated data.
Intensity represents the measured data.
A_guess_01=0.8;
kappa_guess_01=0.5;
sigma_guess_01=0.8;
A_guess_11=0.8;
phasediff_guess_11=7*pi/4;
kappa_guess_11=0.8;
sigma_guess_11=1.4;
Theta_turn=-pi/4;
A_guess_11_sin=0;
start_point(1,1) = A_guess_01;
start_point(1,2) = kappa_guess_01;
start_point(1,3) = sigma_guess_01;
start_point(1,4)=A_guess_11;
start_point(1,5)=phasediff_guess_11;
start_point(1,6)=kappa_guess_11;
start_point(1,7)=sigma_guess_11;
start_point(1,8)=Theta_turn;
start_point(1,9)=A_guess_11_sin;
st=start_point;
Hej= @(p) abs((p(1)*besselj(0,p(2)*r)+besselj(1,p(6)*r).*(p(4)*cos(theta+p(8))+p(9)*sin(theta+p(8)))*exp(1i*p(5))).*(r/a<=1)...
+ (p(1)*besselj(0, p(2)*a)/besselk(0, p(3)*a)*besselk(0,p(3)*r)+... (p(4)*besselj(1, p(6)*a)/besselk(1, p(7)*a)*cos(theta+p(8))...
+p(9)*besselj(1, p(6)*a)/besselk(1,p(7)*a)*sin(theta+p(8))).*besselk(1, p(7)*r).*exp(1i*p(5)))...
.*(r/a>1)).^2- Intensity;
opts = optimoptions(@lsqnonlin,'DiffMaxChange', 0.05,'FinDiffType', 'central', 'Display','off','MaxFunEvals',2E7,'TolFun',1E-25, 'TolX',1E-25,'MaxIter',4E4);
x0 = st; % arbitrary initial guess
lb = 0.0*ones(size(st));
ub = 5*ones(size(st));
[p_estimated,resnorm,residuals,exitflag,output] = lsqnonlin(Hej,x0, lb,[], opts);
When I run this I get the following error.
Error using snls (line 47)
Objective function is returning undefined values at initial point. lsqnonlin cannot continue.
Error in lsqncommon (line 149)
[xC,FVAL,LAMBDA,JACOB,EXITFLAG,OUTPUT,msgData]=...
Error in lsqnonlin (line 236)
[xCurrent,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...
Error in Mode_decomposition_multiple_modes_intensity_trial (line 189)
[p_estimated,resnorm,residuals,exitflag,output] = lsqnonlin(Hej,x0, lb,[],
opts);%optimset('Display','off','MaxFunEvals',2E6,'TolFun',1E-9,'TolX',1E-9,'MaxIter',2E3));%,opts);
Both the input Intensity and the initial guess yields finite values.
All inputs to this error will be highly appreciated.
0 Kommentare
Akzeptierte Antwort
Matt J
am 1 Okt. 2014
Bearbeitet: Matt J
am 1 Okt. 2014
Both the input Intensity and the initial guess yields finite values.
Not when I run your code. You have not defined r, theta, a, and perhaps other quantities used by Hej. Therefore calling Hej(x0) in isolation immediately yields an error.
18 Kommentare
Weitere Antworten (1)
Alan Weiss
am 1 Okt. 2014
Your settings of TolFun and TolX don't make sense. They should not be smaller than 1e-14, and should probably be much larger. See the documentation on tolerances.
It seems that you have complex numbers in your objective function. Maybe not, maybe the abs makes everything real. And I am also not sure, but it seems that you are summing the squares in your function. You shouldn't do that, you should pass the vector of values to lsqnonlin as documented (see the second sentence in Description). If you have a complex objective function, then you should use the Levenberg-Marquardt algorithm as explained here.
Now, finally, I come to your question. I am not sure, but it seems to me that your initial point is right on the edge of the region where your objective function is finite. Finite differences can step outside this region. So, if I am correct, all you need to do is set start_point(1,9) to a value above 0, maybe 1e-2.
Alan Weiss
MATLAB mathematical toolbox documentation
Siehe auch
Kategorien
Mehr zu Colormaps finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!