matlab - fminunc 函数

功能

找到最小的无约束多变量函数

min f(x)
x

where f(x) is a function that returns a scalar(标量).
x is a vector or a matrix;

语法

x = fminunc(fun,x0)
x = fminunc(fun,x0,options)
x = fminunc(problem)
[x,fval] = fminunc(___)
[x,fval,exitflag,output] = fminunc(___)
[x,fval,exitflag,output,grad,hessian] = fminunc(___)

描述

x = fminunc(fun,x0)从x0开始,尝试寻找fun中描述的函数的局部最小值x。 点x0可以是标量,矢量或矩阵

x = fminunc(fun,x0,options)使选项中指定的优化选项最小化。 使用optimoptions来设置这些选项。

x = fminunc(problem) finds the minimum for problem, where problem is a structure described in Input Arguments. Create the problem structure by exporting a problem from Optimization app, as described in Exporting Your Work.

[x,fval] = fminunc(___),对于任何语法,返回的解x

[x,fval,exitflag,output] = fminunc(___)另外返回一个描述fminunc退出条件的值exitflag,以及一个带有关于优化过程信息的结构输出。

[x,fval,exitflag,output,grad,hessian] = fminunc(___)另外返回:
grad — Gradient of fun at the solution x
hessian — Hessian of fun at the solution x. See fminunc Hessian.

例子
Step 1: Write a file objfun.m.

function f = objfun(x)
f = exp(x(1)) * (4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1);

Step 2: Set options.
设置选项以使用“quasi-newton”算法。 设置选项是因为“trust-region”算法要求目标函数包含渐变。 如果您没有设置选项,那么根据您的MATLAB®版本,fminunc可以发出警告。

options = optimoptions(@fminunc,'Algorithm','quasi-newton');

Step 3: Invoke fminunc using the options.

x0 = [-1,1]; % Starting guess
[x,fval,exitflag,output] = fminunc(@objfun,x0,options);

结果

Local minimum found.

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

<stopping criteria details>


x =

    0.5000   -1.0000


fval =

   3.6609e-15


exitflag =

     1


output = 

       iterations: 8
        funcCount: 66
         stepsize: 6.3361e-07
     lssteplength: 1
    firstorderopt: 1.2284e-07
        algorithm: 'quasi-newton'
          message: 'Local minimum found.…'
    原文作者:庵下桃花仙
    原文地址: https://www.jianshu.com/p/f08db2e1a0a9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞