MATLAB: If logic not working inside for loop

for loopif looplogical statementsmultiple conditions

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.

Best Answer

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.
Related Question