【matlab基础知识代码】(十八)无约束最优化问题

min下面的x称为优化向量或者是决策变量

匿名函数法

>> f=@(x)(x(1)^2-2*x(1))*exp(-x(1)^2-x(2)^2-x(1)*x(2)); x0=[0; 0]; [x,b,c,d]=fminsearch(f,x0),

x =

0.6111

-0.3056

b =

-0.6414

c =

1

d =

包含以下字段的 struct:

iterations: 72

funcCount: 137

algorithm: 'Nelder-Mead simplex direct search'

message: '优化已终止:↵ 当前的 x 满足使用 1.000000e-04 的 OPTIONS.TolX 的终止条件,↵F(X) 满足使用 1.000000e-04 的 OPTIONS.TolFun 的收敛条件↵'

使用函数fminunc():

>> [x,b,c,d]=fminunc(f,[0;.0])

Local minimum found.

Optimization completed because the size of the gradient is less than

the value of the optimality tolerance.

<stopping criteria details>

x =

0.6110

-0.3055

b =

-0.6414

c =

1

d =

包含以下字段的 struct:

iterations: 7

funcCount: 27

stepsize: 1.8220e-04

lssteplength: 1

firstorderopt: 1.8030e-06

algorithm: 'quasi-newton'

message: '↵Local minimum found.↵↵Optimization completed because the size of the gradient is less than↵the value of the optimality tolerance.↵↵<stopping criteria details>↵↵Optimization completed: The first-order optimality measure, 6.010135e-07, is less ↵than options.OptimalityTolerance = 1.000000e-06.↵↵'

绘制三维等高线 ,获得并叠印中间结果

Matlab 复制代码
>>  [x,y]=meshgrid(-3:.1:3, -2:.1:2); z=(x.^2-2*x).*exp(-x.^2-y.^2-x.*y); contour(x,y,z,30);
 ff=optimset; 
ff.OutputFcn=@myout; x0=[2 1]; 
x=fminunc(f,x0,ff)

Local minimum found.

Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.

<stopping criteria details>

x =

    0.6110   -0.3055

最优化求解函数的另一种调用方法

建立最优化问题的"结构体"模型

Matlab 复制代码
>>  problem.solver='fminunc'; problem.options=optimset; problem.objective=@(x)(x(1)^2-2*x(1))*exp(-x(1)^2-x(2)^2-x(1)*x(2)); problem.x0=[2; 1]; [x,b,c,d]=fminunc(problem)

Local minimum found.

Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.

<stopping criteria details>

x =

    0.6110
   -0.3055


b =

   -0.6414


c =

     1


d = 

  包含以下字段的 struct:

       iterations: 7
        funcCount: 66
         stepsize: 1.7059e-04
     lssteplength: 1
    firstorderopt: 7.4506e-09
        algorithm: 'quasi-newton'
          message: '↵Local minimum found.↵↵Optimization completed because the size of the gradient is less than↵the value of the optimality tolerance.↵↵<stopping criteria details>↵↵Optimization completed: The first-order optimality measure, 7.437017e-09, is less ↵than options.OptimalityTolerance = 1.000000e-06.↵↵'
相关推荐
逝去的秋风1 分钟前
【代码随想录训练营第42期 Day61打卡 - 图论Part11 - Floyd 算法与A * 算法
算法·图论·floyd 算法·a -star算法
zero_one_Machel11 分钟前
leetcode73矩阵置零
算法·leetcode·矩阵
奈斯。zs14 分钟前
yjs08——矩阵、数组的运算
人工智能·python·线性代数·矩阵·numpy
Melody205014 分钟前
tensorflow-dataset 内网下载 指定目录
人工智能·python·tensorflow
青椒大仙KI1142 分钟前
24/9/19 算法笔记 kaggle BankChurn数据分类
笔记·算法·分类
DisonTangor1 小时前
阿里通义千问开源Qwen2.5系列模型:Qwen2-VL-72B媲美GPT-4
人工智能·计算机视觉
^^为欢几何^^1 小时前
lodash中_.difference如何过滤数组
javascript·数据结构·算法
豆浩宇1 小时前
Halcon OCR检测 免训练版
c++·人工智能·opencv·算法·计算机视觉·ocr
LLSU131 小时前
聚星文社AI软件小说推文软件
人工智能
JackieZhengChina1 小时前
吴泳铭:AI最大的想象力不在手机屏幕,而是改变物理世界
人工智能·智能手机