3 views (last 30 days)

Show older comments

In R2020b and R20201a, I have done the following test of lsqcurvefit. The test function below test(M) uses lsqcurvefit to solve a simple linear equation A*x=y under the assumption that the first M variables are known (enforced by setting lb(1:M)=ub(1:M)) and counts the number of function evaluations. I find, strangely, that for any M>1, an additional N=100 function evaluations are done beyond what is reported by lsqcurvefit (and beyond what should be necessary). Also, even though I have specified MaxIterations=5, six iterations are sometimes done. Does anyone know why this occurs?

TRUECOUNTS= test(0),

TRUECOUNTS=test(1),

TRUECOUNTS=test(99),

function TRUECOUNTS=test(M)

if nargin<1, M=0; end

N=100;

xtrue=(1:N).'; %true parameter values

Adata=rand(N);

TRUECOUNTS=0;

ydata=F(xtrue,Adata);

lb=-inf(N,1); ub=+inf(N,1);

known=1:M; %The first M variables are known

lb(known)=xtrue(known);

ub(known)=xtrue(known);

opts=optimoptions(@lsqcurvefit, 'Display','iter','MaxIterations',5);

TRUECOUNTS=0;

lsqcurvefit(@F,zeros(N,1),Adata,ydata,lb,ub,opts);

disp ' '

function y=F(x,A)

y=A*x;

TRUECOUNTS=TRUECOUNTS+1;

end

end

Alan Weiss
on 23 May 2021

Thank you for reporting this odd behavior, Matt. I investigated and found that an internal check for stopping in the nonlinear least-squares code is

if iterationNumber > maxiter

Therefore, when you set 5 as the iteration limit, the check is triggered at the sixth iteration, not the fifth.

In contrast, several other solvers have the more natural

if iterationNumber >= maxiter

This stops the solver at iteration 5. So this is at least an inconsistency, if not a bug. I will now report this in our internal development system.

By the way, the reason that your test sometimes found only 5 iterations, not 6, is that the problem was solved, not that the iteration limit was hit. Your test(99) is that case.

As for why an extra 100 function evaluations are done, your code line

ydata=F(xtrue,Adata);

causes those evaluations, not the lsqcurvefit code.

Thanks again for the report,

Alan Weiss

MATLAB mathematical toolbox documentation

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

Start Hunting!