Questions about the Lsqnonlin function
Show older comments
2022/7/2
Hi all,
Are there any experts here who use the Lsqnonlin function?I'm trying to use the Lsqnonlin function to optimize a more complex function so that its result converges to 0 (i.e. I expect to find an input value that makes the function result most closely converge to 0),But it reports an error, it says error using eig function because the input matrix contains NAN or inf, I don't know how to fix it, can anyone give me an answer? thanks in advance!
By the way the function I want to optimize is a determinant, I found that as long as I reduce the size of the determinant to 4*4, Lsqnonlin does not report an error, but as soon as I make the size of the determinant bigger, it reports an error.
The following is the error information:
Wrong use of eig
The input matrix contains NaN or Inf.
error trust (line 33)
[V,D] = eig(H);
error trdog (line 132)
st = trust(rhs,MM,delta);
error snls
error lsqncommon (line 169)
snls(funfcn,xC,lb,ub,flags.verbosity,options,defaultopt,initVals.F,initVals.J,caller,
...
error lsqnonlin (line 258)
lsqncommon(funfcn,xCurrent,lb,ub,options,defaultopt,allDefaultOpts,caller,...
2022/7/3
Hi all,
This is my code, it is a function file not a script file, where Zero_seek_Main is the main function, the other functions are sub-functions (in order to construct the matrix).
When I reduce the size of the GvXY matrix (4*4 and below), lsqnonlin does not report an error, which is a bit strange.
Looking forward to any reply.
function []=Zero_seek_Main()
clear;clc;close all
%=====================
d=500*1e-9;
rx=d/4;
ry=rx;
num_Re=5;
num_Im=5;
%====================
%==========%
lsqoptions = optimoptions(@lsqnonlin,'Display','none');
%==========%
Real_w0=linspace(0.8,1.2,num_Re);
Imag_w0=linspace(-0.2,0.2,num_Im);
Result_stoXY=zeros(num_Re*num_Im,1);
count=0;
for uu=1:num_Re
for vv=1:num_Im
count=count+1;
mark=count/(num_Re*num_Im)
Re_w=Real_w0(uu);
Im_w=Imag_w0(vv);
w=Re_w+1j*Im_w;
FUNXY=@(x)det(GvXY(x,d,rx,ry));
%======================
[find_x,~,~,~]=lsqnonlin(FUNXY,w,[],[],lsqoptions);
Result_stoXY(count,1)=find_x;
%======================
end
end
figure
Result_index=[1:length(Result_stoXY)].';
plot(Result_index,Result_stoXY,'bo')
end
function [ModeXY]=GvXY(w,d,rx,ry)
c0=299792458;%light
wsp=3.742*1e15;%rad/s
eps0=8.8541*1e-12;
k=w*wsp/c0;
M1=RegSigGtXY(k,d);%size:2*2
M2=SigGtXY(k,rx,ry,d);%size:2*2
ModeXY=[M1,M2,M2,M2;...
M2,M1,M2,M2;...
M2,M2,M1,M2;...
M2,M2,M2,M1];%size:(2*4,2*4)
ModeXY=(k^2)/eps0.*ModeXY;
ModeXY(isnan(ModeXY))=0;%set NAN to 0
ModeXY(isinf(ModeXY))=0;%set INF to 0
%ModeXY=ModeXY(1:4,1:4);%If I enable this command. Then lsqnonlin will not
%report an error, if I increase the
%size of the matrix (for example, 5*5) lsqnonlin will report an error
end
function [o4]=RegSigGtXY(k,d)
N=1;
index_sto=zeros((2*N+1)^2-1,2);
cc=0;
for m=-N:N
for n=-N:N
if m==0&&n==0
cc=cc;
else
cc=cc+1;
index_sto(cc,1:2)=[m,n];
end
end
end
Sum_sto11=zeros((2*N+1)^2-1,1);%xx
Sum_sto22=zeros((2*N+1)^2-1,1);%yy
Sum_sto12=zeros((2*N+1)^2-1,1);%xy
Sum_sto21=zeros((2*N+1)^2-1,1);%yx
parfor ii=1:size(index_sto,1)
m=index_sto(ii,1);
n=index_sto(ii,2);
%==================
nR=abs(m*d+1j*n*d);
Rx=m*d;
Ry=n*d;
B=BB(k,nR);
A=AA(k,nR);
term=exp(1j*k*nR)/(4*pi*nR);
%==================
Sum_sto11(ii,1)=B/(nR^2)*(Rx^2)*term+A*term;
Sum_sto22(ii,1)=B/(nR^2)*(Ry^2)*term+A*term;
Sum_sto12(ii,1)=B/(nR^2)*Rx*Ry*term;
Sum_sto21(ii,1)=B/(nR^2)*Ry*Rx*term;
end
o4=zeros(2,2);
o4(1,1)=sum(Sum_sto11);
o4(2,2)=sum(Sum_sto22);
o4(1,2)=sum(Sum_sto12);
o4(2,1)=sum(Sum_sto21);
end
function [o3]=SigGtXY(k,rx,ry,d)
N=1;
index_sto=zeros((2*N+1)^2,2);
cc=0;
for m=-N:N
for n=-N:N
cc=cc+1;
index_sto(cc,1:2)=[m,n];
end
end
Sum_sto11=zeros((2*N+1)^2,1);%xx
Sum_sto22=zeros((2*N+1)^2,1);%yy
Sum_sto12=zeros((2*N+1)^2,1);%xy
Sum_sto21=zeros((2*N+1)^2,1);%yx
parfor ii=1:size(index_sto,1)
m=index_sto(ii,1);
n=index_sto(ii,2);
%=====================
nR=abs(m*d+n*d*1j+rx+ry*1j);
Rx=m*d+rx;
Ry=n*d+ry;
Term=exp(1j*k*nR)/(4*pi*nR);
A=AA(k,nR);
B=BB(k,nR);
%=====================
Sum_sto11(ii,1)=B/(nR^2)*(Rx^2)*Term+A*Term;
Sum_sto22(ii,1)=B/(nR^2)*(Ry^2)*Term+A*Term;
Sum_sto12(ii,1)=B/(nR^2)*Rx*Ry*Term;
Sum_sto21(ii,1)=B/(nR^2)*Ry*Rx*Term;
end
o3=zeros(2,2);
o3(1,1)=sum(Sum_sto11);
o3(2,2)=sum(Sum_sto22);
o3(1,2)=sum(Sum_sto12);
o3(2,1)=sum(Sum_sto21);
end
function [o1]=AA(k,R)
o1=1+(1j*k*R-1)/((k*R)^2);
end
function [o2]=BB(k,R)
o2=(3-3*1j*k*R-(k*R)^2)/((k*R)^2);
end
2022/7/3 pm
Hi all,
Some people have asked me why I require the determinant of the matrix, and the explanation is as follows:

Obviously, it is a characteristic equation,But note that the eigenvector [a;b;c;d] is unknown, and omega is also unknown, in order to ensure that the eigenvector [a;b;c;d] is not [0;0;0;0], so I need to make det(Gv(w)-lam(w). *eye(4))=0, or converge to 0, because in practice I found it impossible to find the right omega to make det=0.And also I found that fsolve is not as effective as lsqnonlin, or not effective at all.
Dear experienced experts, how do you solve the above problems in your actual projects? It should be very common in practice, right?
14 Comments
Walter Roberson
on 2 Jul 2022
lsqnonlin() can handle complex values, under a number of restrictions.
However, if you are trying to find an independent value that makes the output of the optimized function closest to 0, then lnsqnonlin() is not appropropriate: lsqnonlin() is for fitting a model to a set of data. You should probably be using fsolve(), which is able to handle complex values under the restrictions described above.
There are other approaches, such as splitting real and complex parts into separate variables, and emitting norm() of the objective. Such techniques can permit bounds.
Walter Roberson
on 2 Jul 2022
Finding a value that drives a determinent to 0... that sounds like an eigenvalue problem ?
ma Jack
on 3 Jul 2022
ma Jack
on 3 Jul 2022
Torsten
on 3 Jul 2022
You are working with a matrix ModeXY with values in the order of 1e30 for both real and imaginary part of which you try to take the determinant. This must result in numerical nonsense.
Review your code for errors, scaling of the equations or a reformulation of your problem. This way at least, you won't succeed.
ma Jack
on 4 Jul 2022
Torsten
on 4 Jul 2022
Yes. Searching for w, [a,b,c,d] under the constaint a^2+b^2+c^2+d^2 = 1 should make the use of "det" superfluous.
ma Jack
on 5 Jul 2022
Torsten
on 5 Jul 2022
I don't understand what you mean.
Searching for a vector x=[a b c d] with norm 1 excludes the case of the trivial solution x = 0.
ma Jack
on 6 Jul 2022
Accepted Answer
More Answers (0)
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!