larg number constraints in matlab

I have an optimization problem which have a larger number of variables,N=20;
how i can define this constrain? please answer to me
Qe and Qi is a matrix (40*10)
Z=[I2g,I2h, W]';
W=sdpvar(1,2*N+2);
function [cineq,ceq]=nonlcon(Z)
N=20;
for(n=1:N)
cineq(1*n)= Qe(n,:)*PP*Z-W(1)*ones(N,1)<= -10000; %%for all N indicates a vector inequality
cineq(2*n)= -Qe(n,:)*PP*Z+W(2)*ones(N,1)<= 10000; %%for all N indicates a vector inequality
cineq(3*n)= Qi(n,:)*ZZ+500-W(3+n)<=0;
cineq(4*n)= -W(3+n)<=0;
cineq(5*n)= W(N+3+n)-Qi(n,:)*ZZ-500<=0;
cineq(6*n)= W(N+3+n)<=0; %%for all N indicates a vector inequality
cineq(7*n)=sum(W(3:N+2))-sum(W(N+3:2*N+2))<=N*Iavemax;
cineq(8*n)=500+Qi(n,:)*ZZ<=Imax*ones(N,1);
cineq(9*n)=-Imax*ones(N,1)<=500+Qi(n,:)*ZZ;
end
ceq=[];
end

 Accepted Answer

Maybe?
sdpvar() is from the third-party package YALMIP https://yalmip.github.io so whether it works or not depends upon YALMIP not on MATLAB.
The [cineq,ceq] style like that is for nonlinear constraints defined by MATLAB. I am not aware that YALMIP has any facility to call nonlinear constraints using that style.
The individual elements you assign look plausible to me to be YALMIP constraint declarations on their right-hand side.
The locations you assign to on the left are broken. Consider than when n = 1, the fourth cineq(4*n) would be assigning to cineq(4*1) = cineq(4) . But when n = 2, the second cineq(2*n) would be assigning to cineq(2*2) = cineq(4) which would overwrite the previous entry. And you never write to any location with a prime index greater than 9. cineq(13) is never assigned to, for example.

5 Comments

thank you for your answer. it's a linear and I trying to define that constrains but I haven't any success .
look at image I index this comment.
Instead of assigning to (for example), cineq(7*n), assign to cineq(7,n) to prevent overwriting what has been written before.
I do that but it still doesn't work.look at my code in m file.
when i run this code .matlab give me this error :
Error: File: LP2.m Line: 73 Column: 1
Function definitions in a script must appear at the end of the file.
Move all statements after the "nonlcon" function definition to before the first local function definition.
clc,clear,close all;
N=20;M=5;
Vdc=40000;
Idc=1000;
Iavemax=1000;
Imax=2300;
Vmax=50000;
W=sdpvar(1,2*N+2);
I2g=sdpvar;
I2h=sdpvar;
V3g=sdpvar;
V3h=sdpvar;
V1h=10000;
V1g=1000;
I1g=1200;
I1h=500;
Fo=50;
Wo=2*pi*Fo;
P=(1/2*Wo)*[V1h V1g 0 Vdc -V1h/3 V1g/3 0 0 -V3h/5 0;-V1g V1h -Vdc 0 -V1g/3 -V1h/3 0 0 -V3g/5 0;
I2h I2g I1h/2 I1g/2 0 2*Idc/3 -I1h/4 -I1g/4 0 I2g/5;-I2g I2h -I1g/2 I1h/2 -2*Idc/3 0 -I1g/4 -I1h/4 0 -I2h/10];
Z=[I2g,I2h, W]';
ZZ=[I2g,I2h]';
Y=[0,0,1,-1 -ones(1,N) ones(1,N)];
PP=[P ;zeros(2*N,2*M)]';
Enom=[3000 1000 ];
vnom=[50000 20000];
inom=[1000 200];
Qe=zeros(N,2*M);
for(n=0:N-1)
for(k=0:2*M-1)
Qe(n+1,k+1)=(1/sqrt(N))*exp(j*2*pi*k*n/N);
end
end
Ni=1;
Qi=zeros(N,2*Ni);
for(n=0:N-1)
for(k=0:2*Ni-1)
Qi(n+1,k+1)=(1/sqrt(N))*exp(j*2*pi*k*n/N);
end
end
Nv=1;
Qv=zeros(N,2*Nv);
for(n=0:N-1)
for(k=0:2*Nv-1)
Qv(n+1,k+1)=(1/sqrt(N))*exp(j*2*pi*k*n/N);
end
end
obj=Y*Z; %%tabe hadaf
function [cineq,ceq]=nonlcon(Z)
ceq=[];
for(n=1:N)
cineq(1,n)= Qe(n,:)*PP*Z-W(1)*ones(N,1)<= -Enom(n); %%for all N indicates a vector inequality
cineq(2,n)= -Qe(n,:)*PP*Z+W(2)*ones(N,1)<= Enom(n); %%for all N indicates a vector inequality
cineq(3,n)= Qi(n,:)*ZZ+inom(n)-W(3+n)<=0;
cineq(4,n)= -W(3+n)<=0;
cineq(5,n)= W(N+3+n)-Qi(n,:)*ZZ-inom(n)<=0;
cineq(6,n)= W(N+3+n)<=0; %%for all N indicates a vector inequality
cineq(7,n)=sum(W(3:N+2))-sum(W(N+3:2*N+2))<=N*Iavemax;
cineq(8,n)=inom(n)+Qi(n,:)*ZZ<=Imax*ones(N,1);
cineq(9,n)=-Imax*ones(N,1)<=inom(n)+Qi(n,:)*ZZ;
end
end
const=sum(cineq);
SS=optimize(const,obj);
z=value(Z);
You have to move
const=sum(cineq);
SS=optimize(const,obj);
z=value(Z);
to before the function statement.
Also, that sum(cineq) will have to be changed to sum(sum(cineq)) or sum(cineq(:))
thank you , but doesn't work . beacuse 'cineq' must be define before difine 'const=sum(cineq(:))'.
and when i do that give me previous Error.

Sign in to comment.

More Answers (1)

Johan Löfberg
Johan Löfberg on 14 Sep 2021
Edited: Walter Roberson on 14 Sep 2021
You're better off posting YALMIP specific questions on YALMIP specific forums

Products

Community Treasure Hunt

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

Start Hunting!