【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.↵↵'
相关推荐
袋鼠云数栈25 分钟前
集团数字化统战实战:统一数据门户与全业态监管体系构建
大数据·数据结构·人工智能·多模态
廋到被风吹走29 分钟前
【AI】Codex 多语言实测:Python/Java/JS/SQL 效果横评
java·人工智能·python
cskywit37 分钟前
【IEEE TNNLS 2025】赋予大模型“跨院行医”的能力:基于全局与局部提示的医学图像泛化框架 (GLP) 解析
人工智能
2501_948114241 小时前
AI API Gateway 选型指南:2026 年生产环境下的聚合平台深度对比
人工智能·gateway
小月球~1 小时前
天梯赛 · 并查集
数据结构·算法
实在智能RPA1 小时前
Agent 在物流行业能实现哪些自动化?——深度拆解 AI Agent 驱动的智慧物流新范式
运维·人工智能·ai·自动化
TechubNews1 小时前
Jack Dorsey:告别传统公司层级,借助 AI 走向智能体架构
大数据·人工智能
伴野星辰1 小时前
如何提高YOLO8目标检测的准确性?
人工智能·目标检测·机器学习
仍然.1 小时前
算法题目---模拟
java·javascript·算法
胡耀超2 小时前
Token的八副面孔:为什么“词元“不需要更好的翻译,而需要更多的读者
大数据·人工智能·python·agent·token·代币·词元