Function 'fmincon' not supported for code generation

Hello,
im currently trying to implement nonlinear MPC in MATLAB/Simulink using the MATLAB function block.
function u_mpc = MPC(x, u_mpc, u_0, A_ineq, b_ineq, Q_mpc, R_mpc)
costfunc = @(x,u) (x)'*Q_mpc*(x) + (u_mpc)'*R_mpc*(u_mpc);
u_mpc = fmincon(costfunc, u_0, A_ineq, b_ineq);
end
The state x is provided by a plant model, the costfunction is a simple quadratic cost with weight matrices, the matrices are taken from the workspace.
Running the Simulink model results in the error: Function 'fmincon' not supported for code generation.
I'm using the 2018b version normally, tried using 2021a but got the same error.
Is there a mistake in the code or is it simply not possible to use fmincon inside a MATLAB function block?
Thanks in advance for any help.

2 件のコメント

Walter Roberson
Walter Roberson 2021 年 5 月 8 日
編集済み: Walter Roberson 2021 年 5 月 11 日
Anuschan Albrecht
Anuschan Albrecht 2021 年 5 月 8 日
Thanks for your fast answer!
On the help page you linked it says to not load options from a file so I added them inside the MATLAB function block:
function u_mpc = MPC(x, u_mpc, u_0, A_ineq, b_ineq, Q_mpc, R_mpc)
costfunc = @(x,u) (x)'*Q_mpc*(x) + (u_mpc)'*R_mpc*(u_mpc);
options = optimoptions('fmincon','Algorithm','sqp');
u_mpc = fmincon(costfunc, u_0, A_ineq, b_ineq,options);
end
This results in the error: Function 'optimoptions' not supported for code generation.

サインインしてコメントする。

 採用された回答

Aditya Patil
Aditya Patil 2021 年 5 月 11 日
編集済み: Aditya Patil 2021 年 5 月 12 日

1 投票

fmincon codegen support was added in R2019b. Also, optimOptions is the last argument for fmincon, you should pass other arguments as empty ([]) if you are not using them. For example,
function u_mpc = MPC(x, u_mpc, u_0, A_ineq, b_ineq, Q_mpc, R_mpc)
costfunc = @(x,u) (x)'*Q_mpc*(x) + (u_mpc)'*R_mpc*(u_mpc);
options = optimoptions('fmincon','Algorithm','sqp');
Aeq = [];
beq = [];
lb = [];
ub = [];
nonlcon = [];
u_mpc = fmincon(costfunc,u_0,A_ineq,b_ineq,Aeq,beq,lb,ub,nonlcon,options);
end

9 件のコメント

Anuschan Albrecht
Anuschan Albrecht 2021 年 5 月 11 日
編集済み: Anuschan Albrecht 2021 年 5 月 11 日
Thanks for your answer!
I didn't include them initially because passing empty variables resulted in this error: Inferred size for data 'A_eq' is empty, which is invalid for a block parameter.
Nonetheless, even defining them inside the function as you suggested didn't resolve the original problem of:
Function 'optimoptions' not supported for code generation.
and
Undefined function or variable 'options'. The first assignment to a local variable determines its class.
Aditya Patil
Aditya Patil 2021 年 5 月 11 日
Can you share your code?
Anuschan Albrecht
Anuschan Albrecht 2021 年 5 月 11 日
function u_mpc = MPC(x, x_ref, u_mpc,u_ref, u_0, A_ineq, b_ineq, Q_mpc, R_mpc)
A_eq = [];
b_eq = [];
lb = [];
ub = [];
nonlcon = [];
costfunc = @(x,u) (x)'*Q_mpc*(x) + (u_mpc)'*R_mpc*(u_mpc);
options = optimoptions('fmincon','Algorithm','sqp');
u_mpc = fmincon(costfunc, u_0, A_ineq, b_ineq, A_eq, b_eq, lb, ub, nonlcon, options);
end
That's the latest iteration of the code inside the MATLAB function block.
Walter Roberson
Walter Roberson 2021 年 5 月 11 日
What is the size() of what is being passed in for A_ineq and b_ineq ?
Anuschan Albrecht
Anuschan Albrecht 2021 年 5 月 11 日
Input x is a vector of size 10x1.
A_ineq and b_ineq are currently filled with zeros:
A_ineq = zeros(10);
b_ineq = zeros(10,1);
Walter Roberson
Walter Roberson 2021 年 5 月 11 日
What are the size() of your various inputs?
I notice that your u_mpc is both an input an and output, and that u_mpc is the result of fmincon() . fmincon() always returns a row vector: is your u_mpc also a row vector ?
Where do you use your input x in your function?
fmincon passes a vector in, but your cost function expects two inputs.
Aditya Patil
Aditya Patil 2021 年 5 月 12 日
編集済み: Aditya Patil 2021 年 5 月 12 日
fmincon codegen support was added in R2019b. The code works fine in R2021a. I recommend updating to latest release.
Anuschan Albrecht
Anuschan Albrecht 2021 年 5 月 12 日
Using R2021a the code did indeed work, which is strange because I already tested an earlier iteration on a newer MATLAB version and it still produced errors. It probably had another coding mistake.
Anyways the code is running without errors on R2021a now, thanks for all the help!
Walter Roberson
Walter Roberson 2021 年 5 月 12 日
Good point about needing a later release! I normally think about that kind of issue early on, but I missed it this time!

サインインしてコメントする。

その他の回答 (0 件)

製品

リリース

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by