Using value from previous cell in for loop calculation

1 vue (au cours des 30 derniers jours)
Andrew Alkiviades
Andrew Alkiviades le 13 Mai 2012
Hi, I am trying to perform a set of calculations shown in the code below. I am trying to create a 365x24 matrix to find the "battery_capacity". This calculation requires knowledge of the number in the previous cell and thats why I have used (j,i-1) format which doesn't work. Also, I know that the starting value of battery capacitance (at position 1,1 in the matrix is 100. Does anyone know how I can change this code so that I can start from 1,2 to 365,24 using the previous cell value of capacity each time?
Thanks
for j = 1:365
for i = 1:24
if Total_hourly_RES(j,i) > Hourly_power_demand(j,i), % Charge batterys
Battery_capacity(1,1) = 100; %%Find capacity of typcial Pb-acid battery used in this type of system
Battery_capacity(j,i) = Battery_capacity(j,i-1)*(1-battery_self_discharge)+(Total_hourly_RES(j,i)-(Hourly_power_demand(j,i)).*battery_charging_efficiency) ; % Available battery bank capacity
if SOC_min*Battery_capacity(j,i) <= Battery_capacity(j,i), Battery_capacity(j,i) = SOC_min*Battery_capacity(j,i) ;
if SOC_max*Battery_capacity(j,i) >= Battery_capacity(j,i), Battery_capacity(j,i) = SOC_max*Battery_capacity(j,i) ;
end
end
else % Discharge battery
Battery_capacity(1,1) = 100; %%Find capacity of typcial Pb-acid battery used in this type of system
Battery_capacity(j,i) = Battery_capacity(j,i-1).*(1-battery_self_discharge)-(Hourly_power_demand(j,i)-Total_hourly_RES(j,i)).*battery_discharging_efficiency; % Available battery bank capacity
if SOC_min*Battery_capacity <= Battery_capacity(j,i), Battery_capacity(j,i) = SOC_min*Battery_capacity(j,i) ;
if SOC_max*Battery_capacity >= Battery_capacity(j,i); Battery_capacity(j,i) = SOC_min*Battery_capacity(j,i) ;
end
end
end
end
end

Réponses (1)

Sabin
Sabin le 3 Oct 2023
The steps to to this:
  1. Memory allocation for Battery_capacity, e.g., Battery_capacity = zeros(365,24)
  2. Initialization: Battery_capacity(:,1) = 100 (assuming for all i=1 the capacity is 100)
  3. For loops for j=1:365 and i=2:24, then it is possible to use Battery_capacity(j,i-1)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by