How to calculate cumulative sum based on pattern in Matlab?
Show older comments
I am trying to simulate battery level of electric car (EV) based on driving and parking patterns from dataset table. Basically, I have driving/parking duration(time intervals), starting battery capacity of EV(SoCin), distance travelled with car energy consumption rate while driving, and charging speed. Here how data looks like:
%%CONSTANTS
energy_con = 6; charging_rate = 4; SoCin = 35; SoCfull=41;
%% TIME INTERVALS
start_time ['00:00' '06:30' '07:00' '16:15' '17:45']
end_time ['06:30' '07:00' '16:15' '17:45' '23:59']
%% VARIABLES
pattern = [p d p d p] % parking/driving pattern
duration = [6.5 0.5 9.25 1.5 6.25]
distance = [0 15 0 25 0]
By idea, it should charge while parking if SoC level lower than full capacity(charging_rate multiplied to parking time), and discharge while driving with the rate distance*energy_con.
I am a newbie to Matlab, but I have tried following:
timediff = duration*60; % time of end and start time difference in minutes
singularvalue = d./timediff; % value per hour
MinScale = zeros(24*60+1,1); % from 1 to 1440 scale is created to simulate day
% in minutes (+1 is for to avoid 0 values in
% time)
traffic_DCH = zeros(24*60+1,1);
e_con = zeros(24*60+1,1);
%% INTERVAL SPREAD
for i = 1:length(end_time)
[~, ~, ~, H, MN, ~] = datevec(start_time(i));
TrSt = H*60+MN+1;
[~, ~, ~, H, MN, ~] = datevec(end_time(i));
TrEn = H*60+MN+1;
if isnan(TrEn) || isnan(TrSt)
continue
else
while SoCin < SoCfull
if pattern == 'p'
e_con(TrSt:TrEn,1) = e_con(TrSt:TrEn,1) + cumsum(ch_rate./timediff(i));
else
e_con(TrSt:TrEn,1) = e_con(TrSt:TrEn,1) - cumsum(econ_rate*singularvalue(i));%
end
end
end
end
%TIME AXIS
close all
TimeM = 1:length(MinScale);
TimeH = TimeM/60;
figure
hold on
plot(TimeH,traffic_DCH)
xlim([1 24])
xlabel("Time (h)")
ylabel("Distance travelled (km)")
grid on
figure
% hold on
plot(TimeH,e_con)
xlim([1 24])
xlabel("Time (h)")
ylabel("SoC (kW)")
grid on
Code might be very bad I admit, it's only showing instant decrease or increase instead of contionous minute by minute change for example when driving or charging. Maybe there is an another approach to it, any help would be much appreciated.
Accepted Answer
More Answers (0)
Categories
Find more on Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!