Optimization with Fmincon command in Simulink
1 次查看(过去 30 天)
显示 更早的评论
Farshid R
2022-9-26
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1141895/image.png)
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 "
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1141900/image.png)
25 个评论
Sam Chak
2022-9-27
Hi @Farshid R
Just trying to understand the system first.
Your two systems have exactly the same dynamics given by
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1137080/image.png)
where the desired input
is designed as
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1137085/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1137090/image.png)
There is no free variables for tuning
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1137095/image.png)
Substituting
into the dynamics yields
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1137100/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1137105/image.png)
Which specific part of the system do you want to "optimize"?
Farshid R
2022-9-27
编辑:Farshid R
2022-9-27
Hello
Thank you for putting aside the time to answer my question.
In fact, I want the difference between the states of the two systems to be between 11.5 and 1.5 according to my constraints. That is, the value of u obtained from fmincon should be obtained and given to the system.
As for what you wrote, it is true that there is a k1CAx1 and k2CAx2 coefficient for each.(k1=2,k2=8)
So, I want to optimize the one based on the modes, so that it is considered the desired value.
How do you think I should do it?
thank you
Sam Chak
2022-9-27
编辑:Sam Chak
2022-9-27
@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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1137250/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1137255/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1137260/image.png)
The states
will converge to zero from an arbitrary set of initial conditions
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1137265/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1137270/image.png)
If system Q with
, then the states
will also converge to zero.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1137275/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1137280/image.png)
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1137285/image.png)
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
2022-9-27
Dear Sam,
I sincerely appreciate your time to help me.
I didn't understand where the optimization part of the modified code is now.
The system is a fractional order, that's why the fractional order toolbox was used.
I have attached simulink. Optimization according to the cost function and constraints that depend on the states and dynamics of the system.
Sam Chak
2022-9-28
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1138540/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1138545/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1138550/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1138555/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1138560/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1138565/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1138570/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1138575/image.png)
which maybe wrong.
Farshid R
2022-9-28
Thank you very much for your reply.
I entered the relations exactly for you, maybe this way I have expressed my meaning better. I am waiting for your positive answer.
Best regards,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1138730/image.png)
Sam Chak
2022-9-28
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1139210/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1139215/image.png)
Perhaps, you can try using fmincon to return
at each sample time.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1139220/image.png)
Farshid R
2022-9-28
Thank you for all your advice.
You are exactly right. I want to justify this with the constraints that I have.
How do you think I should do it?
Thank you
Sam Chak
2022-9-30
编辑:Sam Chak
2022-9-30
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1140745/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1140750/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1140755/image.png)
Also, I'm not sure if I interpret the objective function correctly:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1140760/image.png)
that
should be found inside the solution space defined by the 2-dimensional manifold
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1140750/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1140745/image.png)
Think you may need to solve optimization problem at each time step over a time interval. Does it sound like Dynamic Programming?
Farshid R
2022-9-30
Maybe I should attach a part of the article for you. I mean:
"Decentralized Connectivity Maintenance with Time Delays using Control Barrier Functions "
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1140775/image.png)
Sam Chak
2022-10-1
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
2022-10-1
Thank you very much for taking the time to answer my questions. I am sending him a message, I hope to get a positive answer from him.
Best regards,
Farshid R
2022-10-1
But he said in his profile that I don't answer questions. Do you know someone who is familiar with this topic?
Sam Chak
2022-10-1
编辑:Sam Chak
2022-10-1
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.
Sam Chak
2022-10-1
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.
Bruno Luong
2022-10-1
@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.
Farshid R
2022-10-2
编辑:Farshid R
2022-10-2
Hello @ Bruno Luong
Actually, when I want to do optimization in Simulink, but whatever I do, I can't do optimization in Simulink. I posted an article with the same title and I even posted a formula from that article to be clear. It's actually similar to the answer to this question, but I couldn't adapt it to my work:
If I want to write the simulation in code, how can I express it for the fractional order system?
I don't know how to enter it in the Fmincon command. can you guide.
Bruno Luong
2022-10-2
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
2022-10-2
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)
https://www.mathworks.com/matlabcentral/answers/65202-optimization-calling-fmincon-in-simulink-embedded-block
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)