Code to calculate Bi-Hourly energy tariff

Hi!
I'm working on a project on energy production and consumption. I have a table (called "d") that includes a row with the datetime information of a year in 15min intervals (called "date", 35040 lines), and other with the electric consumption of that year. And I need to calculate the cost of the electricity according to the bi-hourly energy tariff.
In this code I'm just trying to create a vector (pe) that has the correct cost of electricity (€/kWh) in that specific moment, throughout the year, to later multiply by the consumption.
It doesn't work and takes over 10min to run. I'm out of ideas, I would appreciate if someone could help me :)
Bi-hourly tariff:
night time: 0.0969 €/kWh (00:00 - 08:00 / 22:00 - 24:00)
day time: 0.2028 €/kWh (08:00 - 22:00)
peFV = 0.2028;
peV = 0.0969;
pe = zeros(35040,1);
for i=1:length(pe)
%particular case for the days in which the hour changes
if day(d.date(i)) == 25 && month(d.date(i)) == 3 %25 march
for k=1:92
if k>=29 && k<=84
pe(i)=0.2028;
else
pe(i)=0.0969;
end
i=i+1;
end
i=i-1;
elseif day(d.date(i)) == 28 && month(d.date(i)) == 10 %28 October
for k=1:100
if k>=37 && k<=92
pe(i)=0.2028;
else
pe(i)=0.0969;
end
i=i+1;
end
i=i-1;
else %all other days
for k=1:96
if k>=33 && k<=88
pe(i)=peFV;
else
pe(i)=peV;
end
i=i+1;
end
i=1-1;
end
end
1 day has 96 lines, so i'm using the line number to distinguish between hours.
Sorry for the rudimentary code, I'm a beginner.

Answers (1)

I think you can do this type of task much easier by using timetable and retime function. The following is an example.
% Sample data
rng('default'); % for reproducibility
date = (datetime(2017,1,1,0,0,0):minutes(15):datetime(2017,12,31,23,45,0))';
consumption = 10*rand(size(date));
d = timetable(date,consumption);
% Bi-hour tarrif ( pe(1): day time / pe(2): night time)
pe = [0.2028; 0.0969];
% Aggregate electric consumption [kWh] hourly
d2 = retime(d,'hourly',@(x) sum(x)*0.25);
% Assign tarrif type (1: day time / 2: night time)
d2.group = zeros(height(d2),1);
idx = d2.date.Hour >= 8 & d2.date.Hour < 22;
d2.group(idx) = 1;
d2.group(~idx) = 2;
% Calculate hourly cost [euro]
d2.hourlyCost = d2.consumption .* pe(d2.group);
Then, the total cost in euro for this year can be obtained by:
>> sum(d2.hourlyCost)
ans =
6.9600e+03

2 Comments

pe = [0.2028; 0.0969; 0.888; 0.6545; 0.877; 0.7646];
how can we expand the pe to 6 elements?
because " index exceeds matrix dimensions" appears as the code runs
OK. Then, the program needs to be slightly modified, like:
% Sample data
rng('default'); % for reproducibility
date = (datetime(2017,1,1,0,0,0):minutes(15):datetime(2017,12,31,23,45,0))';
consumption = 10*rand(size(date));
d = timetable(date,consumption);
% Hour tarrif (pe(k) : 4*(k-1) o'clock to 4*k o'clock)
pe = [0.2028; 0.0969; 0.888; 0.6545; 0.877; 0.7646]; % <- modified
% Aggregate electric consumption [kWh] hourly
d2 = retime(d,'hourly',@(x) sum(x)*0.25);
% Assign tarrif type (group = k for 4*(k-1) o'clock to 4*k o'clock)
d2.group = floor(d2.date.Hour/4)+1; % <- modified
% Calculate hourly cost [euro]
d2.hourlyCost = d2.consumption .* pe(d2.group);

Sign in to comment.

Categories

Products

Release

R2017a

Asked:

on 2 Aug 2018

Commented:

on 17 Oct 2020

Community Treasure Hunt

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

Start Hunting!