Using value from previous cell in for loop calculation

조회 수: 1 (최근 30일)
Andrew Alkiviades
Andrew Alkiviades 2012년 5월 13일
답변: Sabin 2023년 10월 3일
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

답변 (1개)

Sabin
Sabin 2023년 10월 3일
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)

카테고리

Help CenterFile Exchange에서 Propulsion and Power Systems에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by