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

本文通过MATLAB编程,演示了如何找到从特定曲面到平面的最短距离,利用fmincon函数进行优化计算,展示了初始点对结果的影响,并给出了详细的迭代过程和最终结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

%% 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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值