Error using fmincon MATLAB function in simulink

3 ビュー (過去 30 日間)
ayman mounir
ayman mounir 2021 年 1 月 6 日
コメント済み: Kenneth Chijioke 2025 年 2 月 25 日
Hallo everyone,
I have this optimisation code which is simply maximize the volume of a cube within a surface are does not exceel 10 cm. but I have to run it in Simulink using MATLAB function block (as in the fig). there are two errors:
1- if i run the code as shown below: FMINCON requires 10 input arguments. Must set option Algorithm to 'sqp' and pass to solver.
2-if add coder.extrinsic('fmincon') below function y = fcn(x): Function handles cannot be passed to extrinsic functions.
Thanks in adevance
function y = fcn(x)
% L_G=1;
% W_G=1;
% H_G=1;
% X0=[ L_G W_G H_G 5 6 ]
X0=[x];
xopt=fmincon(@objective,X0,[],[],[],[],[],[],@contraints)
v=calcvolume(xopt)
a=area_surf(xopt)
function volume= calcvolume(x)
L=x(1);
W=x(2);
H=x(3);
volume= L*W*H
end
function surface= area_surf(x)
L=x(1);
W=x(2);
H=x(3);
surface = 2*L*H + 2*L*W + 2*W*H;
end
function obj =objective(x)
obj=-calcvolume(x)
end
function [c, ceq]= contraints(x)
c=area_surf(x)-10
ceq=[];
end

採用された回答

Jesús Zambrano
Jesús Zambrano 2021 年 1 月 6 日
編集済み: Jesús Zambrano 2021 年 1 月 6 日
Hi Ayman,
Could you try the following:
function [v,a] = fcn(x)
myopts = optimoptions('fmincon','Algorithm','sqp');
xopt=fmincon(@objective,X0,[],[],[],[],[],[],@(x) contraints(x),myopts);
v=calcvolume(xopt);
a=area_surf(xopt);
I just specified v and a as the two outputs of the fcn function. Then, I used optimoptons command to create/modify optimization options and applied it to the fmincon. I got a=10 and v= 2.1517. See image enclosed.
Hope it helps!
  2 件のコメント
ayman mounir
ayman mounir 2021 年 1 月 10 日
Hello Jesus,
thank you very much. it works.
Kenneth Chijioke
Kenneth Chijioke 2025 年 2 月 25 日
i have similar issues . i try to simulate fmincon in simulink but i am stuck . i make use of pv and battery system . then my non linear states predict are passed to my mpc controller containing my optimization , cost function and constrainsts but my output of my mpc is not changing it is just constant . that means my fmincon is not working

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGeneral Applications についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by