fminbnd - Find local minimum of single-variable function on fixed interval - MATLAB (original) (raw)

Find local minimum of single-variable function on fixed interval

Syntax

Description

fminbnd is a one-dimensional minimizer that finds a local minimum for a problem specified by

x, x_1, and_x_2 are finite scalars, and_f(x) is a function that returns a scalar.

If multiple local minima exist on the interval (x1,x2),fminbnd returns only one, which is not guaranteed to be the global minimum. For details, see Local vs. Global Minimum.

[x](#bvadyg6-1-x) = fminbnd([fun](#bvadyg6-1-fun),[x1](#bvadyg6-1-x1),[x2](#bvadyg6-1-x2)) returns a value x that is a local minimizer of the scalar valued function that is described in fun in the intervalx1 < x < x2.

example

[x](#bvadyg6-1-x) = fminbnd([fun](#bvadyg6-1-fun),[x1](#bvadyg6-1-x1),[x2](#bvadyg6-1-x2),[options](#bvadyg6-1-options)) minimizes with the optimization options specified in options. Use optimset to set these options.

example

[x](#bvadyg6-1-x) = fminbnd([problem](#bvadyg6-1-problem)) minimizesproblem, where problem is a structure.

[[x](#bvadyg6-1-x),[fval](#bvadyg6-1-fval)] = fminbnd(___), for any input arguments, returns the value of the objective function computed in fun at the solution x.

example

[[x](#bvadyg6-1-x),[fval](#bvadyg6-1-fval),[exitflag](#bvadyg6-1-exitflag)] = fminbnd(___) additionally returns a value exitflag that describes the exit condition.

[[x](#bvadyg6-1-x),[fval](#bvadyg6-1-fval),[exitflag](#bvadyg6-1-exitflag),[output](#bvadyg6-1-output)] = fminbnd(___) additionally returns a structure output that contains information about the optimization.

example

Examples

collapse all

Find the point where the sin(x) function takes its minimum in the range 0<x<2π.

fun = @sin; x1 = 0; x2 = 2*pi; x = fminbnd(fun,x1,x2)

To display precision, this is the same as the correct value x=3π/2.

Minimize a function that is specified by a separate function file. A function accepts a point x and returns a real scalar representing the value of the objective function at x.

Write the following function as a file, and save the file as scalarobjective.m on your MATLAB® path.

function f = scalarobjective(x) f = 0; for k = -10:10 f = f + (k+1)^2cos(kx)*exp(-k^2/2); end

Find the x that minimizes scalarobjective on the interval 1 <= x <= 3.

x = fminbnd(@scalarobjective,1,3)

Minimize a function when there is an extra parameter. The function sin(x-a) has a minimum that depends on the value of the parameter a. Create an anonymous function of x that includes the value of the parameter a. Minimize this function over the interval 0<x<2π.

a = 9/7; fun = @(x)sin(x-a); x = fminbnd(fun,1,2*pi)

This answer is correct; the theoretical value is

For more information about including extra parameters, see Parameterizing Functions.

Monitor the steps fminbnd takes to minimize the sin(x) function for 0<x<2π.

fun = @sin; x1 = 0; x2 = 2*pi; options = optimset('Display','iter'); x = fminbnd(fun,x1,x2,options)

Func-count x f(x) Procedure 1 2.39996 0.67549 initial 2 3.88322 -0.67549 golden 3 4.79993 -0.996171 golden 4 5.08984 -0.929607 parabolic 5 4.70582 -0.999978 parabolic 6 4.7118 -1 parabolic 7 4.71239 -1 parabolic 8 4.71236 -1 parabolic 9 4.71242 -1 parabolic

Optimization terminated: the current x satisfies the termination criteria using OPTIONS.TolX of 1.000000e-04

Find the location of the minimum of sin(x) and the value of the minimum for 0<x<2π.

fun = @sin; [x,fval] = fminbnd(fun,1,2*pi)

Return all information about the fminbnd solution process by requesting all outputs. Also, monitor the solution process using a plot function.

fun = @sin; x1 = 0; x2 = 2*pi; options = optimset('PlotFcns',@optimplotfval); [x,fval,exitflag,output] = fminbnd(fun,x1,x2,options)

Figure Optimization Plot Function contains an axes object. The axes object with title Current Function Value: -1, xlabel Iteration, ylabel Function value contains an object of type scatter.

output = struct with fields: iterations: 8 funcCount: 9 algorithm: 'golden section search, parabolic interpolation' message: 'Optimization terminated:↵ the current x satisfies the termination criteria using OPTIONS.TolX of 1.000000e-04 ↵'

Input Arguments

collapse all

Function to minimize, specified as a function handle or function name.fun is a function that accepts a real scalarx and returns a real scalar f (the objective function evaluated at x).

Specify fun as a function handle for a file:

x = fminbnd(@myfun,x1,x2)

where myfun is a MATLAB® function such as

function f = myfun(x) f = ... % Compute function value at x

You can also specify fun as a function handle for an anonymous function:

x = fminbnd(@(x)norm(x)^2,x1,x2);

Example: fun = @(x)-x*exp(-3*x)

Data Types: char | function_handle | string

Lower bound, specified as a finite real scalar.

Example: x1 = -3

Data Types: double

Upper bound, specified as a finite real scalar.

Example: x2 = 5

Data Types: double

Optimization options, specified as a structure such asoptimset returns. You can use optimset to set or change the values of these fields in the options structure. See Set Optimization Options for detailed information.

Display Level of display (see Optimization Solver Iterative Display): 'notify' (default) displays output only if the function does not converge.'off' or'none' displays no output.'iter' displays output at each iteration.'final' displays just the final output.
FunValCheck Check whether objective function values are valid. The default 'off' allowsfminbnd to proceed when the objective function returns a value that iscomplex orNaN. The 'on' setting throws an error when the objective function returns a value that is complex orNaN.
MaxFunEvals Maximum number of function evaluations allowed, a positive integer. The default is 500. See Tolerances and Stopping Criteria.
MaxIter Maximum number of iterations allowed, a positive integer. The default is 500. SeeTolerances and Stopping Criteria.
OutputFcn Specify one or more user-defined functions that an optimization function calls at each iteration, either as a function handle or as a cell array of function handles. The default is none ([]). See Optimization Solver Output Functions.
PlotFcns Plots various measures of progress while the algorithm executes. Select from predefined plots or write your own. Pass a function name, function handle, or a cell array of function names or handles. The default is none ([]): @optimplotx plots the current point@optimplotfunccount plots the function count@optimplotfval plots the function valueFor information on writing a custom plot function, see Optimization Solver Plot Functions.
TolX Termination tolerance on x, a positive scalar. The default is 1e-4. See Tolerances and Stopping Criteria.

Example: options = optimset('Display','iter')

Data Types: struct

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

Field Name Entry
objective Objective function
x1 Left endpoint
x2 Right endpoint
solver 'fminbnd'
options Options structure such as returned by optimset

Data Types: struct

Output Arguments

collapse all

Solution, returned as a real scalar. Typically, x is a local solution to the problem when exitflag is positive. See Local vs. Global Minimum.

Objective function value at the solution, returned as a real number. Generally, fval = fun(x).

Reason fminbnd stopped, returned as an integer.

1 Function converged to a solution x.
0 Number of iterations exceeded options.MaxIter or number of function evaluations exceeded options.MaxFunEvals.
-1 Stopped by an output function or plot function.
-2 The bounds are inconsistent, meaning x1 > x2.

Information about the optimization process, returned as a structure with fields:

iterations Number of iterations taken
funcCount Number of function evaluations
algorithm 'golden section search, parabolic interpolation'
message Exit message

Limitations

More About

collapse all

In general, optimization solvers return a local minimum (or optimum). The result might be a global minimum (or optimum), but might not.

Curve with two dips; the lower dip is the global minimum, the higher dip is a local minimum

MATLAB and Optimization Toolbox™ optimization solvers typically return a local minimum. Global Optimization Toolbox solvers can search for a global minimum, but do not guarantee that their solutions are global. For an example of global search, see Find Global or Multiple Local Minima (Global Optimization Toolbox).

Algorithms

fminbnd is a function file. The algorithm is based on golden section search and parabolic interpolation. Unless the left endpoint _x_1 is very close to the right endpoint _x_2, fminbnd never evaluates fun at the endpoints, so fun need only be defined for x in the interval _x_1 < x < _x_2.

If the minimum actually occurs at _x_1 or _x_2, fminbnd returns a point x in the interior of the interval (_x_1,_x_2) that is close to the minimizer. In this case, the distance of x from the minimizer is no more than 2*(TolX + 3*abs(x)*sqrt(eps)). See [1] or [2] for details about the algorithm.

Alternative Functionality

App

The Optimize Live Editor task provides a visual interface forfminbnd.

References

[1] Forsythe, G. E., M. A. Malcolm, and C. B. Moler. Computer Methods for Mathematical Computations. Englewood Cliffs, NJ: Prentice Hall, 1976.

[2] Brent, Richard. P. Algorithms for Minimization without Derivatives. Englewood Cliffs, NJ: Prentice-Hall, 1973.

Extended Capabilities

expand all

For C/C++ code generation:

Version History

Introduced before R2006a