To find extrema of a multivariable function in Matlab, you can use the fmincon
function which is provided by the Optimization Toolbox. The fmincon
function minimizes a function subject to constraints. To find the maximum of a function, you need to negate the function and minimize it.
Here is an example code to find the minimum and maximum of a function of two variables using fmincon
:
main.m822 chars27 lines
In this code, the function f
represents the function to be minimized/negated. The lb
and ub
variables represent the lower and upper bounds of the search space. The x0
variable represents the initial guess for the optimization algorithm. You also need to define the options structure with optimization parameters. Finally, you can call the fmincon
function to minimize/negate the function.
The fmincon
function will return the optimal variable values in x_min
and x_max
, as well as the optimized function value in f_min
and f_max
. Note that to find the maximum of a function, you also need to define a nonlinear constraint that negates the function when the constraint is satisfied, as I have shown in this example.
gistlibby LogSnag