optimization: calling fmincon in Simulink (embedded block)

Hello,
I am going to implement a model predictive controller in Simulink. Therefore I need to solve a constrained optimization problem, thus finding the vector that mimimises a certain multi-variable function, subject to constraints. (I do know there is a MPC block in Simulink, but I need to implement it myself as part of a bigger Simulink model).
In Matlab this is possible using fmincon. So I tried using an embedded Matlab block in Simulink where fmincon was called, but this does not work. I also tried "coder.extrinsic('fmincon')" code but that did not work.
Can anybody tell me if it is possible to use fmincon in a SIMULINK model (thus not working in a .m Matlab file) or if there are alternatives? Or do I have to try to implement the algebra behind this function (quadratic programming etc.) in Simulink myself?
All help appreciated! Maarten

1 个评论

I know this is from a while ago, but I would like to +1 this question. I have a very similar problem to Maarten.
Like Maarten I tried using coder.extrinsic('fmincon') from within the MATLAB Function block within simulink; but it is then not possible to pass handles to an extrinsic function (which is needed to be able to run fmincon).
Is there another/better way to perform constrained optimisation within a Simulink model (i.e. where it needs to be run once per time-step). I am familiar with using constrained optimisation of Simulink model parameters (i.e. where Simulink is called once per objective function evaluation); but here we seek to run the Simulink model only once, but run the optimisation once per time-step.
The approach Maarten and I have taken seems like the 'obvious' one but doesn't work. Any pointers gratefully received.
- Khalid.

请先登录,再进行评论。

回答(1 个)

I will post the same answer I gave on Stack Overflow on the same topic
You need to call the optimal problem function with coder.extrinsic.
Instead of using coder.extrinsic on the fmincon function, I usually write a wrapper for the optimization problem that I have to solve as a .m file function for Matlab (namely opt_problem) and declare coder.extrinsic('opt_problem') in the simulink Matlab function. I'll give you a simple example:
Consider this Simulink "model", in which at each integration step I want to solve a linear regression problem on some generated data. The optimization problem is something in the form:
minimize (y - m x - q)²
subject to 0 ≤ m ≤ 1
0 q ≤ 1
The scheme is really simple, bet the regressor calls fmincon:
Let's see inside the regressor:
function [m, q] = regressor(xs, ys, mic, qic)
coder.extrinsic('opt_problem'); % <- Informing the Coder
m = 0;
q = 0;
[m, q] = opt_problem(xs, ys, mic, qic); % <- Optimal problem wrapper call
end
this function is only a wrapper for an external function `opt_problem`. Let'see it (it has two functions inside):
function [m, q] = opt_problem(xs, ys, mic, qic)
fmincon_target = @(mq)(target(mq, xs, ys));
mq = fmincon(fmincon_target, [mic; qic], [], [], [], [], [0; 0], [1; 1]);
m = mq(1);
q = mq(2);
end
function r = target(mq, xs, ys)
r = norm(ys - xs.*mq(1) - mq(2));
end
and that's all. As you can see in the picture, the scheme runs and the solution are the m, q parameters (in the two displays) that minimize the target function while respecting the constraints (m = 1.2 → m_opt = 1).

6 个评论

Hi @Matteo Ragni, I have similar problem, but the difference is that I am using m and q values in the rest of my simulation as gains, where the cost function r is dependable on the results from there (r is indirectly dependant on m and q). So, my question is how do I define m and q to be updated in each optimization run in the rest of the Simulink file? and should I define initial values for them?
Hi Matteo, what is inside the Linear Trasformation with Noise block??
@Matteo Ragni how to add constraint function on m and q please help me writing code
Hello @ manish kumar
Were you able to solve your problem?
I have a similar problem.
What is meant by external function , where it is placed ?
Variable mq is being used before it is defined. I feel like I am missing something.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 LEGO MINDSTORMS EV3 Hardware 的更多信息

提问:

2013-2-27

评论:

2025-1-3

Community Treasure Hunt

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

Start Hunting!

Translated by