Problem with matlabFunction throwing error in latest MATLAB version
Show older comments
I was digging into some code I wrote a few years ago that runs fine on MATLAB 2016a and it's throwing an error at a line of code that uses matlabFunction to create a function file from some symbolic expressions and variables. The file to be created is an objective function file with gradient for a nonlinear optimization.
matlabFunction(Cost,gradCost,'file',filename,'vars',ObjFunVar);
Here Cost and gradCost are are the objective function and gradient which are 1x1 sym and 360x1 sym respectively. ObjFunVar is a cell array that contains the symbolic variables for the optimization and symbolic variables through which I'm passing parameters to the function.
ObjFunVar = {x,T__out,p__E,Delta__t,T__in0,eta__fan,eta__heat,G__0,P__fan,xi__mix,Scale__f,Scale__h};
The variables for the optimization are in x, a 360x1 sym. T__out and p__e are 120x1 sym, and the rest are 1x1 sym.
The error I'm getting is this
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in sym/matlabFunction>computeVarnames (line 902)
allNames = matlab.lang.makeUniqueStrings([repelem("ft",1,nuOfBlocks-1), "ft"
allVars],1:nuOfBlocks-1,namelengthmax);
Error in sym/matlabFunction>postProcessing (line 826)
[newFunctionNames, newArgumentName] = computeVarnames(blocks);
Error in sym/matlabFunction (line 198)
blocks = postProcessing(blocks);
Error in MPC_Heat_Only3 (line 160)
matlabFunction(Cost,gradCost,'file',filename,'vars',ObjFunVar);
As I said, this doesn't happen if I run the code on MATLAB 2016a - everything executes fine. I've found that if I scale back the size of the optimization so that x and gradCost wind up being 150x1, T__out and p__e are 50x1, then there is no error and the whole thing executes without a problem.
Is there something about matlabFunction, the Symbolic Toolbox, or something else that has been changed that could be causing this?
EDIT: I've uploaded the two files of code. MPC_Heat_Only3.m is the code to run. HVAC_MPC_Controller_parameters_Heat_Only_3.m initializes a bunch of parameters and needs to be in the same folder as MPC_Heat_Only3.m.
6 Comments
Walter Roberson
on 13 Apr 2021
Anthony Mogis
on 14 Apr 2021
Walter Roberson
on 14 Apr 2021
An experiment I did last night with 'optimize' turned on showed quite poor optimization ("pessimization" would not be inappropriate for what it did!), but I did not manage to trigger any actual error. (I reported the pessimization)
Walter Roberson
on 15 Apr 2021
I just dug into the code a little. The R2021a version of matlabFunction() has a substantial rewrite compared to the R2020b version, and the problem is occuring in the new code.
Walter Roberson
on 15 Apr 2021
I identified a bug and tested a work-around.
After the work-around, you have a problem in line 288 of your code, that you want to save the non-existent variable Tdiff.
Anthony Mogis
on 15 Apr 2021
Accepted Answer
More Answers (0)
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!