# Nonlinear constraint function with the optimization toolbox

6 views (last 30 days)
Kai Krickmann on 7 Feb 2020
Commented: Kai Krickmann on 7 Feb 2020
First of all thank you for your time!
My goal is to set up a nonlinear constraint related to fluid dynamics. This is about a hydrostatic model, which should be in balance. I would like to calculate all pressures and flows with the help of some measured values. Since the resistance depends on the flow velocity and the pressure, in my opinion the resistances would have to be recalculated after each iteration step.
For this reason, I had to include the calculation of the resistance in the nonlinear function "nonlcon", but the function apparently cannot access the variables in the workspace.
Does anyone have an approach on how to solve this problem?
I also tried it like this
function [c,ceq] = nonlcon(x,anzahlpipes,anzahlnodes,pipesmatlaberweitert,inzierweitert,xgv)
Error message:
Not enough input arguments.
Error in Main>nonlcon (line 224)
for i = 1 : anzahlpipes * 2
Error in Main (line 217)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
nonlinear constraint function:
function [c,ceq] = unitdisk(x)
for i = 1 : anzahlpipes*2
pipesmatlaberweitert(i,6) = x(i+anzahlnodes,1) * pipesmatlaberweitert(i,4) / (XSteam('my_pT',15,20) / XSteam('rho_pT',15,20));
lamda_dif = 0.00001;
while lamda_dif > 0.00000000000000000001
pipesmatlaberweitert(i,7) = 1 / (-2.0 * log( 2.51 / (pipesmatlaberweitert(i,6) * sqrt(pipesmatlaberweitert(i,6))) )) ^ 2;
lamda_dif = lamda_dif - pipesmatlaberweitert(i+anzahlnodes,7);
end
R(i,i) = pipesmatlaberweitert(i,7) * 8 * XSteam('rho_pT',15,20) * pipesmatlaberweitert(i,3) / (pi ^ 2 * pipesmatlaberweitert(i,4) ^ 5);
glgew = [zeros(anzahlnodes*2,anzahlnodes*2), inzierweitert, ones(anzahlnodes*2,anzahlnodes*2); transpose(inzierweitert), R, zeros(anzahlnodes*2,anzahlnodes*2); zeros(anzahlnodes*4,anzahlnodes), ones(anzahlnodes*2,anzahlnodes*2)]; % Gleichgewichtsbedingung nochmal definieren
end
c = [];
ceq = [glgew * x + xgv];
end

Gifari Zulkarnaen on 7 Feb 2020
Edited: Gifari Zulkarnaen on 7 Feb 2020
Seems you have extra parameters beside the optimization input variables.
See these explanation:
I personally like to define those extra parameters as global variables because of the convenience, although others say global variables is somehow troublesome.
For your case, try this (global method):
%% In main script:
global anzahlpipes anzahlnodes pipesmatlaberweitert inzierweitert xgv
% ...
% ...
%% In constraint function
function [c,ceq] = nonlcon(x)
global anzahlpipes anzahlnodes pipesmatlaberweitert inzierweitert xgv
% ...
% ...
end

#### 1 Comment

Kai Krickmann on 7 Feb 2020
Yeahhh it works! Thanks a lot!

Karthi Ramachandran on 7 Feb 2020
1. Is your function "nonlcon" or "unitdisk" , unit disk has no option to call "anzahlpipes"
2. if your nonlcon is a function of 'x' and the others are constants('glgew' and 'xgv') calculated, then supply as '@(x) nonlcon(x,....)'
3. The constarint looks like a linear constarint of the form Ax+b . Check it

Kai Krickmann on 7 Feb 2020
I thought since the resistance changes with each iteration it has to be non-linear. How could I put the recalculation of the resistance in a linear constraint?
Karthi Ramachandran on 7 Feb 2020
If your constriant is of the form Ax=b use "Aeq and beq" option for constarint, instead of nonlinear constarint
for a constarint of this form 2x(1)+x(2)=1 Aeq = [1 2] and beq = 1;
In your case Aeq = glgew and beq = -xgv; if I have understood it right . Pls check your function and pass the parameters correctly . It should work.
Also pls check if it is really necesaru to calvulate the constants inside the function file.