当前位置:网站首页>Using matlab to solve the linear optimization problem based on matlab dynamic model of learning notes _11 】 【
Using matlab to solve the linear optimization problem based on matlab dynamic model of learning notes _11 】 【
2022-08-03 23:52:00 【crooked babi】
题目:
(1)Draw the feasible domain range
question1.m
%% Mark the function
r=4;
a1=0;
a2=0;
theta=0:pi/20:2*pi;
x_1=a1+r*cos(theta);
x_2=a2+r*sin(theta);
plot(x_1,x_2);
hold on
text(2,4,'16-(x_1)^2-(x_2)^2=0','color','b'); %在坐标点(6.8,4)显示x1=7this function line
L2=[-2,-4;5,3];
plot(L2(:,1),L2(:,2));hold on %x2最大值为3
text(3,1,'2-x_1-x_2=0','color','b'); %从点L2(:,1)到点L2(:,2)
L3=[-5,0;5 0];
plot(L3(:,1),L3(:,2));hold on
text(3,0,'x_1=0','color','b')
L4=[0,-5;0,5];
plot(L4(:,1),L4(:,2));
text(0,3,'x_2=0','color','b')
grid on
%% 填充
[X1,X2]=meshgrid(0:0.01:5,0:0.01:5);%draw area
idX1=(X1.*X1+X2.*X2<=16)&(-X2+X1<=2)&(X1>=0)&(X2>=0);
X1=X1(idX1);
X2=X2(idX1);
k=convhull(X1,X2); %计算面积
h=fill(X1(k),X2(k),'g'); %绿色填充
set(h,'edgealpha',0,'facealpha',0.3) %边界,透明度
(2)利用fminconSolve separately without constraints、Constrain the optimal solution
Matlab中的fminconThe function can be used to find the minimum of a constrained nonlinear multivariate function,This is used to find the optimal solution here.
fmincon函数参考:
MatlabSolve nonlinear programs,fmincon函数的用法总结_Xiao Zhu~的博客-CSDN博客_fmincon函数用法
官方帮助文档:https://ww2.mathworks.cn/help/optim/ug/fmincon.html? searchHighlight=fmincon&s_tid=srchtitle_fmincon_1
(2.1)Unconstrained optimal solution
目标函数fun1.m:
%目标函数
function f=fun1(x)
f=(x(1)-2).^2+(x(2)-5).^2
end
Unconstrained optimal solutionquestion2_1.m:
%% Mark the function
r=4;
a1=0;
a2=0;
theta=0:pi/20:2*pi;
x_1=a1+r*cos(theta);
x_2=a2+r*sin(theta);
plot(x_1,x_2);
hold on
text(2,4,'16-(x_1)^2-(x_2)^2=0','color','b'); %在坐标点(6.8,4)显示x1=7this function line
L2=[-2,-4;5,3];
plot(L2(:,1),L2(:,2));hold on %x2最大值为3
text(3,1,'2-x_1-x_2=0','color','b'); %从点L2(:,1)到点L2(:,2)
L3=[-5,0;5 0];
plot(L3(:,1),L3(:,2));hold on
text(3,0,'x_1=0','color','b')
L4=[0,-5;0,5];
plot(L4(:,1),L4(:,2));
text(0,3,'x_2=0','color','b')
grid on
%% 填充
[X1,X2]=meshgrid(0:0.01:5,0:0.01:5);%draw area
idX1=(X1.*X1+X2.*X2<=16)&(-X2+X1<=2)&(X1>=0)&(X2>=0);
X1=X1(idX1);
X2=X2(idX1);
k=convhull(X1,X2); %计算面积
h=fill(X1(k),X2(k),'g'); %绿色填充
set(h,'edgealpha',0,'facealpha',0.3) %边界,透明度
%问题2.1主函数
options=optimset;
x0=[0;0];%给定初值
lb=[0;0];%Function lower bound
ub=[5;5];%upper limit of the function
[x,y]=fmincon('fun1',x0,[],[],[],[],lb,ub)
%加标注
text(-3,2,'X*(1)=2.0000')
text(-2.1,1.6,'4.9994')
text(-3,1.2,'f(X*(1))=4.1847e-07')
(2.2)Constrain the optimal solution
目标函数fun1.m:
%目标函数
function f=fun1(x)
f=(x(1)-2).^2+(x(2)-5).^2
end
Nonlinear constraint functionfun2.m:
%Nonlinear constraint function
function[g,h]=fun2(x)
%matlab中默认g<=0,If it does not correspond, it needs to be reversed
g(1)=-16+x(2).^2+x(1).^2;
g(2)=-2+x(1)+x(2);
h=[];%Use null instead when there is no equality constraint
end
Unconstrained optimal solutionquestion2_2.m:
%% Mark the function
r=4;
a1=0;
a2=0;
theta=0:pi/20:2*pi;
x_1=a1+r*cos(theta);
x_2=a2+r*sin(theta);
plot(x_1,x_2);
hold on
text(2,4,'16-(x_1)^2-(x_2)^2=0','color','b'); %在坐标点(6.8,4)显示x1=7this function line
L2=[-2,-4;5,3];
plot(L2(:,1),L2(:,2));hold on %x2最大值为3
text(3,1,'2-x_1-x_2=0','color','b'); %从点L2(:,1)到点L2(:,2)
L3=[-5,0;5 0];
plot(L3(:,1),L3(:,2));hold on
text(3,0,'x_1=0','color','b')
L4=[0,-5;0,5];
plot(L4(:,1),L4(:,2));
text(0,3,'x_2=0','color','b')
grid on
%% 填充
[X1,X2]=meshgrid(0:0.01:5,0:0.01:5);%draw area
idX1=(X1.*X1+X2.*X2<=16)&(-X2+X1<=2)&(X1>=0)&(X2>=0);
X1=X1(idX1);
X2=X2(idX1);
k=convhull(X1,X2); %计算面积
h=fill(X1(k),X2(k),'g'); %绿色填充
set(h,'edgealpha',0,'facealpha',0.3) %边界,透明度
%问题2.2主函数
options=optimset;
x0=[0;0];%给定初值
lb=[0;0];%Function lower bound
ub=[5;5];%upper limit of the function
[x,y]=fmincon('fun1',x0,[],[],[],[],lb,ub,'fun2')
%加标注
text(-3,2,'X*(2)=0.0000')
text(-2.1,1.6,'2.0000')
text(-3,1.2,'f(X*(2))=13')
(3)Linearly constrained optimal solution
目标函数fun1.m:
%目标函数
function f=fun1(x)
f=(x(1)-2).^2+(x(2)-5).^2
end
Linear constraint functionfun3.m:
%Linear constraint function
function[g,h]=fun3(x)
%matlab中默认g<=0,If it does not correspond, it needs to be reversed
g(1)=-16+x(2).^2+x(1).^2;
g(2)=-2+x(1)+x(2);
%线性约束条件
h=x(1)-x(2);
end
Linearly constrained optimal solutionquestion3.m:
%% Mark the function
r=4;
a1=0;
a2=0;
theta=0:pi/20:2*pi;
x_1=a1+r*cos(theta);
x_2=a2+r*sin(theta);
plot(x_1,x_2);
hold on
text(2,4,'16-(x_1)^2-(x_2)^2=0','color','b'); %在坐标点(6.8,4)显示x1=7this function line
L2=[-2,-4;5,3];
plot(L2(:,1),L2(:,2));hold on %x2最大值为3
text(3,1,'2-x_1-x_2=0','color','b'); %从点L2(:,1)到点L2(:,2)
L3=[-5,0;5 0];
plot(L3(:,1),L3(:,2));hold on
text(3,0,'x_1=0','color','b')
L4=[0,-5;0,5];
plot(L4(:,1),L4(:,2));
text(0,3,'x_2=0','color','b')
grid on
%% 填充
[X1,X2]=meshgrid(0:0.01:5,0:0.01:5);%draw area
idX1=(X1.*X1+X2.*X2<=16)&(-X2+X1<=2)&(X1>=0)&(X2>=0);
X1=X1(idX1);
X2=X2(idX1);
k=convhull(X1,X2); %计算面积
h=fill(X1(k),X2(k),'g'); %绿色填充
set(h,'edgealpha',0,'facealpha',0.3) %边界,透明度
%问题3主函数
options=optimset;
x0=[0;0];%给定初值
lb=[0;0];%Function lower bound
ub=[5;5];%upper limit of the function
[x,y]=fmincon('fun1',x0,[],[],[],[],lb,ub,'fun3')
%加标注
text(-3,2,'X*(3)=1.0000')
text(-2.1,1.6,'1.0000')
text(-3,1.2,'f(X*(3))=17.0000')
边栏推荐
猜你喜欢
rosbridge-WSL2 && carla-win11
[Miscellaneous] How to install the specified font into the computer and then use the font in the Office software?
Why Flutter Flutter of tutorials is the best choice for business?
【OpenCV图像处理】 图像拼接技术
Creo 9.0创建几何点
[Paper Reading] TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
Pytest学习-skip/skipif
2022-08-03:以下go语言代码输出什么?A:2;B:3;C:1;D:0。 package main import “fmt“ func main() { slice := []i
POE交换机全方位解读(下)
初始 List 接口
随机推荐
CAS: 178744-28-0, mPEG-DSPE, DSPE-mPEG, methoxy-polyethylene glycol-phosphatidylethanolamine supply
通过whl安装第三方包
【LeetCode】最长回文子序列(动态规划)
浅谈我国产业园区未来的发展方向
【LeetCode】最长公共子序列(动态规划)
rosbridge-WSL2 && carla-win11
Walk the Maze BFS
rosbridge-WSL2 && carla-win11
关于mnn模型输出的数据杂乱无章问题
Salesforce的中国区业务可能出现新变化,传言可能正在关闭
Prometheus监控Harbor(二进制版)
用栈实现队列
禾匠编译错误记录
2022-08-03:以下go语言代码输出什么?A:2;B:3;C:1;D:0。 package main import “fmt“ func main() { slice := []i
SRE运维解密-什么是SRE:DevOps模型的具体实践!
栈的压入、弹出序列
简单了解下 TCP,学习握手和挥手以及各种状态到底是怎么样的
Unity2021发布WebGL雾效消失问题
rsync 基础用法
Kotlin - extension functions and operator overloading