Hi,
the usage of global variables is not recommended. You missed to define the function handles Fcost1_4 ... Fcost3_4 in Fcompanies_4.m - try this instead:
clc, clear all
global M H a1 a2 a3 b1 b2 b3 Lh umh u1h u2h u3h u4h Ch a4 Ge Ip
M=4;
H=24;
a1(1:7,1)=0.004;
a1(8:24,1)=0.00084;
a2(1:7,1)=0.0006;
a2(8:24,1)=0.00078;
a3(1:7,1)=0.0005;
a3(8:24,1)=0.00080;
b1(1:7,1)=0.064;
b1(8:24,1)=0.064;
b2(1:7,1)=0.046;
b2(8:24,1)=0.063;
b3(1:7,1)=0.044;
b3(8:24,1)=0.065;
r=0.09;
Ns=20;
a4= r/[1-(1+r)^-Ns];
Ge=1.6;
Ip=5000;
dv=H;
Lh=rand(1,H);
umh_i=rand(1,H*M);
u1h=umh_i(1,1:24);
u2h=umh_i(1,25:48);
u3h=umh_i(1,49:72);
u4h=umh_i(1,73:96);
current_m=1;
x0 = u1h;
ub = ones(dv,1);
lb = zeros(dv,1);
Fcost1_4 = @(x)globalfun(x);
Fcost2_4 = @(x)globalfun(x);
Fcost3_4 = @(x)globalfun(x);
options = optimoptions('fmincon','Algorithm','interior-point','Display','iter');
[x,fval] =fmincon(Fcost1_4,x0,[],[],[],[],lb,ub,[],options);
if(x~=u1h)
u1h=x;
end
current_m=2;
x0=u2h;
ub=1-(u1h);
lb=zeros(dv,1);
options = optimoptions('fmincon','Algorithm','interior-point','Display','iter');
[x,fval] =fmincon(Fcost2_4,x0,[],[],[],[],lb,ub,[],options);
if(x~=u2h)
u2h=x;
end
current_m=3;
x0=u3h;
ub=1-(u1h+u2h);
lb=zeros(dv,1);
options = optimoptions('fmincon','Algorithm','interior-point','Display','iter');
[x,fval] =fmincon(Fcost3_4,x0,[],[],[],[],lb,ub,[],options);
if(x~=u3h)
u3h=x;
end
current_m=4;
u4h=1-(u1h+u2h+u3h);
[C]=F_final();
Ch=C;
umh=u1h+u2h+u3h+u4h;
In Fcost1.m your output is a function handle - try this instead:
function y = globalfun(x)
global M H a1 a2 a3 b1 b2 b3 Lh umh u1h u2h u3h
for h=1:H
y = (a1(h)*(Lh(h)*x(h)).^2 + b1(h)*(Lh(h)*x(h)) + a2(h)*(Lh(h)*u2h(h)).^2 + b2(h)*(Lh(h)*u2h(h)) + a3(h)*(Lh(h)*u3h(h)).^2 + b3(h)*(Lh(h)*u3h(h)));
end
end
These changes fix the code to run up to the last three lines:
F_final is not defined - you should fix this and it should work. But you wil not get a price for clean coding... ;-)
In your globalfun you overwrite the result of y in every single run of your loop - i guess this is not what you want. Check the results - they may be not the expected ones, due to coding errros i cant identify, becaus of no insight to the problem.
Best regards
Stephan
Best Answer