lsqnonlin projectBox error when using OutputFcn

조회 수: 3 (최근 30일)
Dennis Nikitaev
Dennis Nikitaev 2022년 3월 8일
댓글: Bruno Luong 2022년 8월 24일
Hello,
I am trying to check the number of iterations and residuals after each iteration of lsqnonlin. I have more unknowns than equations and have set it to use the levenberg-marquardt algorithm. I also made the following output function:
function [stop]=residual_check_fun(x,optimValues,state)
keyboard
iteration=optimValues.iteration;
residual=optimValues.residual;
if iteration>5 && residual>10000
stop=true;
else
stop=false;
end
end
This function works when I have one equation and one unknown, but does not work for more unknowns than equations. The error I am getting is the following:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in projectBox (line 39)
dx(activeLB) = lb(activeLB) - x(activeLB);
Error in levenbergMarquardt>@(x,dx)projectBox(x,dx,lb,ub) (line 125)
project = @(x,dx) projectBox(x,dx,lb,ub);
Error in levenbergMarquardt>computeFirstOrderOpt (line 385)
a = norm(project(XOUT,-gradF),Inf);
Error in levenbergMarquardt>callOutputAndPlotFcns (line 410)
optimValues.firstorderopt = computeFirstOrderOpt(project,xOutputfcn,gradF);
Error in levenbergMarquardt (line 147)
[optimValues, stop] = callOutputAndPlotFcns(outputfcn,plotfcns,caller,reshape(XOUT,xShape),...
Error in lsqncommon (line 186)
levenbergMarquardt(funfcn,xC,lb,ub,flags.verbosity,options,defaultopt,initVals.F,initVals.J, ...
Error in lsqnonlin (line 260)
lsqncommon(funfcn,xCurrent,lb,ub,options,defaultopt,optimgetFlag,caller,...

답변 (2개)

Alan Weiss
Alan Weiss 2022년 3월 9일
The error is because optimValues.residual is a vector, not a scalar. Possibly you want to write norm(optimValues.residual).
Alan Weiss
MATLAB mathematical toolbox documentation
  댓글 수: 1
Dennis Nikitaev
Dennis Nikitaev 2022년 3월 10일
But that line is after the keyboard line. The error occurs before the body of the function. It seems that the error is in between the lsqnonlin and the output function.

댓글을 달려면 로그인하십시오.


Bianca Romanski
Bianca Romanski 2022년 8월 23일
편집: Bianca Romanski 2022년 8월 23일
tl;dr Try updating your Matlab version.
Hello Dennis,
I got exactly the same error as you did. To understand the error better, I reproduced it with some code as simple as possible:
% Call optimization
x0 = [0 1];
options = optimoptions(@lsqnonlin, 'Algorithm','levenberg-marquardt','OutputFcn',@outfun,...
'Display','iter');
[xsol,fval] = lsqnonlin(@objfun,x0,[-1 -1],[1 1],options);
% [xsol,fval] = lsqnonlin(@objfun,x0,[],[],options);
function stop = outfun(x,optimValues,state)
stop = false;
end
function f = objfun(x)
f = exp(x(1))*(4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) +...
2*x(2) + 1);
end
I found out, that the projectBox error occurs only when constraints are used. Without constraints the script worked perfectly fine. As I couldn't think about any reason why output functions only work for unconstraint problems, I was wondering whether this might be a bug in the Matlab function. As bugs are fixed sometimes, I updated my Matlab and ran the above code in Matlab 2022a (instead of 2020b) and it worked! Maybe updating your Matlab version will also solve your issue. :)
  댓글 수: 3
Bianca Romanski
Bianca Romanski 2022년 8월 24일
I changed the 5th line, so that upper and lower bounds are column vectors.
[xsol,fval] = lsqnonlin(@objfun,x0,[-1; -1],[1; 1],options);
That didn't change anything, the same error occurs.
Bruno Luong
Bruno Luong 2022년 8월 24일
Thanks

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by