Optimization of objective function with multiple constraints and variables

I need to minimize an objective function with four variables. The function is to be optimized under 6 nonlinear inequalities, as well as lower and upper bounds.
My basic script is this:
P = 6000;
E = 30e6;
G = 12e6;
L = 14;
fun=@(x) (1+0.10471)*x(1)*x(2)+0.04811*x(3)*x(4)*(L+1);
gs = GlobalSearch;
opts = optimoptions(@fmincon,'Algorithm','interior-point');
problem = createOptimProblem('fmincon','x0',[0.1,0.1,0.1,0.1],'objective',fun,'lb',[0.125,0.1,0.1,0.1],'ub',[5,10,10,5],'nonlcon',nonlcon,'options',opts);
where
function [c,ceq] = nonlcon(~)
P = 6000;
E = 30e6;
G = 12e6;
L = 14;
c =@(x) [-13600 + sqrt((P/(sqrt(2)*x(1)*x(2)))^2+2*(P/(sqrt(2)*x(1)*x(2)))*((P*(L+x(2)/2))*(sqrt((x(2)^2)/4+...
((x(1)+x(3))/2)^2)))/(2*(sqrt(2)*x(1)*x(2)*((x(2)^2)/12+((x(1)+x(3))/2)^2))))*x(2)/(2*(sqrt((x(2)^2)/4+...
((x(1)+x(3))/2)^2)))+((P*(L+x(2)/2))*(sqrt((x(2)^2)/4+...
((x(1)+x(3))/2)^2)))/(2*(sqrt(2)*x(1)*x(2)*((x(2)^2)/12+((x(1)+x(3))/2)^2)))^2;
-30000+(6*P*L)/(x(4)*x(3)^2);x(1)-x(4);0.10471*x(1)^2+0.04811*x(3)*x(4)*(14+x(2))-5;...
0.125-x(1);(4*P*L^3)/(E*x(4)*x(3)^3)-0.25; P-4.013*E*sqrt((x(3)^2*x(4)^6)/36)*(1-x(3)*sqrt(E/(4*G))/(2*L))/(L^2)];
ceq = [];
end
Up until that point, my script runs with no issues. However when I try to insert a way to run the problem and display some results I get errors. One way I tried is
min=run(gs,problem);
and I get
Error using fmincon (line 626)
The constraint function must return two outputs; the nonlinear inequality constraints and the nonlinear equality constraints.
Error in globalsearchnlp
Error in GlobalSearch/run (line 340)
globalsearchnlp(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,options,localOptions);
Error in confuneq1 (line 23)
min=run(gs,problem);
Caused by:
Failure in initial call to fmincon with user-supplied problem structure.
And another one I tried is
sol=solve(problem);
and I get
Error using char
Conversion to char from struct is not possible.
Error in solve>isOption (line 459)
b = ~isa(a, 'logical') && any(strcmpi(char(a), ...
Error in solve>getEqns (line 392)
while k <= numel(argv) && ~isOption(argv{k})
Error in solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
Error in confuneq1 (line 22)
sol=solve(problem);
I would appreciate some help. I don't know if the problem creation is wrong, or the ways I try to run it, or both. Thanks

 Accepted Answer

Hi,
using an anonymus function works here:
P = 6000;
E = 30e6;
G = 12e6;
L = 14;
fun=@(x)(1+0.10471)*x(1)*x(2)+0.04811*x(3)*x(4)*(L+1);
c = @(x)[-13600 + sqrt((P/(sqrt(2)*x(1)*x(2)))^2+...
2*(P/(sqrt(2)*x(1)*x(2)))*((P*(L+x(2)/2))*(sqrt((x(2)^2)/4+...
((x(1)+x(3))/2)^2)))/(2*(sqrt(2)*x(1)*x(2)*((x(2)^2)/12+((x(1)+...
x(3))/2)^2))))*x(2)/(2*(sqrt((x(2)^2)/4+...
((x(1)+x(3))/2)^2)))+((P*(L+x(2)/2))*(sqrt((x(2)^2)/4+...
((x(1)+x(3))/2)^2)))/(2*(sqrt(2)*x(1)*x(2)*((x(2)^2)/12+((x(1)+...
x(3))/2)^2)))^2;
-30000+(6*P*L)/(x(4)*x(3)^2);
x(1)-x(4);
0.10471*x(1)^2+0.04811*x(3)*x(4)*(14+x(2))-5;
0.125-x(1);(4*P*L^3)/(E*x(4)*x(3)^3)-0.25;
P-4.013*E*sqrt((x(3)^2*x(4)^6)/36)*(1-x(3)*sqrt(E/(4*G))/(2*L))/(L^2)];
ceq = @(x)[];
nonl_con = @(x)deal(c(x),ceq(x));
gs = GlobalSearch;
opts = optimoptions(@fmincon,'Algorithm','interior-point');
problem = createOptimProblem('fmincon','x0',[0.1;0.1;0.1;0.1],'objective',fun,'lb',[0.125,0.1,0.1,0.1],'ub',[5,10,10,5],'nonlcon',nonl_con,'options',opts);
min=run(gs,problem);
this gives following result:
GlobalSearch stopped because it analyzed all the trial points.
10 out of 11 local solver runs converged with a positive local solver exit flag.
with min:
>> min
min =
0.2057
0.5305
9.0366
0.2057
Best regards
Stephan

8 Comments

The nice thing is that you have a solution to your problem. The unsatisfactory thing is that I can not tell you why an anonmye function works for the nonlinear constraints, but not a "normal" function.
Does anyone have any idea why that is?
Best regards
Stephan
In order to pass a function reference as an argument to another function, it is necessary to pass it as a function handle.
I did not run the code to test this, however adding the ‘@’ to create a function handle from nonlcon, specifically: ‘...,'nonlcon',@nonlcon,...’ in the argument list, would probably work.
The anonymous function creates a function handle, eliminating that problem.
while we are on the subject. What if I have lower and upper bounds that depend on the variables? How do I introduce that in the fmincon problem?
For example I have lower bounds [0.01*x(1)*x(2), 28, 5] and upper bounds [0.04*x(1)*x(2), 40, 10] for three variables.
I guess it is better to ask this in a new question.
You would use nonlinear constraints for that.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!