To minimize a function subject to constraints in MATLAB, you can use the fmincon
function in the Optimization Toolbox. This function finds the minimum of a constrained nonlinear multivariable function. The general syntax is:
main.m62 chars2 lines
where fun
is the objective function to minimize, x0
is the initial guess for the variables, A
and b
are matrices defining linear inequalities A*x <= b
, Aeq
and beq
are matrices defining linear equalities Aeq*x = beq
, lb
and ub
are the lower and upper bounds of the variables, and nonlcon
is a function handle defining the nonlinear constraint nonlcon(x) <= 0
.
Here is an example of how to use fmincon
to minimize the Rosenbrock function subject to linear and nonlinear constraints:
main.m688 chars28 lines
In this example, the objective function is the Rosenbrock function, which is subject to the linear inequality constraint x1 + x2 <= 1
and the nonlinear inequality constraint x2 - x1^2 <= 0
. The initial guess for x
is [0;0]
. fmincon
returns the optimal value for x
in the variable x
and the minimum value of the function in the variable fval
.
gistlibby LogSnag