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.
lsqnonlin triggering error for incompatible array sizes
8 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
更多回答(1 个)
Matt J
2021-11-3
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Outputs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!