Matlab Coder Code check fails with "Error calling ... This call-site expects more outputs than this function can supply"
23 views (last 30 days)
Show older comments
Sebastian Starke
on 28 Oct 2021
Commented: Sebastian Starke
on 5 Nov 2021
Hi,
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.
Function:
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;
%len=2000;
persistent opts
if isempty(opts)
opts = optimoptions('fmincon','Algorithm','sqp','Display','off','MaxIterations',50);
end
% 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,...
size(x0)),UpBounds,[],opts);
%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);
else
Power = x(3);
end
t(i,:) = PlantMdl(t0,Power,0,0,TubeTemp(i),dt);
t0 = t(i,:);
end
if Mode == 1
Error =sum(sum(abs(t(:,3:5) - Traj(2:4,:)')).*[2 1 1]);
else
Error =40000*sum(sum(abs(t(:,1)-Traj(1,:))));
end
end
end
Problem:
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.
Errors:
Sorry for the Wall of Text but i wanted to provide all info ;)
Thanks in advance for trying to help!
0 Comments
Accepted Answer
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,
Andreas
More Answers (1)
Sebastian Starke
on 4 Nov 2021
1 Comment
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,
Andreas
See Also
Categories
Find more on MPC Design 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!