How to cumulatively subtract in a specific range

1 次查看(过去 30 天)
Hello everybody, how can I adjust my for loop so that every time when the curve is above the max capacity and negative (see some examples, shown by the arrows), the values will be cumulatively subtracted by the capacity (see the black curve). With my Code I calculate only the difference at one point, with no effect on the actual capacity... You can find my code below.
C=cumsum(diffE,'omitnan'); % needed Capacity
Cbmax= 1000; % in MWH, max. capacity of the battery storage
Cbmin= 100; % in MWh, deep charge border
imax= C>=Cbmax-Cbmin; % find where C is larger than Cbmax
imin= C<=Cbmin-Cbmin; % find where C is smaller than Cbmin
C(imax) = Cbmax-Cbmin; % if C is larger than Cbmax: C=Cbmax
C(imin) = Cbmin-Cbmin; % if C is smaller than Cbmin: C=Cbmin
Cres=cumsum(diffE,'omitnan');
for k=1:length(Cres)-1
if Cres(k)>Cbmax
if Cres(k)>Cres(k+1)
C(k+1)=C(k)-(Cres(k)-Cres(k+1));
end
end
end

回答(1 个)

Aashray
Aashray 2025-4-29
According to my understanding, you are simulating a battery storage system, where “differ” is the energy added/removed per time step, and you also compute a cumulative sum to get the battery’s state of charge over time.
The issue with the current approach is that the following line in the shared code clips values after they exceed the battery limit, but it does not fix the cumulative effect, so overflow/underflow errors continue into future values.
C = cumsum(dif C = cumsum(diffE, 'omitnan');
C(C > Cbmax) = Cbmax;
This leads to physically invalid results, like charging beyond capacity.
Instead of computing everything up front and then clipping, we can accumulate step-by-step, and at each step, we add the energy difference to the current actual amount of energy stored (not the cumulative energy), and cap the result between Cbmin and Cbmax. If energy overflows or underflows, it is discarded.
You may refer to the following code for better understanding:
% Parameters
Cbmax = 1000; % Max capacity
Cbmin = 100; % Min capacity
n = 100; % number of time steps
% Simulated energy flow: charging, discharging, bursts
rng(0); % reproducibility
diffE = [ ...
normrnd(100, 30, [30, 1]); % charging
normrnd(-80, 20, [20, 1]); % discharging
normrnd(150, 40, [20, 1]); % overcharging
normrnd(-100, 30, [30, 1]) % heavy discharging
];
diffE = max(min(diffE, 200), -200); % clip extremes
% Blue: Unbounded cumulative sum
Cres = cumsum(diffE, 'omitnan');
% Red: Corrected battery capacity (realistic)
C = zeros(size(diffE));
C(1) = max(min(diffE(1), Cbmax), Cbmin);
for k = 2:length(diffE)
C(k) = C(k-1) + diffE(k);
if C(k) > Cbmax
C(k) = Cbmax;
elseif C(k) < Cbmin
C(k) = Cbmin;
end
end
% Plotting
figure;
plot(Cres, 'b-', 'DisplayName', 'Unbounded (Blue)');
hold on;
plot(C, 'r-', 'LineWidth', 2, 'DisplayName', 'Corrected Capacity (Red)');
yline(Cbmax, ':', 'Max Capacity', 'Color', [0.5 0.5 0.5], 'DisplayName', 'Max Capacity');
yline(Cbmin, '--', 'Min Capacity', 'Color', [0.5 0.5 0.5], 'DisplayName', 'Min Capacity');
xlabel('Time Step');
ylabel('Cumulative Energy (MWh)');
title('Battery Capacity Behavior: Blue (Raw) vs. Red (Corrected)');
legend('Location', 'best');
grid on;
This also generates a plot in which:
  • Blue shows raw cumulative energy (unbounded, may be incorrect)
  • Red shows realistic battery behaviour (never exceeds min/max battery capacity).
I hope you find it useful!

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by