Matlab provides several functions for optimizing functions and finding the minimum or maximum values. One of the commonly used functions in Matlab for finding the minimum or maximum of a function is fminbnd
.
Syntax:
main.m47 chars2 lines
where,
fun
: a function handle that takes a scalar input and returns a scalar value. This function should be continuous between xmin
and xmax
.xmin
: the lower bound of the interval.xmax
: the upper bound of the interval.x
: the value that minimizes or maximizes the function.fval
: the function value at x
.exitflag
: a flag that indicates if the function converged or not.To find the maximum of a function f(x)
between a starting point xmin
and an ending point xmax
, the negative of the function -f(x)
can be minimized using fminbnd
.
Example:
main.m281 chars14 lines
In this example, fminbnd
is used to find the maximum value of the function -x^2 + 2*x + 3
between the range -10
and 10
. The output shows that the maximum occurs at x=1
and the maximum value is f(x) = 4
.
gistlibby LogSnag