How to make a optimization program general for different nonlinear inequality constraints using fmincon function?
Show older comments
I have an optimization problem with different nonlinear inequality.I write the MATLAB code using "fmincon" given below. Now,i want to run the same code for general equation instead of making different file for different equation.How can i do it?
Here,I give the sample code that i make for same optimization problem but different nonlinear inequality.
This code for same optimization problem with two nonlinear inequality.**
m=5;
N1=[1;2;3;4;6];
for i=1:m
N=[N1(i)];
L=2;
if(mod(N,L)==0)
a=((2*pi)./(N./L));
r=20;
d=500;
v=10;
c = @(x)[ (((a.*x(1).^2./(4*r)) + ((a/2 + 2).*x(1)))./v)- x(3);
(((((a.*x(2).^2))./(4*r))+((((a.*x(1))./(2*r))+(a./2)+2).*x(2))-(4*r))./v)-x(3)];
ceq = @(x)[];
nonlinfcn = @(x)deal(c(x),ceq(x));
obj = @(x)x(3);
Aeq=[1 1 0];
beq = [d];
opts = optimoptions(@fmincon,'Algorithm','interior-point');
n=2;
x10=randi([10 100000],10,1);
x20=randi([10 200000],10,1);
x30=randi([100 1500000],10,1);
for j=1:n
x0=[x10(j);x20(j);x30(j)];
[x(:, i),fval] = fmincon(obj,x0,[],[],Aeq,beq,[10;10;100],[500;500;100000000],nonlinfcn,opts);
disp(x);
end
else
fval=0;
end
end
This code for same optimization problem with three nonlinear inequality.**
m=5;
N1=[1;2;3;4;5];
for i=1:m
N=[N1(i)];
L=3;
if(mod(N,L)==0)
r=20;
d=500;
a=((2*pi)./(N./L));
v=10;
c = @(x) [ (((a.*x(1).^2./(4*r)) + ((a/2 + 2).*x(1)))./v)- x(4);
(((((a.*x(2).^2))./(4*r))+((((a.*x(1))./(2*r))+(a./2)+2).*x(2))-(4*r))./v)-x(4);
(((((a.*x(3).^2))./(4*r))+((((a.*(x(1)+x(2)))./(2*r))+(a./2)+2).*x(3))- (4*r))./v)-x(4)];
ceq = @(x)[];
nonlinfcn = @(x)deal(c(x),ceq(x));
obj = @(x)x(4);
Aeq=[1 1 1 0];
beq = [d];
opts = optimoptions(@fmincon,'Algorithm','interior-point');
n=2;
x10=randi([10 100000],10,1);
x20=randi([10 200000],10,1);
x30=randi([10 200000],10,1);
x40=randi([100 1500000],10,1);
for j=1:n
x0=[x10(j);x20(j);x30(j);x40(j)];
[x(:, i),fval] = fmincon(obj,x0,[],[],Aeq,beq,[10;10;10;100],[500;500;500;100000000],nonlinfcn,opts);
disp(x);
end
else
fval=0;
end
end
I want to make single code instead of two code for same optimization problem. Please,help anyone...Thanks in advance...
Answers (0)
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!