• 利用matlab求解线性优化问题【基于matlab的动力学模型学习笔记_11】


    题目:

     (1)画出可行域范围

    question1.m

    1. %% 标出函数
    2. r=4;
    3. a1=0;
    4. a2=0;
    5. theta=0:pi/20:2*pi;
    6. x_1=a1+r*cos(theta);
    7. x_2=a2+r*sin(theta);
    8. plot(x_1,x_2);
    9. hold on
    10. text(2,4,'16-(x_1)^2-(x_2)^2=0','color','b'); %在坐标点(6.8,4)显示x1=7这个函数线
    11. L2=[-2,-4;5,3];
    12. plot(L2(:,1),L2(:,2));hold on %x2最大值为3
    13. text(3,1,'2-x_1-x_2=0','color','b'); %从点L2(:,1)到点L2(:,2)
    14. L3=[-5,0;5 0];
    15. plot(L3(:,1),L3(:,2));hold on
    16. text(3,0,'x_1=0','color','b')
    17. L4=[0,-5;0,5];
    18. plot(L4(:,1),L4(:,2));
    19. text(0,3,'x_2=0','color','b')
    20. grid on
    21. %% 填充
    22. [X1,X2]=meshgrid(0:0.01:5,0:0.01:5);%画出区域
    23. idX1=(X1.*X1+X2.*X2<=16)&(-X2+X1<=2)&(X1>=0)&(X2>=0);
    24. X1=X1(idX1);
    25. X2=X2(idX1);
    26. k=convhull(X1,X2); %计算面积
    27. h=fill(X1(k),X2(k),'g'); %绿色填充
    28. set(h,'edgealpha',0,'facealpha',0.3) %边界,透明度

     (2)利用fmincon分别求解无约束、约束最优解

    Matlab中的fmincon函数可以用于求解带约束的非线性多变量函数的最小值,这里用来求解此处的最优解。

    fmincon函数参考:

    Matlab求解非线性规划,fmincon函数的用法总结_小朱同学~的博客-CSDN博客_fmincon函数用法

    官方帮助文档:https://ww2.mathworks.cn/help/optim/ug/fmincon.html? searchHighlight=fmincon&s_tid=srchtitle_fmincon_1

    (2.1)无约束最优解

    目标函数fun1.m:

    1. %目标函数
    2. function f=fun1(x)
    3. f=(x(1)-2).^2+(x(2)-5).^2
    4. end

    无约束最优解question2_1.m:

    1. %% 标出函数
    2. r=4;
    3. a1=0;
    4. a2=0;
    5. theta=0:pi/20:2*pi;
    6. x_1=a1+r*cos(theta);
    7. x_2=a2+r*sin(theta);
    8. plot(x_1,x_2);
    9. hold on
    10. text(2,4,'16-(x_1)^2-(x_2)^2=0','color','b'); %在坐标点(6.8,4)显示x1=7这个函数线
    11. L2=[-2,-4;5,3];
    12. plot(L2(:,1),L2(:,2));hold on %x2最大值为3
    13. text(3,1,'2-x_1-x_2=0','color','b'); %从点L2(:,1)到点L2(:,2)
    14. L3=[-5,0;5 0];
    15. plot(L3(:,1),L3(:,2));hold on
    16. text(3,0,'x_1=0','color','b')
    17. L4=[0,-5;0,5];
    18. plot(L4(:,1),L4(:,2));
    19. text(0,3,'x_2=0','color','b')
    20. grid on
    21. %% 填充
    22. [X1,X2]=meshgrid(0:0.01:5,0:0.01:5);%画出区域
    23. idX1=(X1.*X1+X2.*X2<=16)&(-X2+X1<=2)&(X1>=0)&(X2>=0);
    24. X1=X1(idX1);
    25. X2=X2(idX1);
    26. k=convhull(X1,X2); %计算面积
    27. h=fill(X1(k),X2(k),'g'); %绿色填充
    28. set(h,'edgealpha',0,'facealpha',0.3) %边界,透明度
    29. %问题2.1主函数
    30. options=optimset;
    31. x0=[0;0];%给定初值
    32. lb=[0;0];%函数下限
    33. ub=[5;5];%函数上限
    34. [x,y]=fmincon('fun1',x0,[],[],[],[],lb,ub)
    35. %加标注
    36. text(-3,2,'X*(1)=2.0000')
    37. text(-2.1,1.6,'4.9994')
    38. text(-3,1.2,'f(X*(1))=4.1847e-07')

     (2.2)约束最优解

    目标函数fun1.m:

    1. %目标函数
    2. function f=fun1(x)
    3. f=(x(1)-2).^2+(x(2)-5).^2
    4. end

    非线性约束条件函数fun2.m:

    1. %非线性约束条件函数
    2. function[g,h]=fun2(x)
    3. %matlab中默认g<=0,若不对应需取反
    4. g(1)=-16+x(2).^2+x(1).^2;
    5. g(2)=-2+x(1)+x(2);
    6. h=[];%没有等式约束的时候用空值代替
    7. end

    无约束最优解question2_2.m:

    1. %% 标出函数
    2. r=4;
    3. a1=0;
    4. a2=0;
    5. theta=0:pi/20:2*pi;
    6. x_1=a1+r*cos(theta);
    7. x_2=a2+r*sin(theta);
    8. plot(x_1,x_2);
    9. hold on
    10. text(2,4,'16-(x_1)^2-(x_2)^2=0','color','b'); %在坐标点(6.8,4)显示x1=7这个函数线
    11. L2=[-2,-4;5,3];
    12. plot(L2(:,1),L2(:,2));hold on %x2最大值为3
    13. text(3,1,'2-x_1-x_2=0','color','b'); %从点L2(:,1)到点L2(:,2)
    14. L3=[-5,0;5 0];
    15. plot(L3(:,1),L3(:,2));hold on
    16. text(3,0,'x_1=0','color','b')
    17. L4=[0,-5;0,5];
    18. plot(L4(:,1),L4(:,2));
    19. text(0,3,'x_2=0','color','b')
    20. grid on
    21. %% 填充
    22. [X1,X2]=meshgrid(0:0.01:5,0:0.01:5);%画出区域
    23. idX1=(X1.*X1+X2.*X2<=16)&(-X2+X1<=2)&(X1>=0)&(X2>=0);
    24. X1=X1(idX1);
    25. X2=X2(idX1);
    26. k=convhull(X1,X2); %计算面积
    27. h=fill(X1(k),X2(k),'g'); %绿色填充
    28. set(h,'edgealpha',0,'facealpha',0.3) %边界,透明度
    29. %问题2.2主函数
    30. options=optimset;
    31. x0=[0;0];%给定初值
    32. lb=[0;0];%函数下限
    33. ub=[5;5];%函数上限
    34. [x,y]=fmincon('fun1',x0,[],[],[],[],lb,ub,'fun2')
    35. %加标注
    36. text(-3,2,'X*(2)=0.0000')
    37. text(-2.1,1.6,'2.0000')
    38. text(-3,1.2,'f(X*(2))=13')

     (3)线性约束最优解

    目标函数fun1.m:

    1. %目标函数
    2. function f=fun1(x)
    3. f=(x(1)-2).^2+(x(2)-5).^2
    4. end

    线性约束条件函数fun3.m:

    1. %线性约束条件函数
    2. function[g,h]=fun3(x)
    3. %matlab中默认g<=0,若不对应需取反
    4. g(1)=-16+x(2).^2+x(1).^2;
    5. g(2)=-2+x(1)+x(2);
    6. %线性约束条件
    7. h=x(1)-x(2);
    8. end

    线性约束最优解question3.m:

    1. %% 标出函数
    2. r=4;
    3. a1=0;
    4. a2=0;
    5. theta=0:pi/20:2*pi;
    6. x_1=a1+r*cos(theta);
    7. x_2=a2+r*sin(theta);
    8. plot(x_1,x_2);
    9. hold on
    10. text(2,4,'16-(x_1)^2-(x_2)^2=0','color','b'); %在坐标点(6.8,4)显示x1=7这个函数线
    11. L2=[-2,-4;5,3];
    12. plot(L2(:,1),L2(:,2));hold on %x2最大值为3
    13. text(3,1,'2-x_1-x_2=0','color','b'); %从点L2(:,1)到点L2(:,2)
    14. L3=[-5,0;5 0];
    15. plot(L3(:,1),L3(:,2));hold on
    16. text(3,0,'x_1=0','color','b')
    17. L4=[0,-5;0,5];
    18. plot(L4(:,1),L4(:,2));
    19. text(0,3,'x_2=0','color','b')
    20. grid on
    21. %% 填充
    22. [X1,X2]=meshgrid(0:0.01:5,0:0.01:5);%画出区域
    23. idX1=(X1.*X1+X2.*X2<=16)&(-X2+X1<=2)&(X1>=0)&(X2>=0);
    24. X1=X1(idX1);
    25. X2=X2(idX1);
    26. k=convhull(X1,X2); %计算面积
    27. h=fill(X1(k),X2(k),'g'); %绿色填充
    28. set(h,'edgealpha',0,'facealpha',0.3) %边界,透明度
    29. %问题3主函数
    30. options=optimset;
    31. x0=[0;0];%给定初值
    32. lb=[0;0];%函数下限
    33. ub=[5;5];%函数上限
    34. [x,y]=fmincon('fun1',x0,[],[],[],[],lb,ub,'fun3')
    35. %加标注
    36. text(-3,2,'X*(3)=1.0000')
    37. text(-2.1,1.6,'1.0000')
    38. text(-3,1.2,'f(X*(3))=17.0000')

     

  • 相关阅读:
    行业追踪,2023-10-26
    jvm中对象创建、内存布局以及访问定位
    Rust 流程控制
    QT DAY7
    DPDK基础组件二(igb_uio、kni、rcu)
    【Flutter】built_value 解决 Flutter 中的不可变性问题
    Arduino驱动ML8511紫外线传感器(光照传感器篇)
    Vue3.x使用vuex进行页面间通信
    初识进程以及父子进程
    〖Python WEB 自动化测试实战篇⑮〗 实战 - 自动化测试的持续集成
  • 原文地址:https://blog.csdn.net/L1234X/article/details/126052235