MATLAB Answers

How to make a array from a loop?

3 views (last 30 days)
Thanathip Boonmee
Thanathip Boonmee on 25 Nov 2019
Commented: M on 25 Nov 2019
j=0;
for i = 1:1440
Z(i) = powerConsumption(i) - solarEnergyGenerated(i);
if (Z(i) < 0) && (j <1200) %Proceed if {Power consumption minus PV is less than 0} and {Battery level is less than 1200kWmin=20kWh}
j = -Z(i) + j; %Charging
elseif (Z(i) > 0) && (j>Z(i)) %Proceed if {Power consumption minus PV is more than 0} and {Battery level is more than Z(i)}
j = j - Z(i); %Discharging
end
end
I am trying to make a battery algorithm on when to charge and discharge. I am not sure how to make a array of 1440x1 from this loop.
I want to make this array to make a plot out of it.

  0 Comments

Sign in to comment.

Answers (1)

M
M on 25 Nov 2019
Edited: M on 25 Nov 2019
You can do something like:
nb = 1440;
j = zeros(1,nb);
Z = zeros(1,nb);
for i = 1 : nb
Z(i) = powerConsumption(i) - solarEnergyGenerated(i);
if i > 2
j(i) = j(i-1) - Z(i);
end
end
You are actally doing the same thing in your 2 conditions "if" and "else".
Then you can plot j and Z :
t = 1 : nb;
plot(t,j);

  3 Comments

Thanathip Boonmee
Thanathip Boonmee on 25 Nov 2019
clc
clear
X = xlsread('meter_01_year_2009.xls');
powerConsumption= X(1:1440); %Extracting 1 day of data, 1day = 1440min
Y = xlsread('PVBowler2013_365.xls');
solarEnergyGenerated = Y(:,1)/1000; %Extracting column 1 and change Wmin to kWmin
j=0;
for i = 1:1440
Z(i) = powerConsumption(i) - solarEnergyGenerated(i);
if (Z(i) < 0) && (j <1200) %Proceed if {Power consumption minus PV is less than 0} and {Battery level is less than 1200kWmin=20kWh}
j = -Z(i) + j; %Charging
elseif (Z(i) > 0) && (j>Z(i)) %Proceed if {Power consumption minus PV is more than 0} and {Battery level is more than Z(i)}
j = j - Z(i); %Discharging
end
end
Actually this is the full code, I am extracting the datas from excel sheet. I want j to be an array of 1440x1
Turlough Hughes
Turlough Hughes on 25 Nov 2019
j = -Z(i) + j;
and
j = j - Z(i); % is also j = -Z(i) + j;
are equivalent calculations. So, as M pointed out, there is no need in that case for an if else to decide which of the above lines to implement.
M
M on 25 Nov 2019
I want j to be an array of 1440x1
j = zeros(1440,1)
defines a vector of size 1440 x 1.
Then access each element with j(k), with k = 1 , ... , 1440

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!