Soc battery using coulomb counting code

12 views (last 30 days)
Anis Aoudia
Anis Aoudia on 1 Sep 2024
Answered: Samar on 19 Feb 2025
Can someone help me ro modify this code " Calculate batery Soc using coulomb counting method "
% Assuming synthData is already defined with fields `t` and `I`
time = synthData.t; % Time in seconds
Unable to resolve the name 'synthData.t'.
current = synthData.I; % Current in Amperes
% Constants
C_battery = 2000; % Battery capacity in milliampere-hours (mAh)
initial_SOC = 100; % Initial State of Charge in percentage
C_rate = C_battery / 1000; % Convert capacity from mAh to Ah
% Initialize State of Charge (SoC)
SOC = zeros(1, length(current)); % Preallocate SoC array
SOC(1) = initial_SOC; % Set initial SoC
% Calculate the change in state of charge using Coulomb counting
for i = 2:length(current)
% Time step
dt = time(i) - time(i-1); % Time interval in seconds
% Calculate the change in charge (in Ah)
delta_Q = current(i) * dt / 3600; % Convert current to Ah
% Update the State of Charge
SOC_estim = ((1-(1/(x*3600))*cumtrapz(Time, -Current))*100); % Convert Ah change to percentage
SOC(i) = max(0, min(SOC(i), 100)); % Ensure SoC is between 0 and 100
end
% Plot the results
figure;
plot(time, SOC, '-o', 'LineWidth', 1.5, 'MarkerSize', 4);
xlabel('Time (seconds)');
ylabel('State of Charge (SoC) [%]');
title('Battery State of Charge over Time');
grid on;
xlim([min(time) max(time)]); % Set x-axis limits
ylim([0 100]); % Set y-axis limits
  1 Comment
Umar
Umar on 1 Sep 2024
Hi @Anis Aoudia,
What is your source of information, could you provide the link to “ Calculate batery Soc using coulomb counting method” which will help resolve this issue, “ time = synthData.t;”

Sign in to comment.

Answers (1)

Samar
Samar on 19 Feb 2025
Hello @Anis Aoudia,
By modifying I believe you are trying to optimize the code such that the execution time is reduced. So, I tried to find which part of the code is taking the most amount of time using MATLAB’s Profile function. Here is the MathWorks documentation for more information:
Since the time and current values were not provided, I assumed random vectors of different sizes (10,1000 and 1e5). From this I inferred that the for loop takes the most amount of time to execute. So, I tried vectorization of the code snippet present in the for loop.
(In the code provided an undefined variable “x” was used to calculate value of SOC_estim. I assume it’s a typo and I used C_rate for calculation).
Vectorization seems to improve the performance of code if size of time and current vectors cross 1000. For vectorization I used the element wise operators. Their usage is explained in the link below:
To calculate the difference between adjacent elements of a vector, the function diff can be used. This function can be used to find “dt. Refer to the link given below for more information.
Refer the link given below to determine the State of Charge by Coulomb Charging.
Let me know in case of doubts if any.

Categories

Find more on Spline Postprocessing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!