In Matlab, you can use the function fmincon to find the minimum of a multivariable function subject to constraints.
The input of fmincon is:
fun: the objective function to minimizex0: the initial guess of the solutionA, b: the linear inequality constraint A*x <= bAeq, beq: the linear equality constraint Aeq*x = beqlb, ub: the lower and upper bounds for the variables xHere is an example code to find the minimum of the function f(x,y) = 3*x + 4*y subject to the constraint x + y <= 5:
main.m125 chars8 lines
The output x is the solution that minimizes the function fun, and the output fval is the minimum value of fun at x.
If you want to find the maximum of a function, you can simply negate the objective function and call fmincon with the negated function:
main.m144 chars10 lines
In this case, max_val is the maximum value of the function -fun.
gistlibby LogSnag