# Using fmincon with for loop

6 views (last 30 days)
Gábor Dupák on 22 Apr 2021
Answered: Alan Weiss on 23 Apr 2021
Hi!
I have a minimalization problem, where i have 3 variables (x1, x2 and x3) and two non linear equations:
Where parama, paramb and paramc are parameters defined earlier in the code. x1 x2 are >=0 and 0<=x3<=1. Ptloademod(k) is the k-th element in a predefined row vector, the same can be said about Ptloadh(k). The two row vectors contain 96-96 elements.
My objective function is:
min c1*x1+c2*x2 (where c1 and c2 are predefined constants).
I know that i have to use nonclon:
c=[];
end
And after that I have to use fmincon:
x0=[1,1,1];
A=[];
b=[];
Aeq=[];
beq=[];
lb=[0,0,0];
ub=[Inf,Inf,1];
nonlcon =@myf;
x=fmincon(objective,x0,A,b,Aeq,beq,lb,ub,nonclon);
So I would like to use a for loop to find the optimal solution for all the 96 cases and create 3 new row vectors containing the x1 x2 and x3 for the 96 cases.
Hope you can help me :)

Alan Weiss on 23 Apr 2021
I think that you need to define nonlcon this way:
Notice that I removed your k argument.
And you need to put that definition inside your loop. Something like this:
solutions = zeros(N,3);
for i = 1:N
x = fmincon(objective,x0,A,b,Aeq,beq,lb,ub,nonclon);
solutions(i,:) = x;
end
Alan Weiss
MATLAB mathematical toolbox documentation