If logic not working inside for loop
Mostra commenti meno recenti
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 Commenti
Darshan Ramakant Bhat
il 19 Apr 2017
Can you please attach all the input (mat) files you are using it for this code? I am not able to run your code.
Darshan Ramakant Bhat
il 19 Apr 2017
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
il 19 Apr 2017
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Quantum Mechanics in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!