Error when using lsqnonlin

12 views (last 30 days)
Stine Larsen
Stine Larsen on 1 Oct 2014
Commented: Stine Larsen on 3 Oct 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.

Accepted Answer

Matt J
Matt J on 1 Oct 2014
Edited: Matt J on 1 Oct 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 Comments
Stine Larsen
Stine Larsen on 3 Oct 2014
I have included the 2 new lines in Hej. In the first call of Hej, I get a matrix with a single zero. Running the following in debugging mode
k=find(check==0)
k =
403602
[row, col]=find(check==0)
row =
408
col =
448
That is at origo.
I will try moving the vectors slightly so that nothing is to be evaluated at (0,0).
Stine Larsen
Stine Larsen on 3 Oct 2014
Moving away from origo there is no error.

Sign in to comment.

More Answers (1)

Alan Weiss
Alan Weiss on 1 Oct 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
  1 Comment
Stine Larsen
Stine Larsen on 1 Oct 2014
Hi Alan,
Thank you for the input!
I have tried with numerous settings for for TolX and TolFun including numbers larger than 1e-14 - all settings results in the same error.
I do not have complex numbers in my objective function, as you point out the abs makes them real. The Intensity matrix consists of purely real and positive numbers. My function will result in real-valued matrix (which for the given inputs may be plotted).
My initialpoint returns finite values (approximately between -1 and 1) for the object funtion. In principle the objective function should yield finite values as long as a>0.
I have tried changing start_point(1,9) - again with the same result.
New suggestions are very welcome.
Stine

Sign in to comment.

Categories

Find more on Colormaps in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!