编程实现二维最优化问题图解法(最优化附加1)

说明

这是最优化课程第一次附加作业

依赖

MATLAB2015

题目

运行效果

编程实现二维最优化问题图解法(最优化附加1)_第1张图片
momentum.jpg

momentum.m

que1.m

que1
第一题
警告: Gradient must be provided for trust-region algorithm; using quasi-newton algorithm instead.
In fminunc (line 397)
In que1 (line 8)

Initial point is a local minimum.

Optimization completed because the size of the gradient at the initial point
is less than the default value of the optimality tolerance.

最优点为

x =

   3              4       

最优值为

fval =

   0   

que2.m

que2
第二题

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.

最优点为

x =

  15/4            5/4     

最优值为

fval =

  65/8 

que3.m

que3
第三题
> In backsolveSys
In solveAugSystem
In leastSquaresLagrangeMults
In barrier
In fmincon (line 797)
In que3 (line 12)
警告: 矩阵接近奇异值,或者缩放错误。结果可能不准确。RCOND = 2.217408e-16。
> In backsolveSys
In solveAugSystem
In leastSquaresLagrangeMults
In nlpStopTest
In barrier
In fmincon (line 797)
In que3 (line 12)
警告: 矩阵接近奇异值,或者缩放错误。结果可能不准确。RCOND = 2.217408e-16。
> In backsolveSys
In solveAugSystem
In normalCauchyStep
In normalStep
In computeTrialStep
In barrier
In fmincon (line 797)
In que3 (line 12)
警告: 矩阵接近奇异值,或者缩放错误。结果可能不准确。RCOND = 2.217408e-16。


程序

momentum.m

%画图
%2.约束条件
x1=[-1:0.5:10];
x2=x1-5/2;
plot(x1,x2);
hold on;
x2=-x1+5;
plot(x1,x2);
hold on;
x1=[-1,10];
x2=[0,0];
plot(x1,x2);
hold on;
x1=[0,0];
x2=[-1,10];
plot(x1,x2);
hold on;
%3.可行域
[X1,X2]=meshgrid(-1:0.1:9,-1:0.1:9);
idx=(-X1+X2<=-5/2)&(X1+X2<=5)&(X1>=0)&(X2>=0);
x1=X1(idx);%行重复数量
x2=X2(idx);%列匹配行
k=convhull(x1,x2);
fill(x1(k),x2(k),'c')
title('可行域')
axis equal
%4.目标函数等值线
[x1,x2]=meshgrid(-2:0.1:8,-2:0.1:8);
target_f=(x1-3).^2+(x2-4).^2;
[C,h]=contour(x1,x2,target_f);
set(h,'ShowText','on','TextStep',get(h,'LevelStep'))
hold on;
%5.最优解
%见另三个文件
%1.坐标系
axis on;grid on;axis([-2,7,-2,7]);
xlabel('x1');ylabel('x2');

que1.m

%(1)
disp('第一题');
format rat;

fun=@(x)(x(1)-3)^2+(x(2)-4)^2;
x0=[3,4];

[x,fval]=fminunc(fun,x0);

disp('最优点为');
x
disp('最优值为');
fval

que2.m

%(2)
disp('第二题');
format rat;

fun=@(x)(x(1)-3)^2+(x(2)-4)^2;
x0=[3,4];
A=[-1,1;1,1];
b=[-5/2,5];
Aeq=[];
beq=[];
lb=zeros(2,1);
[x,fval]=fmincon(fun,x0,A,b,Aeq,beq,lb);

disp('最优点为');
x
disp('最优值为');
fval

que3.m

%(3)
disp('第三题');
format rat;

fun=@(x)(x(1)-3)^2+(x(2)-4)^2;
x0=[3,4];
A=[-1,1;1,1];
b=[-5/2,5];
Aeq=[1,-1];
beq=[0];
lb=zeros(2,1);
[x,fval]=fmincon(fun,x0,A,b,Aeq,beq,lb);

disp('最优点为');
x
disp('最优值为');
fval

你可能感兴趣的:(编程实现二维最优化问题图解法(最优化附加1))