How to calculate cumulative sum based on pattern in Matlab?

1 vue (au cours des 30 derniers jours)
Asrorkhuja Ortikov
Asrorkhuja Ortikov le 17 Juin 2020
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.

Réponse acceptée

Pranjal Kaura
Pranjal Kaura le 18 Juin 2020
Modifié(e) : Pranjal Kaura le 18 Juin 2020
Hey,
It’s my understanding that you want to use parameters like energy_con, charging_rate, duration, pattern etc to simulate the battery level of a car.
I have assumed that the charging and discharge rates are per hour and the units of charging_rate and SoCin/SoCfull are identical.
Problems in your code:
  1. Error in while loop, it will never exit as SoCin and Socfull are not getting updated inside the loop.
  2. Instant decrease or increase in the final graph is because you are assigning all the values in the duration [TrSt:TrEn] with the value of the battery at the TrEn.
  3. Incorrect usage of hold on commands
  4. There is no usage of traffic_DCH in your code and thus I have commented it out
Here's the updated code for simulating and anaylsing the battery levels of a car.
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];
timediff = duration*60; % time of end and start time difference in minutes
singularvalue = distance./timediff; % value per hour
MinScale = zeros(24*60,1);
% traffic_DCH = zeros(24*60,1); %commented it out, because hasnt been used
% in code
e_con = zeros(24*60,1);
e_con(1) = SoCin; %starting charge/level of the battery
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
if pattern(i) == 'p'
for j = TrSt:TrEn %for loop to iterate over the timings when the car is parked and update the battery level based off
%the previous battery level.
e_con(j, 1) = min(e_con(max(j - 1, 1),1) + charging_rate/60, SoCfull);
end
else
for j = TrSt:TrEn
e_con(j,1) = max(e_con(max(j - 1, 1),1) - energy_con/60, 0);%energy_con/60, because rates are hourly
end
end
end
end
close all
TimeH = (1:length(MinScale))/60;
% figure
% hold on
% plot(TimeH,traffic_DCH)
% xlim([1 24])
% xlabel("Time (h)")
% ylabel("Distance travelled (km)")
% grid on
% hold on
figure
plot(TimeH,e_con)
xlim([1 24])
xlabel("Time (h)")
ylabel("SoC (kW)")
grid on
  1 commentaire
Asrorkhuja Ortikov
Asrorkhuja Ortikov le 19 Juin 2020
Modifié(e) : Asrorkhuja Ortikov le 19 Juin 2020
Yes, @Pranjal Kaura your statements are right. This code works like a charm now. I was confused by iteration loops between loops interval. Now I have got it. Thanks a loooooot !!!!!!!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Energy Storage dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by