Matlab Coder Code check fails with "Error calling ... This call-site expects more outputs than this function can supply"

15 views (last 30 days)
Background: I wrote a function using fmincon to optimize inputs for a control system. Predictive Control so to say.
In Matlab everything works fine, it runs through and delivers good output and solves the control task very well - so i wanted to generate C-Code and evaluate how i can utilize this in a physical controller.
Inputs are: uold - 3x1 vector; TubeTemp - scalar; Traj - 45x1 vector; len - scalar; state - 6x1 vector; UpBounds, 3x1 vector; RateMode - scalar
This function is called in a loop with a system model (standard closed loop control ) and is updated every iteration with new values for the above described variables. Its basically a heating process and the states change of course over time.
function [u] = Executable_MPC(uold,TubeTemp,Traj,len,state,UpBounds,RateMode)
x0 = uold;
%x = uold;
persistent opts
if isempty(opts)
opts = optimoptions('fmincon','Algorithm','sqp','Display','off','MaxIterations',50);
% Pass additional parameters to objfun
objfun = @(x)obj(x,TubeTemp(:,1:len),Traj(:,1:len),state,RateMode);
% Solve
[solution,objectiveValue] = fmincon(objfun,x0,[],[],[],[],repmat(0.01,...
%Write Outputs
u = solution;
% Clear variables
% clearvars objfun
function Error = obj (x,TubeTemp,Traj,t0,Mode)
t_length = length(Traj);
t = zeros(t_length,6);
dt = 60;
for i=1:t_length
if i<t_length/6
Power = x(1);
elseif i >=t_length/6 && i < 2*t_length/6
Power = x(2);
Power = x(3);
t(i,:) = PlantMdl(t0,Power,0,0,TubeTemp(i),dt);
t0 = t(i,:);
if Mode == 1
Error =sum(sum(abs(t(:,3:5) - Traj(2:4,:)')).*[2 1 1]);
Error =40000*sum(sum(abs(t(:,1)-Traj(1,:))));
Using the Matlab Coder GUI it fails in the Step: "Check for Run-Time Issues". I select the calling script as recommended from the GUI and it returns a whole bunch of errors. I don't really understand what the problem is, since i basically generated this script from an example that mathworks provided with the "optimization toolbox live editor". I am not an expert in coding or math - just a control engineer.
Since line 11 is marked together with line 14 i think it has something to do with the way how i call the objective function, but it doesn't give a hint what causes the problem and i didn't find any documentation around fmincon how to do that properly.
Sorry for the Wall of Text but i wanted to provide all info ;)
Thanks in advance for trying to help!

Accepted Answer

Andreas Apostolatos
Andreas Apostolatos on 5 Nov 2021
As mentioned above, the issue gets resolved if you remove the declaration of the optimization options variable as 'persistent'.
I hope this helps.
Kind regards,
  1 Comment
Sebastian Starke
Sebastian Starke on 5 Nov 2021
This solves all problems, never thought that the persistent declaration can lead to this... Many thanks for the help!

Sign in to comment.

More Answers (1)

Sebastian Starke
Sebastian Starke on 4 Nov 2021
Hi again,
i found out some more info:
It seems that in the call of fmincon, the Matlab Code Check expects the objective function to have 3 Outputs (objective value, gradient, hessian). But this seems to be an error, since in the "optimoptions" I already tried to actively set the respective entries to "false" ('SpecifyObjectiveGradient',false). It seems this does not work at all and i am starting to doubt that any of the specified options are evaluated... Maybe i need to contact the mathworks support now, since a provided function does odd stuff.
  1 Comment
Andreas Apostolatos
Andreas Apostolatos on 4 Nov 2021
Hi Sebastian,
The issue seems to be triggered in this case by having the optimization options which you feed into 'fmincon' function declared as persistent. Please do not declare the optimization options as persistent in this case to have this issue resolved.
I hope that this information herein helps.
Kind regards,

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