Optimization with Fmincon command in Simulink
Mostra commenti meno recenti
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 "

25 Commenti
Farshid R
il 26 Set 2022
Sam Chak
il 27 Set 2022
Hi @Farshid R
Just trying to understand the system first.
Your two systems have exactly the same dynamics given by

where the desired input
is designed as
There is no free variables for tuning
.
Substituting
into the dynamics yields
.Which specific part of the system do you want to "optimize"?
@Farshid R, Thanks for your reply. I'm not sure if I understand your optimization objective.
Say, here is system P and the input
is given by
The states
will converge to zero from an arbitrary set of initial conditions
.
If system Q with
, then the states
will also converge to zero.
tspan = [0 10];
x0 = [1 0.5]; % initial values
[t, x] = ode45(@odefcn, tspan, x0);
plot(t, x), grid on, xlabel({'$t$'}, 'interpreter', 'latex')
legend({'$x_{1}(t)$', '$x_{2}(t)$'}, 'interpreter', 'latex', 'fontsize', 14)
function xdot = odefcn(t, x)
A = [-2 1.2; -2 -4];
B = eye(2);
k1 = 2;
K1 = [-2 1.2; -2 -4] + k1*eye(2);
u1 = - K1*x;
xdot = A*x + B*u1;
end
Farshid R
il 27 Set 2022
Farshid R
il 28 Set 2022
Sam Chak
il 28 Set 2022
Hi @Farshid R
Thanks for your reply, but I don't understand your systems as mentioned in my comment.
My advive is that you don't worry about the optimization at this stage (because it has not become a 'problem' yet).
Try mathematically describing the desired output that you want to get as clearly as possible.
If you don't, people can interpret wrongly. For example, you mentioned:
"the difference between the states of the two systems to be between 11.5 and 1.5"
and I interpreted that as follow:








which maybe wrong.
Farshid R
il 28 Set 2022
Sam Chak
il 28 Set 2022
I'm trying to break down your problem into pieces.
So you want to find a unique array of
at which the objective function
takes its minimum value subject to the constraints?
takes its minimum value subject to the constraints?Perhaps, you can try using fmincon to return
at each sample time.
Farshid R
il 28 Set 2022
Farshid R
il 29 Set 2022
Hi @Farshid R
I think I get what you want, more or less from the mathematical aspect, but I have never done it before.
It does not make sense to me that you have a desired structured input
and yet you want to force-finding a different non-structured input
that gives the
and yet you want to force-finding a different non-structured input
.Also, I'm not sure if I interpret the objective function correctly:

that
should be found inside the solution space defined by the 2-dimensional manifold
.
.Think you may need to solve optimization problem at each time step over a time interval. Does it sound like Dynamic Programming?
Farshid R
il 30 Set 2022
Sam Chak
il 1 Ott 2022
Thanks for showing the paper. However, the original problem has evolved to something that I'm unfamiliar with.
I'm unsure, but I think this maybe belong to the class of the Consensus Optimization problem.
Farshid R
il 1 Ott 2022
Farshid R
il 1 Ott 2022
John D'Errico is a very knowledgeable person in Advanced Math and MATLAB. Perhaps you can visit the optimization-related Answers under his profile, and kindly asks for his advice and drop a link in the Comment section. You can also find some of his custom functions for solving optimization problems in File Exchange:
Some of the Advanced Math experts I know as @Walter Roberson, @Matt J, @Torsten, @Bruno Luong. All of them are helpful and can provide good advices on how to attack the problem when you formulate the math problem in a clear and concise manner.
Farshid R
il 1 Ott 2022
Sam Chak
il 1 Ott 2022
If possible, please edit your question to re-formulate the optimization problem in a clear and concise manner.
Out of courtesy, you definitely do not want the Experts to scroll down and compel them read all the Comments to understand your math problem.
Farshid R
il 1 Ott 2022
Bruno Luong
il 1 Ott 2022
@Farshid R sorry, I can't help you, I read the thread few times and I don't understand clearly what you want to solve and where you are stuck. Furthermore I don't know simulink.
Bruno Luong
il 2 Ott 2022
I'm not familiar with "algebric connectivity", "CBF". What is "U" set?
In your model in the top,there is a fractional derivative I don' see it implemented anywhere, the t variable does not seem to present anywhere in your code. Why you set U dersired to something dependent to x? What is the known parameters what is not in your problem? What does exactly means "I can't do optimization in Simulink"? (I don't know simulink)
Sorry but I can't make a head and tail of this question. You might start to a new question again. I think I'm not alone to find the question nit not clearly described.
Farshid R
il 2 Ott 2022
Risposte (0)
Categorie
Scopri di più su System Composer in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


