Function 'fmincon' not supported for code generation

조회 수: 29 (최근 30일)
Anuschan Albrecht
Anuschan Albrecht 2021년 5월 8일
댓글: Walter Roberson 2021년 5월 12일
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
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일
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월 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개)

카테고리

Help CenterFile Exchange에서 Manual Performance Optimization에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by