# Optimization with Fmincon command in Simulink

5 views (last 30 days)
Farshid R on 26 Sep 2022
Commented: Farshid R on 2 Oct 2022
Hello friends,
I am facing an errors when I try to use fmincon command in simulink. I have attached the simulink and the errors photo.
Corresponding block code for both system dynamics
function xfd1 = fcn(x1,u1)
A=[-2 1.2;-2 -4];
B=[1 0;0 1];
xfd1=A*x1+B*u1;
function xfd2 = fcn(x2,u2)
A=[-2 1.2;-2 -4];
B=[1 0;0 1];
xfd2=A*x2+B*u2;
The corresponding code of the u_desired block
function [u_desired1,u_desired2 ] = fcn(x1,x2)
A=[-2 1.2;-2 -4];
C=eye(2);
%%
u_desired1=(-C*A*x1);
u_desired2=(-C*A*x2);
Optimization related code
function [ u1,u2] = fcn(u_desired1,u_desired2)
coder.extrinsic('fmincon')
global x1 x2
u=zeros(4, 1);
x0 = zeros(4, 1);
optimoptions('fmincon','Display','iter','Algorithm','sqp');
u = fmincon(@fun,x0,[],[],[],[],[],[],@nonl,options);
u1=[u(1);u(2)];
u2=[u(3);u(4)];
function f=fun(u_desired)
f = (0.5*(( u-u_desired)'*(u- u_desired)));
function [c,ceq] = nonl(u_desired)
global x1 x2
x11=[x1(1);x1(2)];
x12=[x2(1);x2(2)];
ceq = [];
c = [
norm(x11 - x12) - 2*0.5+10+0.5
-(norm(x11 - x12)) + 2*0.5+0.5];
%
Maybe I should attach a part of the article for you. I mean:
"Decentralized Connectivity Maintenance with Time Delays using Control Barrier Functions "
Farshid R on 2 Oct 2022
Hello @ Bruno Luong
Thank you very much for taking the time to answer me.
U is the control input that should be given to the system. which is obtained from the solving optimization.
The U_desired is obtained from the designed controller and must be dependent on x, and the system is asked to determine. the U_desired with the constraints of the U value for the system according to the U_desired.
The parameters are obtained from the x variables that are considered from the dynamics of the system and all the parameters are known.
It means that when I run the Simulink function, it encounters errors. I thought my question might be vague, that's why I sent you a link to a question that had the same problem as me.
optimization: calling fmincon in Simulink (embedded block)

### Categories

Find more on Parameter Estimation in Help Center and File Exchange

R2018b

### Community Treasure Hunt

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

Start Hunting!