필터 지우기
필터 지우기

fmincon gradient of nonlinear inequality constraints must have size???

조회 수: 9 (최근 30일)
Ali Esmaeilpour
Ali Esmaeilpour 2019년 9월 7일
편집: Matt J 2019년 9월 8일
Greeting guys! I run my code and I get the following error:
"Gradient of nonlinear inequality constraints must have size 4-by-12."
My code includes two objective functions and two nonlinear inequality constraints and I'm using fmincon to solve my problem and I added gradient of my nonlinear constraints and got error.
clc;
clear;
close all;
%% Problem Definition
nVar=4;
VarSize=[1 nVar];
VarMin=10^(-3);
VarMax=10^2;
%% Weighted-Sum Approach
%N=60;
%w1=linspace(0,1,N);
%w2=1-w1;
FWS=@(x) MyCost(x);
x0=unifrnd(VarMin,VarMax,VarSize);
x0=x0';
A=[];
b=[];
LB=[10^(-3);10^(-3);10^(-3);10^(-3)];
UB=[10^2;10^2;10^2;10^2];
nonlcon = @NLC2;
options=optimoptions('fmincon','Display','iter','Algorithm','interior-point','SpecifyConstraintGradient',true,'TolFun',10^(-12),'TolCon',10^(-10),'MaxFunEvals',4*10^3);
[x,fval,exitflag,output,lambda,grad,hessian]=fmincon(FWS,x0,A,b,[],[],LB,UB,nonlcon,options)
function [c,ceq,gradc,gradceq]=NLC2(x)
n=2;
c(1,:)=[Obj1(x)-x(n:end);-x(n:end)];
c(2,:)=[Obj2(x)-x(n:end);-x(n:end)];
ceq=[];
if nargout>1
gradc(1,:)=[gradient(Obj1(x)-x(n:end),x);gradient(-x(n:end),x)];
gradc(2,:)=[gradient(Obj2(x)-x(n:end),x);gradient(-x(n:end),x)];
gradceq=[];
end
end
function z = MyCost(x)
K=3;
s=zeros(1,K);
for i=1:K
s(i) = (10^(K-i))*2;
end
S = diag(s);
M=60;
w1=linspace(0,1,M);
w2=1-w1;
for j=1:M
z1 = w1(j)*Obj1(x);
z2 = w2(j)*Obj2(x);
end
n=2;
z = z1+z2+x(n:end)'*S*x(n:end);
end
function z = Obj2(x)
yref = 0;
p1 = -0.0053913;
p2 = 0.011457;
p3 = 0.039991;
p4 = -0.1433;
p5 = 0.070996;
p6 = 0.31844;
p7 = -0.64456;
p8 = 0.33539;
p9 = 0.30767;
p10 = -0.37571;
p11 = 0.071788;
mu = 50.5;
sigma = 29.011;
n=2;
g = (x(1:n-1)-mu)/sigma;
z = sum(((yref-(p1*g.^10+p2*g.^9+p3*g.^8+p4*g.^7+p5*g.^6+p6*g.^5+p7*g.^4+p8*g.^3+p9*g.^2+p10*g+p11)).^2));
end
function z = Obj1(x)
yref = 0;
p1 = -6.2039e-06;
p2 = 0.012729;
p3 = -0.040506;
p4 = -0.035217;
p5 = 0.28562;
p6 = -0.3329;
p7 = -0.19587;
p8 = 0.75971;
p9 = -0.54118;
p10 = -0.021845;
p11 = 0.098187;
mu = 50.5;
sigma = 29.011;
n=2;
g = (x(1:n-1)-mu)/sigma;
z = sum(((yref-(p1*g.^10+p2*g.^9+p3*g.^8+p4*g.^7+p5*g.^6+p6*g.^5+p7*g.^4+p8*g.^3+p9*g.^2+p10*g+p11)).^2));
end

답변 (1개)

Matt J
Matt J 2019년 9월 7일
What is mysterious about the error message? It has told you that your gradc output has to be 4x12 and in your NLC2 you clearly only give it 2 rows.
if nargout>2
gradc(1,:)=[gradient(Obj1(x)-x(n:end),x);gradient(-x(n:end),x)];
gradc(2,:)=[gradient(Obj2(x)-x(n:end),x);gradient(-x(n:end),x)];
gradceq=[];
end
Why are gradc(3,:) and gradc(4,:) omitted?
  댓글 수: 15
Ali Esmaeilpour
Ali Esmaeilpour 2019년 9월 7일
yeah done already. Obj1(x) and Obj2(x) and MyCost(x) are for that purpose.
Matt J
Matt J 2019년 9월 7일
편집: Matt J 2019년 9월 8일
No, the function I asked you to write depends on x, i, and k. Obj1 and Obj2 are not in that form.
In any case, I don't want to refer to your original code. There are too many mistakes in it to know for certain what is intended and how things there are supposed to match up with your equations. For example, I don't see a variable called y in your code like there is in your equations. I don't see a variable called F. I don't see i, k, or delta,...
So let's start from scratch, starting with the functional form of y_ik(x):
function y=closedloopresponse(x,i,k)
%%% What goes in here????
end

댓글을 달려면 로그인하십시오.

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by