Fix variables to real values when using lsqnonlin optimizing

23 views (last 30 days)
Hello,
I'm using lsqnonlin (with levenberg-marquardt) to optimize a non linear least square problem, however, the solver sometimes returns complex numbers.
Is there a way to limit the values to real numbers? I tried adding up and lower bounds at [0] and [inf] but that didn't change anything.

Accepted Answer

Alan Weiss
Alan Weiss on 10 Dec 2012
The solver returns complex numbers only when your objective function returns complex numbers. There might be something you can do to keep the objective function real, such as bounding the range of the decision variables, but I cannot tell what the appropriate range is without seeing your objective function.
In fact, the levenberg-marquardt algorithm in lsqnonlin handles complex-valued data appropriately, so there might not be a problem.
If you insist that the solver never consider a complex value, you can use fmincon with the interior-point or sqp algorithms. But then you lose the efficiency of least squares solvers.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 Comments
alex pabouctsidis
alex pabouctsidis on 10 Dec 2012
Thanks for the comment.
I verified my code and cannot find any source of complex numbers. during the optimizing process the solver tries complex numbers which causes errors in my objective function.
I also make sure my initial guess is complex free.
Here is my objective function (the error arises when optimizing teta1):
function err = ML_problem_LS(Teta1,Teta2,ym,yz)
%Teta1: D11 D12 D13 D21 D22 D23 D31 D32 D33 / ox oy oz / d / a1 b1
%c1 ... ak bk ck
%Teta2: sm_x sm_y sm_z / sz_x sz_y sz_z
k = size(ym,1);
err = NaN(2*k+1,1);
D = [Teta1(1) Teta1(2) Teta1(3);...
Teta1(4) Teta1(5) Teta1(6);...
Teta1(7) Teta1(8) Teta1(9)];
o = [Teta1(10) Teta1(11) Teta1(12)]';
d = Teta1(13);
mn = [sqrt(1-d^2) 0 d]';
sigm = [Teta2(1) 0 0;0 Teta2(2) 0;0 0 Teta2(3)];
sigz = [Teta2(4) 0 0 ; 0 Teta2(5) 0;0 0 Teta2(6)];
for i=1:k
Rbn(:,:) = angle2dcm( Teta1(14+3*(i-1)),Teta1(15+3*(i-1)),Teta1(16+3*(i-1)));
em = ym(i,:)'-D*Rbn*mn-o;
ez = yz(i,:)'-Rbn*[0;0;1];
err(2*i-1) = em'*(sigm\em);
err(2*i) = ez'*(sigz\ez);
end
err(2*k+1) = real(sqrt( k/2*log(det(sigm)) + k/2*log(det(sigz)) ));
end
Alan Weiss
Alan Weiss on 11 Dec 2012
As I said before, the solver does not introduce complex numbers. Your objective function does.
mn can easily be complex, if abs(Teta1(13)) > 1. This leads to err being complex.
As documented here, the lsqnonlin trust-region-reflective algorithm satisfies constraints at all iterations. So use that algorithm, along with appropriate bounds on Teta1(13).
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!