题目:
(1)画出可行域范围
question1.m
%% 标出函数
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=7这个函数线
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);%画出区域
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)利用fmincon分别求解无约束、约束最优解
Matlab中的fmincon函数可以用于求解带约束的非线性多变量函数的最小