fmincon constraint in matlab

fmincon is a built-in MATLAB optimization solver that solves nonlinear programming problems with non-linear constraints. Constraints can be defined as linear or non-linear inequality/equality functions. Here's an example code snippet that demonstrates how to use fmincon with constraints:

main.m
% Define objective function and initial guess
fun = @(x) 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
x0 = [-1,2];

% Define inequality constraints 
A = [1,1];
b = 1;

% Define options for fmincon solver
options = optimoptions('fmincon','Display','iter');

% Solve with fmincon
[x,fval,exitflag,output] = fmincon(fun,x0,A,b,[],[],[],[],[],options);
342 chars
14 lines

In the above code:

  • fun is the objective function to be minimized
  • x0 is the initial guess for the solver to start the minimization
  • A,b define the linear inequality constraint
  • options defines the solver options.
  • x is the output, the optimized decision variables that minimize the given objective function.
  • fval is the optimal objective function value of fun found by fmincon
  • exitflag is an output that indicates the exit condition of the solver
  • output is a structure containing output about the number of function evaluations, algorithm iterations, etc.

Please refer to MATLAB documentation for more detailed explanation of the fmincon usage and syntax.

related categories

gistlibby LogSnag