Documentation

fminsearch

使用无衍生化方法找到最小的无约束多变量函数

不nlinear programming solver. Searches for the minimum of a problem specified by

最小 x f ( x )

f(x) is a function that returns a scalar, andx是向量或矩阵。

Syntax

x = fminsearch(娱乐,x0)
x =fminsearch(fun,x0,options)
x = fminsearch(问题)
[x,fval] = fminsearch(___)
[x,fval,exitflag] = fminsearch(___)
[X,FVAL,EXITFLAG,输出] = fminsearch(___)

描述

example

x= fminsearch(fun,X0)starts at the pointX0并试图找到当地的最低限度x描述的功能fun

example

x= fminsearch(fun,X0,选项)最小imizes with the optimization options specified in the structure选项。Use最佳集设置这些选项。

x= fminsearch(问题)finds the minimum for问题, 在哪里问题是一个结构。

example

[x,fval] = fminsearch(___),对于任何以前的输入语法,返回fval目标函数的价值fun在解决方案上x

[x,fval,exitflag] = fminsearch(___)另外返回值exitflag描述了出口条件。

example

[x,fval,exitflag,output] = fminsearch(___)另外返回结构outputoptim的信息ization process.

例子

collapse all

Minimize Rosenbrock's function, a notoriously difficult optimization problem for many algorithms:

这功能is minimized at the pointx = [1,1]最小值0

Set the start point tox0 = [-1.2,1]并最小化Rosenbrock的功能fminsearch

fun = @(x)100*(x(2)-x(1)^2)^2 +(1 -x(1))^2;x0 = [-1.2,1];x = fminsearch(娱乐,x0)
x =1.0000 1.0000

Set options to monitor the process asfminsearchattempts to locate a minimum.

设置选项以在每次迭代中绘制目标函数。

选项=最佳集('PlotFcns',@optimplotfval);

Set the objective function to Rosenbrock's function,

这功能is minimized at the pointx = [1,1]最小值0

Set the start point tox0 = [-1.2,1]并最小化Rosenbrock的功能fminsearch

fun = @(x)100*(x(2)-x(1)^2)^2 +(1 -x(1))^2;x0 = [-1.2,1];x =fminsearch(fun,x0,options)
x =1.0000 1.0000

最小化其值通过执行文件给出的目标函数。功能文件必须接受真实的向量x和return a real scalar that is the value of the objective function.

复制以下代码并将其包含在名为的文件中Objectivesfcn1.m在您的MATLAB®路径上。

功能f = objectivefcn1(x) f = 0;fork = -10:10 f = f + exp( - (x(1)-x(2))^2-2*x(1)^2)*cos(x(2))*sin(2*x(2));end

开始x0 = [0.25,-0.25]和search for a minimum ofObjectivesfcn

x0 = [0.25,-0.25];x = fminsearch(@objectivefcn1,x0)
X = -0.1696 -0.5086

有时,您的目标函数具有额外的参数。这些参数不是要优化的变量,而是在优化过程中固定值。例如,假设您有一个参数ain the Rosenbrock-type function

This function has a minimum value of 0 at,。例如,如果, you can include the parameter in your objective function by creating an anonymous function.

以额外的参数为额外的参数创建目标函数。

f = @(x,a)100*(x(2) - x(1)^2)^2 + (a-x(1))^2;

将参数放入您的MATLAB®工作区中。

a = 3;

创建一个匿名功能x仅包括参数的工作空间值。

fun = @(x)f(x,a);

从开始解决问题x0 = [-1,1.9]

x0 = [-1,1.9];x = fminsearch(娱乐,x0)
x =3.0000 9.0000

有关在目标功能中使用额外参数的更多信息,请参见Parameterizing Functions

使用使用fminsearch

Write an anonymous objective function for a three-variable problem.

x0 = [1,2,3];fun = @(x)-norm(x + x0)^2*exp(-norm(x-x0)^2 + sum(x));

Find the minimum offun开始于X0。也找到最小值的值。

[x,fval] = fminsearch(fun,x0)
x =1.5359 2.5645 3.5932
fval = -5.9565e+04

在运行和完成后,检查优化的结果。

设置选项以提供迭代显示,该选项在求解器运行时提供有关优化的信息。另外,设置绘图函数以显示求解器运行时显示目标函数值。

选项=最佳集('展示','iter','PlotFcns',@optimplotfval);

Set an objective function and start point.

功能f = objectivefcn1(x) f = 0;fork = -10:10 f = f + exp( - (x(1)-x(2))^2-2*x(1)^2)*cos(x(2))*sin(2*x(2));end

包括代码Objectivesfcn1作为MATLAB®路径上的文件。

x0 = [0.25,-0.25];fun = @objectivefcn1;

获取所有求解器输出。求解器完成后,请使用这些输出来检查结果。

[X,FVAL,EXITFLAG,输出] = fminsearch(fun,x0,options)
迭代func -count min F(x)过程0 1 -6.70447 1 3 -6.89837初始简称2 5 -7.34101展开3 7 -7.91894展开4 9 -9.07939展开5 11 -10.50478 17 -12.8052外部9 19 -12.8052内部的合同10 21 -13.0189扩展11 23 -13.0189内部的合同12 25 -13.0374反射13 27 -13.122反射14 28 -13.122反射15 29 -13.122反映16 -13.122反映16 31 -13.122外部17 -13.122合同33 -13.1279内部18 35 -13.1279合同内部19 37 -13.1296内部的合同20 39 -13.1301合同21 41 -13.1305反映22 43 -13.1306内部23 45 -13.1309内部24 47 -13.1309内部25 47 -13.1309合同反射26 51 -13.131内部27 53 -13.131合同28 55 -13.131合同内部29 57 -13.131合同外部合同外部30 59 -13.131内31 61 -13.131内部32 63 -13.131内部33 65 -13.131内33 65 -13.131内部34.13.131合同33 65 -13.131合同67 -13.131内部的合同35 69 -13.131合同INSIDE优化终止:当前X使用选项满足终止条件。TOLX为1.0000EE -04和F(x)满足使用选项的收敛标准。TOLFUN1.000000E -04 x = -0.1696 -0.5086 X = -1696 -0.5086次数1输出=带字段的结构:迭代:35 funccount:69算法:'nelder-mead-mead simplex直接搜索'消息:“优化终止:...”

这value ofexitflagis1, 意义fminsearchlikely converged to a local minimum.

output结构显示迭代次数。迭代显示和情节也显示了此信息。这output结构还显示了迭代显示显示的函数评估数量,但所选的绘图函数没有。

Input Arguments

collapse all

函数以最小化,指定为函数句柄或功能名称。fun是接受向量或数组的函数x并返回真正的标量f(the objective function evaluated atx)。

指定fun作为文件的函数句柄:

x = fminsearch(@myfun,x0)

在哪里myfunis a MATLAB®功能,例如

函数f = myfun(x)f = ...%计算函数值在x

You can also specifyfun作为匿名函数的功能句柄:

x =fminsearch(@(x)norm(x)^2,x0);

例子:fun = @(x)-x*exp(-3*x)

数据类型:char|功能_handle|细绳

初始点,指定为真实矢量或实际数组。求解器使用和大小的元素数量X0to determine the number and size of variables thatfunaccepts.

例子:x0 = [1,2,3,4]

数据类型:双倍的

优化选项, specified as a structure such as最佳集返回。您可以使用最佳集在选项结构中设置或更改这些字段的值。看设置选项for detailed information.

展示

显示级别(请参阅迭代显示):

  • '通知'(默认值)仅当功能不收敛时才显示输出。

  • '最后'仅显示最终输出。

  • '离开'或者'none'显示没有输出。

  • 'iter'在每次迭代处显示输出。

funvalcheck

Check whether objective function values are valid.'on'displays an error when the objective function returns a value that iscomplex或者NaN。这default'离开'显示没有错误。

maxfunevals

Maximum number of function evaluations allowed, a positive integer. The default is200*数字。看公差和停止标准

Maxiter

允许的最大迭代次数,一个正整数。默认值是200*数字。看公差和停止标准

outputfcn

指定一个或多个用户定义的函数,优化函数在每次迭代中都调用,无论是函数句柄还是函数句柄的单元格数组。默认值无([])。看Output Functions

PlotFcns

在算法执行时绘制各种进度度量。从预定义图中选择或编写自己的图。传递函数手柄或功能手柄的单元格数组。默认值无([]):

  • @optimplotxplots the current point.

  • @optimplotfunccount绘制函数计数。

  • @optimplotfval绘制功能值。

有关编写自定义绘图功能的信息,请参见Plot Functions

托尔福

对功能值的终止公差,一个正标量。默认值为1E-4。看公差和停止标准。与其他求解器不同,fminsearch满足时停止两个都托尔福TolX

TolX

终止公差x, a positive scalar. The default value is1E-4。看公差和停止标准。与其他求解器不同,fminsearch满足时停止两个都托尔福TolX

例子:options = optimset('display','iter')

数据类型:struct

Problem structure, specified as a structure with the following fields.

字段名称 Entry

客观的

目标功能

X0

初始点forx

求解器

'fminsearch'

选项

选项结构,例如返回最佳集

数据类型:struct

输出参数

collapse all

Solution, returned as a real vector or real array. The size ofx与大小相同X0。通常,x是解决问题的本地解决方案exitflagis positive.

解决方案的目标函数值,返回为实数。一般来说,fval=娱乐(x)

原因fminsearch停了下来, returned as an integer.

1

这功能converged to a solutionx

0

迭代次数exceeded选项。Maxiter或者number of function evaluations exceededoptions.maxfunevals

-1

该算法由输出函数终止。

有关优化过程的信息,作为带有字段的结构返回:

迭代

迭代次数

funcCount

Number of function evaluations

algorithm

'Nelder-Mead simplex direct search'

message

退出消息

提示

  • fminsearchonly minimizes over the real numbers, that is, the vector or arrayx必须仅由实数和f(x) must only return real numbers. Whenx具有复杂的值,分裂xinto real and imaginary parts.

  • Usefminsearchto solve nondifferentiable problems or problems with discontinuities, particularly if no discontinuity occurs near the solution.

Algorithms

fminsearch使用Lagarias等人的单纯形搜索方法。[1]。这是一种直接搜索方法,不使用数值或分析梯度如fminunc。这algorithm is described in detail infminsearchAlgorithm。这algorithm is not guaranteed to converge to a local minimum.

参考

[1]Lagarias, J. C., J. A. Reeds, M. H. Wright, and P. E. Wright. “Convergence Properties of the Nelder-Mead Simplex Method in Low Dimensions.”暹罗优化杂志。卷。9,第1页,1998年,第112-147页。

扩展功能

在R2006a之前引入

这个话题有帮助吗?