To minimize a function with linear constraints in Matlab, you can use the built-in function fmincon
. This function finds the minimum of a function subject to constraints on the variables, where the constraints are linear expressions.
The general syntax of fmincon
is:
main.m61 chars2 lines
where:
fun
: is a function handle to the objective function you want to minimizex0
: is a vector of initial guess for the optimization variablesA
,b
: define the inequality constraints A*x <= b
Aeq
,beq
: define the equality constraints Aeq*x = beq
lb
,ub
: define the lower and upper bounds for each variableHere is an example code that illustrates how to use fmincon
to minimize a quadratic function with linear constraints:
main.m501 chars27 lines
This code defines an objective function (x(1)-2)^2 + (x(2)-1)^2
and finds its minimum subject to 3 linear constraints and bounds on the variables. The output x
contains the optimal solution, fval
the optimal function value, exitflag
the exit status and output
additional information about the optimization process.
gistlibby LogSnag