fmincon vs fminbnd in matlab

fminbnd and fmincon are both optimization functions in Matlab, but they are used to handle different types of optimization problems.

fminbnd is used for solving problems with a single variable, where the input function is univariate. It can search for the minimum of a function within a specified interval.

On the other hand, fmincon is used for solving optimization problems with multiple variables, where the input function is multivariate. This function can handle both constrained and unconstrained optimization problems where the constraints can be linear or nonlinear.

Here is a simple example of using fminbnd for univariate optimization:

main.m
% Define the function to minimize
fun = @(x) sin(x).*exp(-x);

% Minimize the function within the interval [0, 10]
x_min = fminbnd(fun, 0, 10);
144 chars
6 lines

Here is a simple example of using fmincon for multivariate optimization:

main.m
% Define the function to minimize
fun = @(x) x(1)^2 + x(2)^2;

% Define linear inequality constraints
A = [-1 -1];
b = -2;

% Define bounds on the variables
lb = [0 0];
ub = [Inf Inf];

% Find the minimum of the function subject to the constraints and bounds
x_min = fmincon(fun, [0, 0], A, b, [], [], lb, ub);
311 chars
14 lines

In summary, if you are trying to minimize a function of a single variable, use fminbnd. If you are trying to minimize a function of multiple variables subject to constraints, use fmincon.

related categories

gistlibby LogSnag