lsqnonlin triggering error for incompatible array sizes

5 views (last 30 days)
I am currently working on a system identification task, where I want to determine parameters used in a simulink model. The model output is compared with measured real-world values within a cost-function. Now I want to use lsqnonlin to solve for optimal model parameters.
This process workes in many cases, but depending on the initial values I sometimes get the following error:
Norm of First-order
Iteration Func-count f(x) step optimality
0 9 5.95176 6.49e+04
1 18 1.77504 0.0600514 1.63e+04
Arrays have incompatible sizes for this operation.
Error in twoStepFinDiffFormulas
Error in finitedifferences
Error in computeFinDiffGradAndJac
Error in sfdnls (line 54)
computeFinDiffGradAndJac(x,funfcn,confcn,valx, ...
Error in snls (line 343)
[newA, findiffevals] = sfdnls(xcurr,newfvec,Jstr,group,alpha, ...
Error in lsqncommon (line 179)
snls(funfcn,xC,lb,ub,flags.verbosity,options,defaultopt,initVals.F,initVals.J,caller, ...
Error in lsqnonlin (line 260)
lsqncommon(funfcn,xCurrent,lb,ub,options,defaultopt,optimgetFlag,caller,...
Error in model_identification (line 77)
[X,RESNORM,RESIDUAL,EXITFLAG]=lsqnonlin(@minFunction,...
As one can see, the optimization already executed several iterations before the error happens and sometimes, depending on the initial values, the minimum is found even without an error. Looking at the function-call-stack I am tempted to believe, that in some cases the lsqnonlin algorithm calls some internal functions with invalid parameters. Is there something obvious I am missing or could there be a problem within the lsqnonlin algorithm?

Accepted Answer

Alex
Alex on 3 Nov 2021
Edited: Alex on 3 Nov 2021
The reason for the error was quite obvious. I had set the Simulink solver to "variable step size" which leads to variable sizes of the output arrays as well. Of course when simulink changes the step size, the objective function returns arrays with incompatible sizes for the gradient calculation.

More Answers (1)

Matt J
Matt J on 3 Nov 2021
I don't think you are missing something obvious, but it is doubtful that the problem is in the lsqnonlin algorithm. Use dbstop to trap the occurrence of the error and test your objective function with whatever set of parameters triggers the error.
  1 Comment
Alex
Alex on 3 Nov 2021
Thank you for the fast response. You got me thinking of the right solution.

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!