【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.↵↵'
相关推荐
qq_160144871 分钟前
零基础两个月后,我拿到了AI应用方向的offer,低门槛转行真实路径
人工智能
alwaysrun1 分钟前
Agentic AI与思维链和自我反思简介
人工智能·agent·思维链·思维树·自我反思
云qq3 分钟前
C++ 原子操作
开发语言·c++·算法
源于花海7 分钟前
模型独立的学习方式——协同训练
深度学习·机器学习·协同训练
geneculture10 分钟前
亚符号:人机互助中被忽视的根基——一种认知哲学分析
人工智能·融智学的重要应用·人机间性·符号与规则·亚符号与权重·融智时代杂志
aisifang0010 分钟前
PDF转Word神器:Gemini3.1Pro一键搞定文档处理
人工智能·pdf·word
xrgs_shz11 分钟前
基于轻量化浅层卷积神经网络的手写数字识别
算法·matlab·cnn
东坡肘子12 分钟前
让 AI 从称手到称心 -- 肘子的 Swift 周报 #134
人工智能·swiftui·swift
潘祖记14 分钟前
# 一行命令让 AI 接管全屋智能:FeyaGate Skill 保姆级接入教程,小米/涂鸦/美的/易微联全搞定
人工智能·后端·asp.net
joshchen21518 分钟前
强化学习基础(赵世钰)第二章 贝尔曼方程
人工智能·python·机器学习·强化学习