If logic not working inside for loop

5 次查看(过去 30 天)
I am running a for loop which contains if/else logical statements. The code runs, but the results make it clear that the logic is not working correctly and the logical conditions are not being recognised. I'm not sure where I'm going wrong. The critical code is here, but the rest is attached.
datetimeAS=[datetime(2012,7,1,0,0,0):minutes(30):datetime(2013,6,30,23,30,0)]';
Demand = zeros(size(datetimeAS,1),1);
Export = zeros(size(datetimeAS,1),1);
Solar = zeros(size(datetimeAS,1),1);
SOC = zeros(size(datetimeAS,1),1);
Battmax=zeros(size(datetimeAS,1),1);
if ismember(i,index_15);
AGSel= randperm(276,1);
[AS1,AS2,AS3]= intersect(datetimeAS,Home.TimeStamp);
Home3=zeros(size(datetimeAS));
%creates a matrix to manage missing data
Home3(AS2,1)=Home.Data(AS3,4);
Solar=2*AusGrid2013(:,AGSel);
LoadminPV = (Home3-2*AusGrid2013(:,AGSel));
for k = 2:size(datetimeAS,1);
% SOC=0 for k=1
SOC1 = SOC(k-1);
NetLoad = LoadminPV(k);
Battdisch = [SOC1 NetLoad maxdis];
Battmax = SOCmax-SOC1;
ExpBatt = -NetLoad;
Battch = [Battmax ExpBatt];
if NetLoad >= 0 ;%if net load is positive
if SOC1 <= 0; %battery empty, draw from grid
Demand = NetLoad;
SOC = 0;
Export = 0;
elseif SOC1 > 0; %battery is discharging
Demand = NetLoad - min(Battdisch);
SOC = SOC1 - min(Battdisch);
Export = 0;
end
elseif NetLoad < 0 ;%if PV is exporting
if SOC1 >= SOCmax; %battery fully charged, export to grid
SOC = SOC1;
Demand = 0;
Export = -NetLoad;
elseif SOC1 < SOCmax; %battery is charging
SOC = SOC1 + eff*min(Battch);
Demand = 0;
Export = (SOCmax-SOC1)-min(Battch);
end
end
Demand(k,1)=Demand;
SOC(k,1)=SOC;
Export(k,1)=Export;
end
I am trying to use Matlab to model the behaviour of households with battery storage and solar. There should be 4 potential states: the net load >0 and the battery is empty; the net load >0 and the battery has charge; the net load <0 and the battery is full; the net load <0 and the battery is charging. For the first few loops, it should be condition 1, net load >0 and empty battery, but it is not recognising this.
  3 个评论
Darshan Ramakant Bhat
Also you can put break points just before the if-else statement and debug your code to see what is going wrong. Following documentation explains on how to use the debugger
Sharon Young
Sharon Young 2017-4-19
Thank you for your suggestion on where to put the break points. I had been trying to put them in the wrong place, and it hadn't been working.
I'm working with about 1GB of data, which exceeds the file size I can upload. I have attached a revised file which works with my data.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2017-4-19
编辑:Jan 2017-4-19
Please use Matlab's tools for an automatic indentation. Then the different levels of the IF conditions get clear immediatly.
You can be sure that the IF conditions are calculated accurately even inside a loop. This means, that the problem must be somewhere else. E.g.:
Demand(k,1)=Demand;
SOC(k,1)=SOC;
Export(k,1)=Export;
This cannot run. As soon as e.g. Demand is a vector, you try to assign the complete vector to one of its elements. This must cause an error. If you do not get an error, you are running a different code.
Please post the code you are actually running. Perhaps you have stored multiple versions of the M-file and Matlab does not run the file you are expecting. This can be found out by the debugger easily: Set a breakpoint in the code and step through it line by line.
Note: If you have checked "if NetLoad >= 0" already, there is no need to check "elseif NetLoad < 0" again. A simple "else" is enough. At least, if the value of NetLoad is not NaN, but if this should be tested, better use isnan directly.
  1 个评论
Sharon Young
Sharon Young 2017-4-19
Thank you for your time. As you say, the problem was not the IF conditions, which were correct. The code you nominated was part of the problem: I was not correctly saving the outputs from my loop. Attached is my updated code file. The corresponding data I am working with is too large to upload.
for k = 2:size(datetimeAS,1);
% SOC=0 for k=1
SOC1 = SOC(k-1);
NetLoad = LoadminPV(k,1);
Battdisch = [SOC1 NetLoad maxdis];
Battmax = SOCmax-SOC1;
ExpBatt = -NetLoad;
Battch = [Battmax ExpBatt];
if NetLoad >= 0 && SOC1 <= 0; %battery empty, draw from grid
Demand = NetLoad;
SOC1 = 0;
Export = 0;
elseif NetLoad >= 0 && SOC1 > 0; %battery is discharging
Demand = NetLoad - min(Battdisch);
SOC1 = SOC1 - min(Battdisch);
Export = 0;
elseif NetLoad < 0 && SOC1 >= SOCmax; %battery fully charged, export to grid
SOC1 = SOC1;
Demand = 0;
Export = -NetLoad;
elseif NetLoad < 0 && SOC1 < SOCmax; %battery is charging
SOC1 = SOC1 + eff*min(Battch);
Demand = 0;
% Export = (SOCmax-SOC1)-min(Battch);
Export = -NetLoad-min(Battch);
end
% %save the results to use them!
Demand2(k,i)=Demand;
SOC(k,i)=SOC1;
Export2(k,i)=Export;
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Quantum Mechanics 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by