이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Optimization with Fmincon command in Simulink
조회 수: 3 (최근 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
Walter Roberson
2022년 9월 26일
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에서 Manual Performance Optimization에 대해 자세히 알아보기
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 (한국어)