How to write cell function in constraint optimization
Afficher commentaires plus anciens
Hi
I have written code for minimizing a problem. It is giving error messages. Specially in the 'gcon' constraint function file. I have no idea where I did wrong.
Would be greatly appreciated your kind help.
Constraint file named 'gcon'
function [S] = gcon(x,y)
N = 3; % number of subdivisions
n = 3;
alt=2;
for i = 2:n
for j = 2:n
for k = 1:N
if (i==2 && j==2)
z(i,j,k) = 8 ;
elseif (i==2 && j==3)
z(i,j,k) = 8 ;
elseif (i==3 && j==2)
z(i,j,k) = 2 ;
elseif (i==3 && j==3)
z(i,j,k) = 2 ;
end
end
end
end
for i = 1:n-1
for j = 2:n
for k = 1:N
if (i==1 && i<j)
s(i,j,k) = (x(k) - z(j,2,k))^2 + (y(k) - z(j,3,k))^2 + alt^2;
elseif (i<j)
s(i,j,k) = (z(i,2,k) - z(j,2,k))^2 + (z(i,3,k) - z(j,3,k))^2;
end
end
end
end
for k = 1:N
S(k)=max([(min(s(1,2,k),s(1,3,k)))^2, (min(s(1,2,k),s(2,3,k)))^2, (min(s(1,3,k),s(2,3,k)))^2])^2-alpha;
end
Objective function file is
function p = Objs(alpha)
p = alpha;
end
The script file is
clc
tic
N = 3;
n = 3;
x0=5*ones(1,n);
alt = 2;
%options = optimset('Algorithm','active-set');
%options = optimoptions(@fmincon, 'Algorithm','active-set','MaxIter',5000);
%options = optimoptions(@fmincon, 'Algorithm','interior-point');
options = optimoptions(@fmincon,'Algorithm','sqp','MaxIter',5000);
lb=[];
ub=[];
f1_min = fmincon(@Objs,x0,[],[],[],[],lb,ub,@gcon,options)
toc
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Problem-Based Optimization Setup dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!