Matlab Coder Code check fails with "Error calling ... This call-site expects more outputs than this function can supply"
19 views (last 30 days)
Sebastian Starke on 28 Oct 2021
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;
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);
[solution,objectiveValue] = fmincon(objfun,x0,,,,,repmat(0.01,...
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;
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]);
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!