# Error Not enough input arguments using Fmincon with nlcon and linear constraints

2 views (last 30 days)
ikram shahzad on 1 Nov 2018
Commented: ikram shahzad on 4 Nov 2018
I am trying to minimize a function say fun using fmincon, it has 18 elements, having linear and non-linear constraints. linear constraints are applied on solution s, s(1) to s(7). and 2 nonlcon constraints are applied on solution s(8) to s(18).optimization is good for linear constraints but shows error using nonlcon function(@test3), not enough input arguments.
C_test_43.csv, file is required to run this code, it is attached with.
% code
clc
clear
time=50;
C=M(1:time);
Cmax=max(C);
T=10;
n=time/T;
X=zeros(1,n);
X(1,1)=8;
x=zeros(1,time);
x(1,1)=8;
E=1+(25-1).*rand(1,time);
batcharg=1;
Ve=(25-(2*batcharg))/Cmax;
Vt=(25-(2*batcharg))/Cmax;
V=(25-(2*batcharg))/Cmax;
m=11;
fr = 0 + (16.8*10^9-0).*rand(m,time);
fmax=max(fr);
fmax=max(fmax);
p = 0 + (2*10^-3-0).*rand(m,time);
pmax=max(p);
pmax=max(pmax);
Q=rand(1,time);
QD=rand(1,time);
a=0.1;
bb=0.025;
k=7.8*10^-21;
L=6400;
Hm=-50 + (-75-50).*rand(m,time);
phi=0.1;
w=1*10^6;
N=-174;
tau=1;
DL=tau.*fr./L;
D=w.*tau.*log2(1+(Hm.*p)/N.*w);
Td=0+(20-0).*rand(1,time);
vlb=zeros(1,m+7);
vub=zeros(1,m+7);
num=m+7;
energy_ub=1500;
f=zeros(1,num);
% code
fun=@(s)f;
for t=1:time
n=ceil(t/T);
freq=fr(1:m,t);
f(1,1)=X(n);
f(1,2)=x(t);
f(1,3)=V.*C(n);
f(1,4)=Q(t);
f(1,5)=QD(t);
f(1,6)=Ve;
f(1,7)=Vt;
for u=8:m+7
f(1,u)=min(DL(u-7,t)+D(u-7,t),D(u-7,t)+D(u-7,t));
end
i=1;
j=m+7;
Aeq=ones(i,j);
for j=8:m+7
Aeq(i,j)=-1;
end
beq=1;
for j=8:m+7
vlb(j)=0;
vub(j)=fmax;
end
vlb(1)=-batcharg;
vub(1)=batcharg;
vlb(2)=-1;
vub(2)=1;
vub(3)=energy_ub;
vlb(4)=-1;
vub(4)=1;
vlb(5)=-1;
vub(5)=1;
vlb(6)=0;
vub(6)=1000;
vlb(7)=0;
vub(7)=20;
if X(n)>-V*C(n)
vub(1)=1;
else
vlb(1)=-1;
end
if x(t)>-V*C(t)
vub(2)=1;
else
vlb(2)=-1;
end
if Q(t)>-Ve*E(t)
vub(4)=1;
else
vlb(4)=0;
end
if QD(t)>-Vt*Td(t)
vub(5)=1;
else
vlb(5)=0;
end
nonlcon = @test3;
[s,fval]=fmincon(fun,freq,[],[],Aeq,beq,vlb,vub,nonlcon(tau,L,m));
% function for 2 non linear constraints
function [c,ceq] = test3(s,tau,L,m)
for n=8:m+7
ceq(1) =tau.*(s(n)/L);
ceq(2)=w(n).*tau.*log2(1+(Hm.*p)/N.*w(n));
end
c = [];
end

#### 1 Comment

Walter Roberson on 1 Nov 2018
Search for Paramaterizing Functions

Nicolas Schmit on 2 Nov 2018
There are several problems in your code
• You must pass the optimization variable as a argument to the nonlinear constraints.
• The size of Aeq, Beq, vlb and vub do not match the size of the freq vector.
• The cost function must return a scalar.
I suggest you have a look at the examples in the documentation of fmincon.

Show 2 older comments
ikram shahzad on 3 Nov 2018
Thanks to Walter Roberson and Nicolas Schmit for suggestions. I tried the examples in following link but these are very simple https://www.mathworks.com/help/optim/ug/fmincon.html#busow0u-1
I also tried
x = fgoalattain(@myfun,x0,goal,weight,A,b,Aeq,beq,...
lb,ub,@mycon)
where mycon is a MATLAB function such as
function [c,ceq] = mycon(x)
c = ... % compute nonlinear inequalities at x.
ceq = ... % compute nonlinear equalities at x.
function[c ,ceq]=test3(s,tau,L) results a vector but fmincon needs a scalar. when i use fmincon it shows error :
(line 609) Supplied objective function must return a scalar value.
when i use fgoatattain, it gives error
too many input arguments
moreover, i am confused to select values for goal in my code, what is criteria to choose goal values for fgoalattain?
Thanks for your help
Walter Roberson on 3 Nov 2018
You still have
fun=@(s)f;
which tells it to ignore the input s and to always return the content of f, a constant vector. Are you sure that is what you want to do? Is your purpose to try to find a point that violates the constraints the least -- that you do not actually care about the return value of myfun and only want to try to figure out where some points are that (nearly) satisfy the constraints?
Perhaps you want something like
fun = @(s) f*s;
??
ikram shahzad on 4 Nov 2018
Thanks Thanks to Walter Roberson. my simulation got successful by defining fun=@(s)X(n)*s(1)+x(t)*s(2)+V*C(n)*s(3)+Q(t)*s(4)+QD(t)*s(5)+Ve*s(6)+Vt*s(7)+f(1,u)*s(u); Thank you very much.