To find the extremum (minimum or maximum) of a function in Matlab, you can use the fminsearch
function for unconstrained optimization or fmincon
for constrained optimization.
Here's an example of how to use fminsearch
to find the minimum of a function:
main.m289 chars12 lines
Here's an example of how to use fmincon
to find the minimum of a function subject to a constraint:
main.m423 chars17 lines
Note that in both examples, the fun
variable is the function to minimize, x0
is the initial guess, xmin
is the location of the minimum found, and fval
is the value of the function at the minimum found. In the second example, lb
and ub
are the lower and upper bounds on the variables, respectively, and nonlcon
is a function handle to the nonlinear constraint.
gistlibby LogSnag