[fmincon] How should I write an objective function which links to other functions rather than directly using "x"
Show older comments
Hello,
I have a problem about writing an objective function. How should I write the objective function that links to other functions of which x is expressed in order to pass the decision variable x to fmincon ? Below is an example
%Objective File
function TotCost=objfuntion(x)
global CapCost FuCost Demand % CapCost FuCost and Demand will be reused in constraint file
x0=100*ones(12,1);
x=x0;
%MW and CF are real decision variables
MW=reshape(x(1:6,1),[3,2]);%convert a part of vector x to the matrix of variable "MW" dimension 3x2
CF=reshape(x(7:12,1),[3,2]);%convert the rest of vector x to the matrix of variable "CF" dimension 3x2
U=[2 3 4];
V=[3.5; 2.5; 1.5];
W=repmat(V,1,2);
S=[1 3 5];
CapCost=sum(U*(MW.^W));
FuCost=sum(S*(MW.*CF/100));
Demand=sum(sum(MW.*CF/100,1));
TotCost = CapCost+FuCost; %objective function
lb=zeros(12,1);
MWMax = [200; 300; 400];
CFMax = [85; 75; 45];
ub=[MWMax; MWMax; CFMax; CFMax];
options=optimset('MaxFunEvals',Inf,'MaxIter',5000,'Algorithm','interior-point');
[x,fval,exitflag,output]=fmincon(@objfuntion,x0,[],[],[],[],lb,ub,@confuntion,options)
%Constraint File
function [c,ceq]=confuntion(x)
global CapCost FuCost Demand
c=[500-Demand; CapCost-20000000; FuCost-1500];
ceq=[];
TotCost is the objective function which I want to minimize.
By coding like this, I got an error saying "Failure in initial user-supplied objective function evaluation. FMINCON cannot continue"
Any comment is appreciated !!
Thank you
Accepted Answer
More Answers (0)
Categories
Find more on Choose a Solver 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!