lsqnonlin projectBox error when using OutputFcn

2 views (last 30 days)
Dennis Nikitaev
Dennis Nikitaev on 8 Mar 2022
Commented: Dennis Nikitaev on 10 Mar 2022
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,...

Answers (1)

Alan Weiss
Alan Weiss on 9 Mar 2022
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 Comment
Dennis Nikitaev
Dennis Nikitaev on 10 Mar 2022
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by