MATLAB: How to write the nonlinear constraints in a correct way

nonlinear constraints

I have a complex problem to solve and I'm using the fmincon solver. Firstly I wrote the equality and inequality constraints like it was done in the user guide for Optimisation Toolbox in the following form:
variables = {'I1','I2','HE1','HE2','LE1','LE2','C','BF1',...
'BF2','HPS','MPS','LPS','P1','P2','PP','EP'};
N = length(variables);
% create variables for indexing
for v = 1:N
eval([variables{v},' = ', num2str(v),';']);
end
Aeq = zeros(4,16); beq = zeros(4,1);
Aeq(1,[LE2,HE2,I2]) = [1,1,-1];
Aeq(2,[LE1,LE2,BF2,LPS]) = [1,1,1,-1];
Aeq(3,[I1,I2,BF1,HPS]) = [1,1,1,-1];
Aeq(4,[C,MPS,LPS,HPS]) = [1,1,1,-1];
etc.
My question is how to assign these variables in separate script file to be able to write the nonlinear constraints in the form like this:
LE1^2+BF1^2=C*MPS : For example
Thank you.

Best Answer

I am afraid that you didn't quite understand the example. The point is that an x variable has many components, and in this example the components were named.
To write a nonlinear constraint function, use the syntax
function [c,ceq] = mycfun(x)
% Name the variables if you like
variables = {'I1','I2','HE1','HE2','LE1','LE2','C','BF1',...
'BF2','HPS','MPS','LPS','P1','P2','PP','EP'};
N = length(variables);
% create variables for indexing
for v = 1:N
eval([variables{v},' = ', num2str(v),';']);
end
% Then start the computation
c = [];
ceq = x(LE1)^2+x(BF1)^2 - x(C)*x(MPS);
I hope this makes sense to you. For details for nonlinear constraints, see the documentation.
Alan Weiss
MATLAB mathematical toolbox documentation