cumtrapz is giving different values for similar data.
21 views (last 30 days)
Show older comments
Hello,
I am not sure how exactly cumtrapz work. I saw some videos and read some stuff and got an idea about but still dont have a clue why is this error happening. So i have a code where I read some data from a text file which is current and voltage and use that to calculate power. Then multiply cumtrapz ´(power) by dt/1000 to get energy where dt is 1(3600). So I do this with multiple txt files, and all of them give correct soultion except one. I dont know why because the one which is wrong has similar voltage and current like the other text files. So they should produce similar values. For eg the current value at the end of the array would be 0.020 and voltage value would be 3.34 or something close like 3.31 or 3.36 etc. So energy would be 0.1705 or something close. As said all of them have close values except that one wrong one which has 0.2205 or something. I dont know why is this error happening. I can upload the wrong text file which is called Unit_029 and one correct text file which is Unit_043. I can also paste the code here. Please run the code using both txt files and see and could you help me understand why is this cumtrapz producing error or different energy from similar curent and voltage. Also a point I want to mention is Unit 29 has more values than Unit 43, so I thought maybe that is the reason why there is this error. But then there is a third .txt file Unit_05 which has much more values than Unit_029, yet it gives correct energy values just like Unit_43. So the number of values is also not the reason why cumtrapz is giving wrong error. I have also uploaded the Unit_05.txt as well. Thank you.
clear;
clc;
[file,location] = uigetfile('*.txt', 'Select *.txt Batteryfile to compare' );
%Ensure user selected a file
if file == 0
error('No file selected! Please try again.')
end
filePath = fullfile(location, file);
fileData = readtable(filePath, 'Delimiter', ';', 'ReadVariableNames', true, 'VariableNamingRule', 'preserve');
Current = fileData.Current / 1000;
Voltage = fileData.Volt / 1000;
power = Voltage .* Current;
dt = 1/3600;
energy = cumtrapz(power)*dt/1000;
energy = energy(end:-1:1);
1 Comment
Stephen23
on 16 Oct 2024
Edited: Stephen23
on 16 Oct 2024
"I dont know why is this error happening"
No error is thrown.
It looks like UNIT_29 has a few jumps/discontinuities in the Volt data, which might indicate that the Volt data has not been recorded correctly:
T1 = readtable('Unit_29.txt', 'Delimiter',';') % nok
T2 = readtable('Unit_43.txt', 'Delimiter',';') % ok
dt = 1/3600;
P1 = T1.Volt./1000 .* T1.Current./1000;
P2 = T2.Volt./1000 .* T2.Current./1000;
E1 = cumtrapz(P1)*dt/1000;
E2 = cumtrapz(P2)*dt/1000;
figure()
yyaxis left
plot(T1{:,:})
yyaxis right
plot(E1)
figure()
yyaxis left
plot(T2{:,:})
yyaxis right
plot(E2)
Answers (0)
See Also
Categories
Find more on Environment and Settings 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!