# store values from if functions in a loop to plot as connecting line

1 view (last 30 days)
slowlearner on 18 May 2020
Closed: slowlearner on 22 May 2020
To be fully honest i don't know how to solve this i'm trying to calculate the profitability of Vehicle 2 grid, using old spotprices from 2015, at the moment I'm trying to store true output values in an array but only if certain conditions are met hence the if functions. The loop works fine it displayes what i want and I get the outputs on the workflow when the price falles within C,D,E or F and everything goes upp and down accordingly, however i don't know how i'm going to temporarily store the true values (the ones commented with" %data storage") to be able to use the same ones when the conditions change ex. from D to E and so on. At the moment I'm only counting to 72 itterations before break.
However I need a way to plot the true output results for every itterations becouse i have 8688 itterations to go. Starting value of SOC is zero btw A is a (1:8760, 2)timetable with datetime and spotprices, "s" is a {(1:365),1} cellarray with two 24hour values in each cell ("datetime","spotprice"). I've bin looking around and found "Output Function Syntax" but never used it and don't really comprehend the explanation this might be a too big of a project for a newb but start with whats interesting and learn from fails is a great teach.
TLDR: need to store new calculated values in array for everyhour and continue calculation with the new values and plot the true outputvalues
i = 0;
g = 0;
q = 0;
k = 0;
p = 0;
SOC = []; %data storage of SOC
chargevalue = []; %data storage of spotrice
batterikwh = []; %data storage of power to battery
moneys2pay = []; %data storage of how much moneys involved
for m = 1:1:365
bi = sortrows(s{m,1},'spotpris'); % sorts s{k} daily
C = bi(1:6,:); %defines the 6 cheapest hours of the day on the whole year
D = bi(7:12,:); %defines the 6 2nd cheapest hours of the day on the whole year
E = bi(13:18,:); %defines the 6 2nd most expensive hours of the day on the whole year
F = bi(19:24,:); % defines the 6 most expensive hours of the day on the whole year
g = g+1 %keep track of the days
p = p+1;
k = k+1;
for q = 1:1:24
i = i+1 %keep track of the hours in the day
if ismember(A.tid(i,1),C.tid) && sum(SOC)<0.8 && ~ismember(A.tid(i,1),timetowork(k,1)) && ~ismember(A.tid(i,1),timetohome(p,1))
pause(1) %if time i is in cell C and LESS than .8 SOC and not worktime and not hometime
disp ('1a');
batterikwh = batterycapacity(chargerate, sum(batterikwh));
SOC = Stateofcharge(batterikwh, maxcapacity);
moneys2pay = sum(moneys2pay)+(A.spotpris(i,1)*chargerate);
chargevalue =sum(chargevalue)+(A.spotpris(i,1)*chargerate);
batterispotpris = chargevalue/batterikwh;
elseif ismember(A.tid(i,1),C.tid) && sum(SOC)>0.8 && ~ismember(A.tid(i,1),timetowork(k,1)) && ~ismember(A.tid(i,1),timetohome(p,1))
pause(1) %if time i is in cell C and MORE than 0.8 SOC not work time and not hometime
disp('1b')
elseif ismember(A.tid(i,1),D.tid) && sum(SOC)<0.8 && ~ismember(A.tid(i,1),timetowork(k,1)) && ~ismember(A.tid(i,1),timetohome(p,1))
pause(1) %If time i is in cell D and LESS than 0.8 SOC and not worktime and not hometime
disp('2a');
batterikwh = batterycapacity(chargerate, sum(batterikwh));
SOC = Stateofcharge(batterikwh, maxcapacity);
moneys2pay = sum(moneys2pay)+(A.spotpris(i,1)*chargerate);
chargevalue = sum(chargevalue)+ (A.spotpris(i,1)*chargerate);
batterispotpris = chargevalue/batterikwh;
elseif ismember(A.tid(i,1),D.tid) && sum(SOC)>0.8 && ~ismember(A.tid(i,1),timetowork(k,1)) && ~ismember(A.tid(i,1),timetohome(p,1))
pause(1) %If time i is in cell D and MORE than 0.8 SOC and not worktime and not hometime
disp('2b')
elseif ismember(A.tid(i,1),D.tid) && sum(SOC)>0.5 && batterispotpris<B(i,1) && ~ismember(A.tid(i,1), timetowork(k,1)) && ~ismember(A.tid(i,1),timetohome(p,1))
pause(1) %If time i is in cell D and MORE than 0.5 SOC and batterispotpris is less than spotprice time i and not worktime and not hometime
batterikwh = batterydischarge(chargerate, sum(batterikwh)); %
SOC = Stateofcharge(batterikwh, maxcapacity);
disp('2c')
moneys2pay = sum(moneys2pay)-(A.spotpris(i,1)*chargerate*d);
chargevalue = sum(chargevalue)-(A.spotpris(i,1)*chargerate);
batterispotpris = chargevalue/batterikwh;
elseif ismember(A.tid(i,1),E.tid) && sum(SOC)>0.5 && batterispotpris>B(i,1) && ~ismember(A.tid(i,1), timetowork(k,1)) && ~ismember(A.tid(i,1),timetohome(p,1)) || ismember(A.tid(i,1),E.tid) && sum(SOC)>0.4 && ~ismember(A.tid(i,1), timetowork(k,1)) && ~ismember(A.tid(i,1),timetohome(p,1))
pause(1)%If time i is in cell E and MORE than 0.5 SOC and batterispotpris is MORE than spotprice at time i and not worktime and not hometime
disp('3a')
elseif ismember(A.tid(i,1),E.tid) && sum(SOC)>0.5 && batterispotpris<B(i,1) && ~ismember(A.tid(i,1), timetowork(k,1)) && ~ismember(A.tid(i,1),timetohome(p,1))
pause(1)%If time i is in cell E and MORE than 0.5 SOC and batterispotpris is LESS than spotprice at time i and not worktime and not hometime
batterikwh = batterydischarge(chargerate, sum(batterikwh));
SOC = Stateofcharge(batterikwh, maxcapacity);
disp('3b')
moneys2pay = sum(moneys2pay)-(A.spotpris(i,1)*chargerate*d);
chargevalue = sum(chargevalue)- (A.spotpris(i,1)*chargerate);
batterispotpris = chargevalue/batterikwh;
elseif ismember(A.tid(i,1),E.tid) && sum(SOC)<0.4 && ~ismember(A.tid(i,1), timetowork(k,1)) && ~ismember(A.tid(i,1), timetohome(p,1))
pause(1)%If time i is in cell E and LESS than 0.5 SOC and batterispotpris is LESS than spotprice at time i and not worktime and not hometime
batterikwh = batterycapacity(chargerate, sum(batterikwh));
SOC = Stateofcharge(batterikwh, maxcapacity); %This is a G2V
disp('3c'); %charge function
moneys2pay = sum(moneys2pay)+(A.spotpris(i,1)*chargerate);
chargevalue = sum(chargevalue)+(A.spotpris(i,1)*chargerate);
batterispotpris = chargevalue/batterikwh;
elseif ismember(A.tid(i,1),F.tid) && sum(SOC)>0.5 && batterispotpris<B(i,1) && ~ismember(A.tid(i,1), timetowork(k,1)) && ~ismember(A.tid(i,1),timetohome(p,1))
pause(1) %If time i is in cell F and SOC MORE than 0.5 and batterispotpris LESS than spotprice at time i and not worktime and not hometime
batterikwh = batterydischarge(chargerate, sum(batterikwh));
SOC = Stateofcharge(batterikwh, maxcapacity); %This is a
disp('duckoff'); %V2G charge function
moneys2pay = sum(moneys2pay)-(A.spotpris(i,1)*chargerate*d);
chargevalue = sum(chargevalue)-(A.spotpris(i,1)*chargerate);
batterispotpris = chargevalue/batterikwh;
elseif ismember(A.tid(i,1),F.tid) && sum(SOC)>0.5 && batterispotpris>B(i,1) && ~ismember(A.tid(i,1), timetowork(k,1)) && ~ismember(A.tid(i,1),timetohome(p,1))
pause(1)%If time i is in cell F and SOC MORE than 0.5 and batterispotpris MORE than spotprice at time i and not worktime and not hometime
disp('duckoff2')
elseif ismember(A.tid(i,1),F.tid) && sum(SOC)<0.5 && ~ismember(A.tid(i,1),timetowork(k,1)) && ~ismember(A.tid(i,1),timetohome(p,1))
pause(1)%If time i is in cell F and SOC LESS than 0.5 and not worktime and not hometime
disp('duckoff3')
elseif ismember(A.tid(i,1),timetowork(k,1)) || ismember(A.tid(i,1),timetohome(p,1))
pause(1)%If time i is worktime or hometime
batterikwh = batterydischarge(Drivingloss, sum(batterikwh));
SOC = Stateofcharge(batterikwh, maxcapacity);
disp('driveMF')
moneys2pay = sum(moneys2pay)-(A.spotpris(i,1)*chargerate*d);
chargevalue = sum(chargevalue)-(SEKDrive(30,batterispotpris));
batterispotpris = chargevalue/batterikwh;
else
i>768
break
end
xx = tid(i);
yy = A.spotpris(i);
figure(1); plot(xx, SOC, '.'); title('Battery Capacity'); xlabel('Time'); ylabel('StateOfCharge [10^-2]'); hold on
figure(2); plot(xx, moneys2pay, '.'); title('Cost of charge'); xlabel('Time'); ylabel('Amount payed(t) [SEK]'); hold on
figure(3); plot(xx,batterispotpris,'.'); title('Batteryload Value'); xlabel('Time'); ylabel('Batterispotpris [SEK/kWh]'); hold on
figure(4); plot(xx, chargevalue, '.'); title('Chargevalue'); xlabel('Time'); ylabel('CostOfChargeState [SEK]'); hold on
end
if g>31
break
end
end
clear i m g q k p
this is how the plot looks now:
it's dots for everyhour and don't know how to connect them. (kinda looks like a scatter plott now yes =.=?)

Geoff Hayes on 19 May 2020
nikolas - in your above code, what variables correspond ot the true output values? Which calculated values do you need to save? How are they re-used on subsequent iterations?
slowlearner on 20 May 2020
hey Geoff Hayes i managed to plot the ones i wanted which are the ones denoted as %data storage in the beginning however it's just as dots at specific times and some what incomprehencible beyond 700 itterations need to connect the dots now somehow...