file 1)
function f= objet_cost30(x)
f=((100*x(1))-(5.81*x(1))+(50*x(1)))/20+ ((450*x(2))-(26.14*x(2))+(86*x(2)))/20+((500*x(3))+(200*x(3)))/20 ;
file 2)
function [c,ceq]= con_objet30(x)
% wind power
pw(1)= 2.412 *x(1) ;
pw(2)= 0.512 *x(1) ;
pw(3)= 1.321 *x(1) ;
pw(4)= 1.079 *x(1) ;
pw(5)= 3.538 *x(1) ;
pw(6)= 3.538 *x(1) ;
pw(7)= 3.609 *x(1) ;
pw(8)= 4.621 *x(1) ;
pw(9)= 4.055 *x(1) ;
pw(10)= 4.706 *x(1) ;
pw(11)= 4.212 *x(1) ;
pw(12)= 3.828 *x(1) ;
pw(13)= 5.055 *x(1) ;
pw(14)= 3.538 *x(1) ;
pw(15)= 3.263 *x(1) ;
pw(16)= 2.524 *x(1) ;
pw(17)= 2.638 *x(1) ;
pw(18)= 3.330 *x(1) ;
pw(19)= 3.681 *x(1) ;
pw(20)= 2.940 *x(1) ;
pw(21)= 2.468 *x(1) ;
pw(22)= 2.000 *x(1) ;
pw(23)= 1.905 *x(1) ;
pw(24)= 1.680 *x(1) ;
% solar power
ps(1)= 0.000 *x(2) ;
ps(2)= 0.000 *x(2) ;
ps(3)= 0.000 *x(2) ;
ps(4)= 0.000 *x(2) ;
ps(5)= 0.000 *x(2) ;
ps(6)= 0.000 *x(2) ;
ps(7)= 0.000 *x(2) ;
ps(8)= 0.000 *x(2) ;
ps(9)= 0.025 *x(2) ;
ps(10)= 0.046 *x(2) ;
ps(11)= 0.060 *x(2) ;
ps(12)= 0.067 *x(2) ;
ps(13)= 0.066 *x(2) ;
ps(14)= 0.058 *x(2) ;
ps(15)= 0.043 *x(2) ;
ps(16)= 0.022 *x(2) ;
ps(17)= 0.000 *x(2) ;
ps(18)= 0.000 *x(2) ;
ps(19)= 0.000 *x(2) ;
ps(20)= 0.000 *x(2) ;
ps(21)= 0.000 *x(2) ;
ps(22)= 0.000 *x(2) ;
ps(23)= 0.000 *x(2) ;
ps(24)= 0.000 *x(2) ;
% power demand
pd(1)= 2716.920 ;
pd(2)= 2706.120 ;
pd(3)= 2746.800 ;
pd(4)= 2672.280 ;
pd(5)= 2696.400 ;
pd(6)= 2872.800 ;
pd(7)= 2708.640 ;
pd(8)= 2586.240 ;
pd(9)= 2556.720 ;
pd(10)= 2522.160 ;
pd(11)= 2493.000 ;
pd(12)= 2565.000 ;
pd(13)= 2561.760 ;
pd(14)= 2631.240 ;
pd(15)= 2729.520 ;
pd(16)= 2825.280 ;
pd(17)= 2961.720 ;
pd(18)= 3181.320 ;
pd(19)= 3155.040 ;
pd(20)= 3122.280 ;
pd(21)= 3058.200 ;
pd(22)= 3155.760 ;
pd(23)= 3097.440 ;
pd(24)= 3003.120 ;
% minimum permitted storage level
pbmin=3;
% soc
soc(1)=0;
soc(2)=soc(1);
soc(3)=soc(2);
soc(4)=soc(3);
soc(5)=soc(4);
soc(6)=soc(5);
soc(7)=soc(6);
soc(8)=soc(7);
soc(9)=soc(8);
soc(10)=soc(9);
soc(11)=soc(10);
soc(12)=soc(11);
soc(13)=soc(12);
soc(14)=soc(13);
soc(15)=soc(14);
soc(16)=soc(15);
soc(17)=soc(16);
soc(18)=soc(17);
soc(19)=soc(18);
soc(20)=soc(19);
soc(21)=soc(20);
soc(22)=soc(21);
soc(23)=soc(22);
soc(24)=soc(23);
for i= 1:24;
a(i)= pw(i)+ps(i); % total power
b(i)= a(i)-pd(i); % surplus power
c(i)= b(i)*0.82; % charge
d(i)= (soc(i)-pbmin)/0.82; % discharge
g(i)= pbmin-soc(i);
z(i)= soc(i)-x(3);
if a(i) > pd(i)
t(i)=1;
uu(i)=0;
u(i)=0;
pb(i)=soc(i)+c(i); % 충전
else
t(i)=0;
uu(i)=1;
u(i)=1;
pb(i)=soc(i)-d(i);
end
% battery charge or discharge
batt(1)= soc(1)+(c(1)*t(1))-(d(1)*uu(1));
batt(2)= soc(2)+(c(2)*t(2))-(d(2)*uu(2));
batt(3)= soc(3)+(c(3)*t(3))-(d(3)*uu(3));
batt(4)= soc(4)+(c(4)*t(4))-(d(4)*uu(4));
batt(5)= soc(5)+(c(5)*t(5))-(d(5)*uu(5));
batt(6)= soc(6)+(c(6)*t(6))-(d(6)*uu(6));
batt(7)= soc(7)+(c(7)*t(7))-(d(7)*uu(7));
batt(8)= soc(8)+(c(8)*t(8))-(d(8)*uu(8));
batt(9)= soc(9)+(c(9)*t(9))-(d(9)*uu(9));
batt(10)= soc(10)+(c(10)*t(10))-(d(10)*uu(10));
batt(11)= soc(11)+(c(11)*t(11))-(d(11)*uu(11));
batt(12)= soc(12)+(c(12)*t(12))-(d(12)*uu(12));
batt(13)= soc(13)+(c(13)*t(13))-(d(13)*uu(13));
batt(14)= soc(14)+(c(14)*t(14))-(d(14)*uu(14));
batt(15)= soc(15)+(c(15)*t(15))-(d(15)*uu(15));
batt(16)= soc(16)+(c(16)*t(16))-(d(16)*uu(16));
batt(17)= soc(17)+(c(17)*t(17))-(d(17)*uu(17));
batt(18)= soc(18)+(c(18)*t(18))-(d(18)*uu(18));
batt(19)= soc(19)+(c(19)*t(19))-(d(19)*uu(19));
batt(20)= soc(20)+(c(20)*t(20))-(d(20)*uu(20));
batt(21)= soc(21)+(c(21)*t(21))-(d(21)*uu(21));
batt(22)= soc(22)+(c(22)*t(22))-(d(22)*uu(22));
batt(23)= soc(23)+(c(23)*t(23))-(d(23)*uu(23));
batt(24)= soc(24)+(c(24)*t(24))-(d(24)*uu(24));
% energy not serve
e(1)=(pbmin-(soc(1)+(c(1)*t(1)))-b(1))*u(1);
e(2)=(pbmin-(soc(2)+(c(2)*t(2)))-b(2))*u(2);
e(3)=(pbmin-(soc(3)+(c(3)*t(3)))-b(3))*u(3);
e(4)=(pbmin-(soc(4)+(c(4)*t(4)))-b(4))*u(4);
e(5)=(pbmin-(soc(5)+(c(5)*t(5)))-b(5))*u(5);
e(6)=(pbmin-(soc(6)+(c(6)*t(6)))-b(6))*u(6);
e(7)=(pbmin-(soc(7)+(c(7)*t(7)))-b(7))*u(7);
e(8)=(pbmin-(soc(8)+(c(8)*t(8)))-b(8))*u(8);
e(9)=(pbmin-(soc(9)+(c(9)*t(9)))-b(9))*u(9);
e(10)=(pbmin-(soc(10)+(c(10)*t(10)))-b(10))*u(10);
e(11)=(pbmin-(soc(11)+(c(11)*t(11)))-b(11))*u(11);
e(12)=(pbmin-(soc(12)+(c(12)*t(12)))-b(12))*u(12);
e(13)=(pbmin-(soc(13)+(c(13)*t(13)))-b(13))*u(13);
e(14)=(pbmin-(soc(14)+(c(14)*t(14)))-b(14))*u(14);
e(15)=(pbmin-(soc(15)+(c(15)*t(15)))-b(15))*u(15);
e(16)=(pbmin-(soc(16)+(c(16)*t(16)))-b(16))*u(16);
e(17)=(pbmin-(soc(17)+(c(17)*t(17)))-b(17))*u(17);
e(18)=(pbmin-(soc(18)+(c(18)*t(18)))-b(18))*u(18);
e(19)=(pbmin-(soc(19)+(c(19)*t(19)))-b(19))*u(19);
e(20)=(pbmin-(soc(20)+(c(20)*t(20)))-b(20))*u(20);
e(21)=(pbmin-(soc(21)+(c(21)*t(21)))-b(21))*u(21);
e(22)=(pbmin-(soc(22)+(c(22)*t(22)))-b(22))*u(22);
e(23)=(pbmin-(soc(23)+(c(23)*t(23)))-b(23))*u(23);
e(24)=(pbmin-(soc(24)+(c(24)*t(24)))-b(24))*u(24);
k(1)=-pw(1)-ps(1)-pb(1)+((1-(e(1)/67325.76))*pd(1));
k(2)=-pw(2)-ps(2)-pb(2)+((1-(e(2)/67325.76))*pd(1));
k(3)=-pw(3)-ps(3)-pb(3)+((1-(e(3)/67325.76))*pd(1));
k(4)=-pw(4)-ps(4)-pb(4)+((1-(e(4)/67325.76))*pd(1));
k(5)=-pw(5)-ps(5)-pb(5)+((1-(e(5)/67325.76))*pd(1));
k(6)=-pw(6)-ps(6)-pb(6)+((1-(e(6)/67325.76))*pd(1));
k(7)=-pw(7)-ps(7)-pb(7)+((1-(e(7)/67325.76))*pd(1));
k(8)=-pw(8)-ps(8)-pb(8)+((1-(e(8)/67325.76))*pd(1));
k(9)=-pw(9)-ps(9)-pb(9)+((1-(e(9)/67325.76))*pd(1));
k(10)=-pw(10)-ps(10) -pb(10)+((1-(e(10)/67325.76))*pd());
k(11)=-pw(11)-ps(11) -pb(11)+((1-(e(11)/67325.76))*pd(1));
k(12)=-pw(12)-ps(12) -pb(12)+((1-(e(12)/67325.76))*pd(1))
k(13)=-pw(13)-ps(13) -pb(13)+((1-(e(13)/67325.76))*pd(1));
k(14)=-pw(14)-ps(14) -pb(14)+((1-(e(14)/67325.76))*pd(1));
k(15)=-pw(15)-ps(15)-pb(15)+((1-(e(15)/67325.76))*pd(1));
k(16)=-pw(16)-ps(16) -pb(16)+((1-(e(16)/67325.76))*pd(1));
k(17)=-pw(17)-ps(17) -pb(17)+((1-(e(17)/67325.76))*pd(1));
k(18)=-pw(18)-ps(18) -pb(18)+((1-(e(18)/67325.76))*pd(1));
k(19)=-pw(19)-ps(19) -pb(19)+((1-(e(19)/67325.76))*pd(1));
k(20)=-pw(20)-ps(20) -pb(20)+((1-(e(20)/67325.76))*pd(1));
k(21)=-pw(21)-ps(21) -pb(21)+((1-(e(21)/67325.76))*pd(1));
k(22)=-pw(22)-ps(22) -pb(22)+((1-(e(22)/67325.76))*pd(1));
k(23)=-pw(23)-ps(23) -pb(23)+((1-(e(23)/67325.76))*pd(1));
k(24)=-pw(24)-ps(24) -pb(24)+((1-(e(24)/67325.76))*pd(1));
% constraints2
w(1)=(pw(1)+ps(1)+pb(1)-(pw(1)+ps(1)+soc(1)+c(1)-pd(1))-pd(1));
w(2)=(pw(2)+ps(2)+pb(2)-(pw(2)+ps(2)+soc(2)+c(2)-pd(2))-pd(2));
w(3)=(pw(3)+ps(3)+pb(3)-(pw(3)+ps(3)+soc(3)+c(3)-pd(3))-pd(3));
w(4)=(pw(4)+ps(4)+pb(4)-(pw(4)+ps(4)+soc(4)+c(4)-pd(4))-pd(4));
w(5)=(pw(5)+ps(5)+pb(5)-(pw(5)+ps(5)+soc(5)+c(5)-pd(5))-pd(5));
w(6)=(pw(6)+ps(6)+pb(6)-(pw(6)+ps(6)+soc(6)+c(6)-pd(6))-pd(6));
w(7)=(pw(7)+ps(7)+pb(7)-(pw(7)+ps(7)+soc(7)+c(7)-pd(7))-pd(7));
w(8)=(pw(8)+ps(8)+pb(8)-(pw(8)+ps(8)+soc(8)+c(8)-pd(8))-pd(8));
w(9)=(pw(9)+ps(9)+ pb(9)-(pw(9)+ps(9)+soc(9)+c(9) -pd(9))-pd(9));
w(10)=(pw(10)+ps(10)+pb(10)-(pw(10)+ps(10)+soc(10)+c(10)-pd(10))-pd(10));
w(11)=(pw(11)+ps(11)+pb(11)-(pw(11)+ps(11)+soc(11)+c(11)-pd(11))-pd(11));
w(12)=(pw(12)+ps(12)+pb(12)-(pw(12)+ps(12)+soc(12)+c(12)-pd(12))-pd(12));
w(13)=(pw(13)+ps(13)+pb(13)-(pw(13)+ps(13)+soc(13)+c(13)-pd(13))-pd(13));
w(14)=(pw(14)+ps(14)+pb(14)-(pw(14)+ps(14)+soc(14)+c(14)-pd(14))-pd(14));
w(15)=(pw(15)+ps(15)+pb(15)-(pw(15)+ps(15)+soc(15)+c(15)-pd(15))-pd(15));
w(16)=(pw(16)+ps(16)+pb(16)-(pw(16)+ps(16)+soc(16)+c(16)-pd(16))-pd(16));
w(17)=(pw(17)+ps(17)+pb(17)-(pw(17)+ps(17)+soc(17)+c(17)-pd(17))-pd(17));
w(18)=(pw(18)+ps(18)+pb(18)-(pw(18)+ps(18)+soc(18)+c(18)-pd(18))-pd(18));
w(19)=(pw(19)+ps(19)+pb(19)-(pw(19)+ps(19)+soc(19)+c(19)-pd(19))-pd(19));
w(20)=(pw(20)+ps(20)+pb(20)-(pw(20)+ps(20)+soc(20)+c(20)-pd(20))-pd(20));
w(21)=(pw(21)+ps(21)+pb(21)-(pw(21)+ps(21)+soc(21)+c(21)-pd(21))-pd(21));
w(22)=(pw(22)+ps(22)+pb(22)-(pw(22)+ps(22)+soc(22)+c(22)-pd(22))-pd(22));
w(23)=(pw(23)+ps(23)+pb(23)-(pw(23)+ps(23)+soc(23)+c(23)-pd(23))-pd(23));
w(24)=(pw(24)+ps(24)+pb(24)-(pw(24)+ps(24)+soc(24)+c(24)-pd(24))-pd(24));
c=[ pbmin-soc(i);
soc(i)-x(3);
k(i)
w(i)
];
ceq=[];
end
optimization toolbax continually results "Attempted to access c(2); index out of bounds because numel(c)=1."
thank you
Hi,
I'm afraid this isn't an error from the Optimization Toolbox. Before you call an Optimization Toolbox solver, it can be a good idea to check that your objective and constraint functions evaluate correctly. You can do this by calling them with a test point at the MATLAB command prompt.
In your example, we can check the objective and constraint functions by running them at the MATLAB command prompt (I'm using [0 0 0] as a test point here)
First, your objective function
y= objet_cost30([0 0 0])
y =
0
This evaluates without error. So, we can check the constraint
[c, ceq] = con_objet30([0 0 0])
You should see the following error:
??? Attempted to access c(2); index out of bounds because numel(c)=1.
Error in ==> con_objet30 at 133
batt(2)= soc(2)+(c(2)*t(2))-(d(2)*uu(2));
This error is telling you that there is a bug in your constraint function, con_objet30. You'll need to debug your file to make it run without error.
To get you started on debugging your file, I've noticed that you use the variable c both to store the constraints and to store the quantity charge. To remove this conflict, change "c" to something else (e.g. "cineq") at lines 1 and 234 in con_objet30.
Hope this helps!
Best regards,
Paul
yes
ok,
but
function f= objet_cost30(x1,x2,x3)
f=((100*x1)-(5.81*x1)+(50*x1))/20+ ((450*x2)-(26.14*x2)+(86*x2))/20+((500*x3)+(200*x3))/20 ;
400<=x1<=10000
200<=x2<=8000
0<=x3<=40
only objectfunction's optimization toolbox results Optimization running. Input argument "x2" is undefined.
and
command window is x1=[400:1:10000] ,x2=[200:1:8000], x3=[0:1:40]
results Matrix dimensions must agree.
i'm not understand
i'm need your help
thanks
Hi
In your objective function, you have x1, x2 and x3 as decision variables.
The way you specify x1, x2 and x3 as vector is not correct
i.e. x1=[400:1:10000] ,x2=[200:1:8000], x3=[0:1:40]
These are the constraints on x1,x2 and x3.
Just see the help of your optimization function how to specify these contraints
Saif