Accelerating the pace of engineering and science

• Trials

# fminbnd

Find minimum of single-variable function on fixed interval

## Equation

Finds a minimum for a problem specified by

x, x1, and x2 are scalars and f(x) is a function that returns a scalar.

## Syntax

x = fminbnd(fun,x1,x2)
x = fminbnd(fun,x1,x2,options)
x = fminbnd(problem)
[x,fval] = fminbnd(...)
[x,fval,exitflag] = fminbnd(...)
[x,fval,exitflag,output] = fminbnd(...)

## Description

fminbnd attempts to find a minimum of a function of one variable within a fixed interval.

 Note:   Passing Extra Parameters explains how to pass extra parameters to the objective function, if necessary.

x = fminbnd(fun,x1,x2) returns a value x that is a local minimizer of the scalar valued function that is described in fun in the interval x1 < x < x2. fun is either a function handle to a file or is an anonymous function.

x = fminbnd(fun,x1,x2,options) minimizes with the optimization options specified in the structure options. Use optimset to set these options.

x = fminbnd(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 app, as described in Exporting Your Work.

[x,fval] = fminbnd(...) returns the value of the objective function computed in fun at the solution x.

[x,fval,exitflag] = fminbnd(...) returns a value exitflag that describes the exit condition of fminbnd.

[x,fval,exitflag,output] = fminbnd(...) returns a structure output that contains information about the optimization.

## Input Arguments

Function Arguments contains general descriptions of arguments passed into fminbnd. 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 scalar x and returns a scalar f, the objective function evaluated at x. The function fun can be specified 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.```fun can also be a function handle for an anonymous function.`x = fminbnd(@(x)sin(x^2),x1,x2);` options Options provides the function-specific details for the options values. problem f Objective function x1 Left endpoint x2 Right endpoint solver 'fminbnd' options Options structure created with optimset

## Output Arguments

Function Arguments contains general descriptions of arguments returned by fminbnd. 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 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. output Structure containing information about the optimization. The fields of the structure are iterations Number of iterations taken funcCount Number of function evaluations algorithm Optimization algorithm used message Exit message

## Options

Optimization options used by fminbnd. You can use optimset to set or change the values of these fields in the options structure options. See Optimization Options Reference for detailed information.

 Display Level of display. 'off' or 'none' 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 values are valid. 'on' displays an error when the objective function returns a value that is complex, Inf, or NaN. The default 'off' displays no error. MaxFunEvals Maximum number of function evaluations allowed, a positive integer. The default is 500. MaxIter Maximum number of iterations allowed, a positive integer. The default is 500. 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 Output Function. PlotFcns Plots various measures of progress while the algorithm executes, select from predefined plots or write your own. Pass a function handle or a cell array of function 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 Plot Functions. TolX Termination tolerance on x, a positive scalar. The default is 1e-4.

## Examples

A minimum of sin(x) occurs at

```x = fminbnd(@sin,0,2*pi)
x =
4.7124```

The value of the function at the minimum is

```y = sin(x)
y =
-1.0000```

To find the minimum of the function

f(x) = (x – 3)2 – 1,

on the interval (0,5), first write a function file.

```function f = myfun(x)
f = (x-3)^2 - 1;```

Next, call an optimization routine.

`x = fminbnd(@myfun,0,5)`

This generates the solution

```x =
3```

The value at the minimum is

```y = myfun(x)

y =
-1```

If fun is parameterized, you can use anonymous functions to capture the problem-dependent parameters. For example, suppose you want to minimize the objective function myfun defined by the following function file:

```function f = myfun(x,a)
f = (x - a)^2;```

Note that myfun has an extra parameter a, so you cannot pass it directly to fminbind. To optimize for a specific value of a, such as a = 1.5.

1. Assign the value to a.

`a = 1.5; % define parameter first`
2. Call fminbnd with a one-argument anonymous function that captures that value of a and calls myfun with two arguments:

```x = fminbnd(@(x) myfun(x,a),0,1)

x =
0.9999```

## Limitations

The function to be minimized must be continuous. fminbnd might only give local solutions.

fminbnd often exhibits slow convergence when the solution is on a boundary of the interval. In such a case, fmincon often gives faster and more accurate solutions.

fminbnd only handles real variables.

expand all

### Algorithms

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

If the minimum actually occurs at x1 or x2, fminbnd returns a point x in the interior of the interval (x1,x2) 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.

## References

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

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