find the constrained maximum of a function using fmincon in matlab

To find the constrained maximum of a function using the fmincon function in MATLAB, you need to define the objective function, the inequality constraints, and the bounds on the decision variables.

Here is an example that demonstrates how to use fmincon to find the constrained maximum of a function:

main.m
% Define the objective function
fun = @(x) -(x(1)^2 + 2*x(2)^2 + 3*x(3)^2);

% Define the inequality constraints and bounds
A = [1 1 1; -1 -1 -1];
b = [1; -1];
lb = [0; 0; 0];
ub = [Inf; Inf; Inf];

% Use fmincon to find the constrained maximum
x0 = [0; 0; 0];  % Initial guess for the decision variables
options = optimset('Display', 'iter');  % Optional: display iterations
[x, fval] = fmincon(fun, x0, A, b, [], [], lb, ub, [], options);

% Display the constrained maximum and the corresponding decision variables
disp('Constrained maximum:')
disp(-fval)
disp('Decision variables:')
disp(x)
594 chars
20 lines

In this example, we define a simple objective function -x1^2 - 2*x2^2 - 3*x3^2 to maximize. We also define two inequality constraints x1 + x2 + x3 <= 1 and -x1 - x2 - x3 <= -1. The decision variables x are bounded by lb and ub constraints.

The fmincon function takes the objective function, initial guess, inequality constraints, bounds, and other optional arguments. It returns the constrained maximum fval and the corresponding decision variables x.

Note that the - sign is used in the objective function to find the maximum instead of the minimum, as fmincon is designed for minimization problems by default.

gistlibby LogSnag