Using fmincon with for loop
4 views (last 30 days)
Show older comments
Hi!
I have a minimalization problem, where i have 3 variables (x1, x2 and x3) and two non linear equations:
x(1)+parama*x(2)*x(3)=Ptloademod(k)
paramb*x(2)+(paramc-paramb)*x(2)*x(3)=Ptloadh(k)
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:
function [c,ceq]=myf(x, Ptloademod, Ptloadh,k,paramb,paramc,parama)
c=[];
ceq(1)=x(1)+parama*x(2)*x(3)-Ptloademod(k);
ceq(2)=paramb*x(2)+(paramc-paramb)*x(2)*x(3)-Ptloadh(k);
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 :)
0 Comments
Answers (1)
Alan Weiss
on 23 Apr 2021
I think that you need to define nonlcon this way:
nonlcon = @(x)myf(x, Ptloademod, Ptloadh,paramb,paramc,parama);
Notice that I removed your k argument.
And you need to put that definition inside your loop. Something like this:
N = length(Ptloademod);
solutions = zeros(N,3);
for i = 1:N
nonlcon = @(x)myf(x, Ptloademod(i), Ptloadh(i),paramb,paramc,parama);
x = fmincon(objective,x0,A,b,Aeq,beq,lb,ub,nonclon);
solutions(i,:) = x;
end
Alan Weiss
MATLAB mathematical toolbox documentation
0 Comments
See Also
Categories
Find more on Nonlinear Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!