Matlab线性/非线性规划优化算法(6)

实例:
寻找曲面到平面的最短距离:
在这里插入图片描述

%% how the initial points affect the results
clc
clear all
[x,y] = meshgrid(-4:0.1:4,-4:0.1:4);
z = x.^2 + y.^2;
 mesh (x,y,z);
plot3(x,y,z)
hold on 
zz = x-3*y -9;
if zz>0
    zz = 0;
end
mesh(x,y,zz);


hold on
x0 = [0,-0.2];
Aeq = [1 -3]; beq = 9;
A = [];
b = [];
nonlcon = [];
lb = [-4,-4];
ub = [4,4];
fun = @(x)x(1).^2 + x(2).^2;
options = optimoptions('fmincon','Display','iter','Algorithm','interior-point');
 [x,fval,exitflag,output,lambda,grad,hessian] ...
    = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
    
plot3(x(1),x(2),x(1)-3*x(2) -9,'r*')

结果如下:

                                      First-order      Norm of
 Iter F-count            f(x)  Feasibility   optimality         step
    0       3    4.000000e-02    8.400e+00    1.133e-01
    1       6    8.103372e+00    1.776e-15    3.148e+00    2.659e+00
    2       9    8.102246e+00    0.000e+00    9.832e-02    1.055e-01
    3      12    8.100000e+00    1.776e-15    9.528e-04    4.699e-02
    4      15    8.100000e+00    0.000e+00    8.229e-06    3.996e-04
    5      18    8.100000e+00    0.000e+00    7.692e-08    3.500e-07

Local minimum found that satisfies the constraints.

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

<stopping criteria details>

x =

    0.9000   -2.7000


fval =

    8.1000


exitflag =

     1


output = 

  struct with fields:

         iterations: 5
          funcCount: 18
    constrviolation: 0
           stepsize: 3.4999e-07
          algorithm: 'interior-point'
      firstorderopt: 7.6924e-08
       cgiterations: 0
            message: '↵Local minimum found that satisfies the constraints.↵↵Optimization completed because the objective function is non-decreasing in ↵feasible directions, to within the value of the optimality tolerance,↵and constraints are satisfied to within the value of the constraint tolerance.↵↵<stopping criteria details>↵↵Optimization completed: The relative first-order optimality measure, 1.424514e-08,↵is less than options.OptimalityTolerance = 1.000000e-06, and the relative maximum constraint↵violation, 0.000000e+00, is less than options.ConstraintTolerance = 1.000000e-06.↵↵'


lambda = 

  struct with fields:

         eqlin: -1.8000
      eqnonlin: [0×1 double]
       ineqlin: [0×1 double]
         lower: [2×1 double]
         upper: [2×1 double]
    ineqnonlin: [0×1 double]


grad =

    1.8000
   -5.4000


hessian =

    1.7636    0.0948
    0.0948    2.1038

图中红色星号即为最优点
在这里插入图片描述

发布了71 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/gophae/article/details/104100861