【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.↵↵'
相关推荐
NocoBase42 分钟前
使用Langflow和AstraDB构建AI助手:从架构设计到与NocoBase的集成
人工智能·低代码·开源·api·开发工具
浪九天1 小时前
人工智能直通车系列14【机器学习基础】(逻辑回归原理逻辑回归模型实现)
人工智能·深度学习·神经网络·机器学习·自然语言处理
OreoCC2 小时前
第N5周:Pytorch文本分类入门
人工智能·pytorch·python
紫雾凌寒4 小时前
计算机视觉应用|自动驾驶的感知革命:多传感器融合架构的技术演进与落地实践
人工智能·机器学习·计算机视觉·架构·自动驾驶·多传感器融合·waymo
安忘4 小时前
LeetCode 热题 -189. 轮转数组
算法·leetcode·职场和发展
sauTCc4 小时前
DataWhale-三月学习任务-大语言模型初探(一、二、五章学习)
人工智能·学习·语言模型
Y1nhl4 小时前
力扣hot100_二叉树(4)_python版本
开发语言·pytorch·python·算法·leetcode·机器学习
暴力袋鼠哥4 小时前
基于深度学习的中文文本情感分析系统
人工智能·深度学习
视觉语言导航5 小时前
RAG助力机器人场景理解与具身操作!EmbodiedRAG:基于动态三维场景图检索的机器人任务规划
人工智能·深度学习·具身智能
岱宗夫up5 小时前
《加快应急机器人发展的指导意见》中智能化升级的思考——传统应急设备智能化升级路径与落地实践
人工智能·aigc