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

12 次查看(过去 30 天)
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 个评论
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