In MATLAB, you can use the fminbnd
function to perform a bisection search for the minimum (or maximum) of a scalar function.
Here's an example of how to use the fminbnd
function:
main.m270 chars10 lines
In the example above, the function f
is defined as x^2 - 4
. The fminbnd
function is then called with the function f
as the first argument, and the lower and upper bounds of the search range (-10
and 10
) as the second and third arguments respectively.
The fminbnd
function returns the minimum value of the function f
(f_min
) and the corresponding value of x
(x_min
) at which the minimum occurs.
Note: The fminbnd
function assumes that the function being minimized is a scalar function of a single variable. If you have a multivariable function, you will need to use a different optimization function, such as fminsearch
or fminunc
, and provide an appropriate initial guess as an additional argument.
gistlibby LogSnag