Error in fmincon : how to write "fun" when x is an array?
Show older comments
Hi,
I'm using the fmincon function to solve a NLP(Nonlinear problem).The x of the problem is an array(6*7). So I'm a little confused about how to write myfun(the objective function) and the nonlcon(the nonlinear constraints).
Many thanks in advance for your help!
Best wishes!
I think I should post my error first:

myfun is a scalar function of x(an array,6*7), with several other parameters(such as L,G,G1,N0) which are all defined and assigned in test.m. To calculate the objective function's value, I need to calculate some variables first, such as SINR,C and I ,using variable x and the assigned parameters. And to calculate f, I used for-loops.( Question 1 :Can I use for-loops to express the objective function, rather than define the function with a equation as usual? )
The variables SINR,C and I are also needed in the nonlcon. So I defined them as global variables in myfun.m. What confused me is that when I write mycon(the nonlcon) I find out I don't need the original variable x ,I only need the intermediate variables SINR,C and I. Is is permitted to write mycon without variable x? (That's the Question 2)
And the result of mycon: [c ceq],c is an array(2*7),ceq=[].
Question 3 : Is it right to write like this?
f=@(x)myfun(x,L,G,G1,N0);
nonlcon = mycon(L);
x = fmincon(f,x0,A,b,Aeq,beq,lb,ub,nonlcon);
And here are myfun.m and mycon.m for reference.
%=======================myfun.m===============================
function f = myfun1(x,L,G,G1,N0)
f=0;
global SINR;
SINR=zeros(5,7);
global I;
I=zeros(5,7);
global C;
C=zeros(1,7);
% -------------------to calculate I---------------------------
for k=1:5
for n=1:7
for m=1:7
t=x(k,n)*x(k,m)*x(6,m)*G1(n,m);
I(k,n)=I(k,n)+t;
end
end
end
% -------------------to calculate SINR--------------------------
for k=1:5
for n=1:7
SINR(k,n)=x(k,n)*x(6,n)*G(n,n)/(I(k,n)+N0);
end
end
% ---------to calculate C and the objective function's value f----
for n=1:7
for k=1:5
t=x(k,n)*log2(1+SINR(k,n));
C(1,n)=t+C(1,n);
end
t2=L(2,n)*C(1,n);
f=f+t2;
end
%============================mycon.m=================================
function [ c,ceq ] = mycon1(L)
c=zeros(2,7);
global I;
global C;
for n=1:7
for k=1:5
c(2,n)=c(2,n)+I(k,n);
end
c(1,n)=L(3,n)*(tran(L(2,n))*C(1,n)+tran(L(2,L(1,n)))*C(1,L(1,n)));
c(2,n)=c(2,n)-3;
end
ceq=[];
Accepted Answer
More Answers (1)
Most importantly noncon needs to be a function (of x, which does not need to be used inside the function). Personally I do not trust globals (search this forum to know why), so I rewrote your code using nested functions, which are more robust and much easier to debug.
Note that the loops most likely could be vectorized. I also did not run your code (you did not supply tran, nor did I check the algorithm.
1 Comment
Guan Chen
on 1 May 2017
Categories
Find more on 基于求解器的非线性优化 in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!