Abhishek Bhatia
Abhishek Bhatia

Reputation: 9814

Constrained minimization using fmincon

I wanted to solve a constrained minimization problem using fmincon. But the constraints are defined in terms of a function like f(x_0)<a, where x_0 is a solution to the problem. Is it possible?

On the docs, the example only include this x_0<a form.

Code:

f_obj = @(x)var_zcors(x,t_cw);
opt_theta = fminbnd(f_obj,0,360);

Now, x should constrained such that f_constraint(x)< a.

Update(From answer by @Phil Goddard):

f_obj = @(x)var_zcors(x,t_cw);
f_nl = @(x)deal(f_constraint(x)-a,[]);
x0 = 180; % or whatever is appropriate
opt_theta = fmincon(f_obj,x0,[],[],[],[],0,360,f_nl);

Say in the above code f_constraint returns a vector [x_min y_max] instead of a scalar. And I want to specify the following constraints:

x_min>b
y_max<a

What is a possible way to achieve that?

Upvotes: 2

Views: 653

Answers (1)

Phil Goddard
Phil Goddard

Reputation: 10782

You have a nonlinear constraint and hence need to use the nonlinear constraint input to fmincon. That is,

f_obj = @(x)var_zcors(x,t_cw);
f_nl = @(x)deal(f_constraint(x)-a,[]);
x0 = 180; % or whatever is appropriate
opt_theta = fmincon(f_obj,x0,[],[],[],[],0,360,f_nl);

If you have multiple (non-linear) constraints, then as per the examples in the doc, you write a function to return a vector of constraints. In your case you want to write a function in a separate file like the following:

function [c,ceq] = my_nonlinear_constraints(x,ab)

% define the non-linear inequality constraints
% (This assumes that ab is a 2 element vector containing your a and b
% variables.)
[x_min,y_max] = f_constraint(x);
c = nan(2,1);
c(1) = -x_min+ab(2); % this is x_min>b
c(2) = y_max-ab(1);  % this is y_max<a

% There are no non-linear equality constraints, but this is required
ceq = [];

Then, to perform the optimization, you want

% Variables a and b must be defined prior to this.
f_obj = @(x)var_zcors(x,t_cw);
f_nl = @(x)my_nonlinear_constraints(x,[a b]);
x0 = 180; % or whatever is appropriate
opt_theta = fmincon(f_obj,x0,[],[],[],[],0,360,f_nl);

Upvotes: 6

Related Questions