| Optimization Toolbox™ | ![]() |
Find minimum of unconstrained multivariable function using derivative-free method
Finds the minimum of a problem specified by
![]()
where x is a vector and f(x) is a function that returns a scalar.
x = fminsearch(fun,x0)
x = fminsearch(fun,x0,options)
x = fminsearch(problem)
[x,fval] = fminsearch(...)
[x,fval,exitflag] = fminsearch(...)
[x,fval,exitflag,output] = fminsearch(...)
fminsearch attempts to find a minimum of a scalar function of several variables, starting at an initial estimate. This is generally referred to as unconstrained nonlinear optimization.
x = fminsearch(fun,x0) starts at the point x0 and attempts to find a local minimum x of the function described in fun. fun is a function handle for either an M-file function or an anonymous function. x0 can be a scalar, vector, or matrix.
x = fminsearch(fun,x0,options) minimizes with the optimization options specified in the structure options. Use optimset to set these options.
x = fminsearch(problem) finds the minimum for problem, where problem is a structure described in Input Arguments.
Create the structure problem by exporting a problem from Optimization Tool, as described in Exporting to the MATLAB® Workspace.
[x,fval] = fminsearch(...) returns in fval the value of the objective function fun at the solution x.
[x,fval,exitflag] = fminsearch(...) returns a value exitflag that describes the exit condition of fminsearch.
[x,fval,exitflag,output] = fminsearch(...) returns a structure output that contains information about the optimization.
Passing Extra Parameters explains how to parameterize the objective function fun, if necessary.
Function Arguments contains general descriptions of arguments passed into fminsearch. This section provides function-specific details for fun, options, and problem:
fun | The function to be minimized. fun is a function handle for a function that accepts a vector x and returns a scalar f, the objective function evaluated at x. The function fun can be specified as a function handle for an M-file function x = fminsearch(@myfun,x0) where myfun is a MATLAB® function such as function f = myfun(x) f = ... % Compute function value at x fun can also be a function handle for an anonymous function, such as x = fminsearch(@(x)norm(x)^2,x0,A,b); | |
options | Options provides the function-specific details for the options values. | |
| problem | objective | Objective function |
x0 | Initial point for x | |
solver | 'fminsearch' | |
options | Options structure created with optimset | |
Function Arguments contains general descriptions of arguments returned by fminsearch. This section provides function-specific details for exitflag and output:
exitflag | Integer identifying the reason the algorithm terminated. The following lists the values of exitflag and the corresponding reasons the algorithm terminated. | |
1 | The function converged to a solution x. | |
0 | Number of iterations exceeded options.MaxIter or number of function evaluations exceeded options.FunEvals. | |
-1 | The algorithm was terminated by the output function. | |
output | Structure containing information about the optimization. The fields of the structure are | |
| iterations | Number of iterations | |
| funcCount | Number of function evaluations | |
| algorithm | Optimization algorithm used | |
| message | Exit message | |
Optimization options used by fminsearch. You can use optimset to set or change the values of these fields in the options structure options. See Optimization Options for detailed information.
Display | Level of display. 'off' displays no output; 'iter' displays output at each iteration; 'final' displays just the final output; 'notify' (default) displays output only if the function does not converge. |
| FunValCheck | Check whether objective function and constraints values are valid. 'on' displays an error when the objective function or constraints return a value that is complex or NaN. 'off' (the default) displays no error. |
MaxFunEvals | Maximum number of function evaluations allowed. |
MaxIter | Maximum number of iterations allowed. |
| OutputFcn | Specify one or more user-defined functions that an optimization function calls at each iteration. See Output Function. |
PlotFcns | Plots various measures of progress while the algorithm executes, select from predefined plots or write your own. Specifying @optimplotx plots the current point; @optimplotfunccount plots the function count; @optimplotfval plots the function value. |
TolFun | Termination tolerance on the function value. |
TolX | Termination tolerance on x. |
A classic test example for multidimensional minimization is the Rosenbrock banana function
![]()
The minimum is at (1,1) and has the value 0. The traditional starting point is (-1.2,1). The anonymous function shown here defines the function and returns a function handle called banana:
banana = @(x)100*(x(2)-x(1)^2)^2+(1-x(1))^2;
Pass the function handle to fminsearch:
[x,fval,exitflag] = fminsearch(banana,[-1.2, 1])
This produces
x =
1.0000 1.0000
fval =
8.1777e-010
exitflag =
1This indicates that the minimizer was found at [1 1] with a value near zero.
You can modify the first example by adding a parameter a to the second term of the banana function:
![]()
This changes the location of the minimum to the point [a,a^2]. To minimize this function for a specific value of a, for example a = sqrt(2), create a one-argument anonymous function that captures the value of a.
a = sqrt(2); banana = @(x)100*(x(2)-x(1)^2)^2+(a-x(1))^2;
Then the statement
[x,fval,exitflag] = fminsearch(banana, [-1.2, 1], ...
optimset('TolX',1e-8));
seeks the minimum [sqrt(2), 2] to an accuracy higher than the default on x. The result is
x =
1.4142 2.0000
fval =
4.2065e-018
exitflag =
1fminsearch uses the simplex search method of [1]. This is a direct search method that does not use numerical or analytic gradients as in fminunc.
If n is the length of x, a simplex in n-dimensional space is characterized by the n+1 distinct vectors that are its vertices. In two-space, a simplex is a triangle; in three-space, it is a pyramid. At each step of the search, a new point in or near the current simplex is generated. The function value at the new point is compared with the function's values at the vertices of the simplex and, usually, one of the vertices is replaced by the new point, giving a new simplex. This step is repeated until the diameter of the simplex is less than the specified tolerance.
fminsearch is generally less efficient than fminunc for problems of order greater than two. However, when the problem is highly discontinuous, fminsearch might be more robust.
fminsearch solves nondifferentiable problems and can often handle discontinuity, particularly if it does not occur near the solution. fminsearch might only give local solutions.
fminsearch only minimizes over the real numbers, that is, x must only consist of real numbers and f(x) must only return real numbers. When x has complex variables, they must be split into real and imaginary parts.
fminsearch is not the preferred choice for solving problems that are sums of squares, that is, of the form
![]()
Instead use the lsqnonlin function, which has been optimized for problems of this form.
[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," SIAM Journal of Optimization, Vol. 9, Number 1, pp. 112–147, 1998.
@ (function_handle), fminbnd, fminunc, optimset, optimtool, anonymous functions
![]() | fminimax | fminunc | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |