fmincon in MATLAB Function block (SIMULINK)

조회 수: 1 (최근 30일)
jaskier222
jaskier222 2017년 7월 13일
답변: jaskier222 2017년 7월 13일
Hello! I'd like to do MPC controller by using MATLAB. Could u help me with my problem? I try to use 'fmincon' in MATLAB Function block in simulink. There is my code in Function block in simulink:
function u = fcn(unused_parameters)
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
x0 = [-1,2];
A = [1,2];
b = 1;
coder.extrinsic('fmincon');
x = fmincon(fun,x0,A,b)
u=x(1,1)
And Matlab gave me this error: "Function handles cannot be passed to extrinsic functions.
Function 'MPC' (#23.171.174), line 7, column 13: "fun" Launch diagnostic report."
Thanks for your suggestions!

채택된 답변

Sebastian Castro
Sebastian Castro 2017년 7월 13일
As the error message suggests, you can't use function handles (i.e. the fmincon cost function) as an argument to an extrinsic function, unless that function handle is declared in an extrinsic function itself.
You can do one of 2 things:
  1. Wrap up all of that code in yet another function, let's call it myOptimization.m. Then, declare that whole function as extrinsic and call it from your MATLAB Function block.
  2. Given your cost function looks like a pretty run-of-the-mill MPC cost function, have you thought about just using the MPC Toolbox block? https://www.mathworks.com/help/mpc/ref/mpccontroller.html
- Sebastian

추가 답변 (1개)

jaskier222
jaskier222 2017년 7월 13일
Hello Sebastian, thanks 4 your answer. I did my own linear MPC with state space model without Toolbox. There I used another function - 'quadprog' and it works with Quadratic programming. Now I'd like to do nonlinear MPC, so I try your 2. point.
I hope it works!

카테고리

Help CenterFile Exchange에서 Model Predictive Control Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by