# fminbnd

Find minimum of single-variable function on fixed interval

## 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` is a one-dimensional minimizer that finds a minimum for a problem specified by

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

example

````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`.```

example

````x = fminbnd(fun,x1,x2,options)` minimizes with the optimization options specified in `options`. Use `optimset` to set these options.```
````x = fminbnd(problem)` finds the minimum for `problem`, where `problem` is a structure.```

example

``````[x,fval] = fminbnd(___)```, for any input arguments, returns the value of the objective function computed in `fun` at the solution `x`.```
``````[x,fval,exitflag] = fminbnd(___)``` additionally returns a value `exitflag` that describes the exit condition.```

example

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

## Examples

collapse all

Find the point where the $\mathrm{sin}\left(x\right)$ function takes its minimum in the range $0.

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

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

`3*pi/2`
```ans = 4.7124 ```

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)^2*cos(k*x)*exp(-k^2/2); end ```

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

```x = fminbnd(@scalarobjective,1,3) ```
```x = 2.0061 ```

Minimize a function when there is an extra parameter. The function $\mathrm{sin}\left(x-a\right)$ 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.

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

This answer is correct; the theoretical value is

`3*pi/2 + 9/7`
```ans = 5.9981 ```

Monitor the steps `fminbnd` takes to minimize the $\mathrm{sin}\left(x\right)$ function for $0.

```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 ```
```x = 4.7124 ```

Find the location of the minimum of $\mathrm{sin}\left(x\right)$ and the value of the minimum for $0.

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

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)``` ```x = 4.7124 ```
```fval = -1.0000 ```
```exitflag = 1 ```
```output = struct with fields: iterations: 8 funcCount: 9 algorithm: 'golden section search, parabolic interpolation' message: 'Optimization terminated:...' ```

## Input Arguments

collapse all

Function to minimize, specified as a function handle or function name. `fun` is a function that accepts a real scalar `x` 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 as `optimset` 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'` allows `fminbnd` to proceed when the objective function returns a value that is `complex` or `NaN`. The `'on'` setting throws an error when the objective function returns a value that is `complex` or `NaN`. `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`. See Tolerances 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 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 value For 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 NameEntry

`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.

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

• The function to be minimized must be continuous.

• `fminbnd` might only give local solutions.

• `fminbnd` can exhibit slow convergence when the solution is on a boundary of the interval.

## 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  or  for details about the algorithm.

## Alternative Functionality

### App

The Optimize Live Editor task provides a visual interface for `fminbnd`.

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

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