Why does cumtrapz return negative values?

22 views (last 30 days)
Bas Wagemaker
Bas Wagemaker on 30 Dec 2019
Commented: Tom on 18 Feb 2023
Hi!
I am trying to calculate the area underneath a small piece of line using cumtrapz. The line has a positive slope and has positive x and y value. However, cumtrapz returns negative values for the area:
area = 0 -0.0145 -0.0290 -0.0435 -0.0580
I think it has something to do with the stepsize of the X-vector. How can I accurately calculate the area underneath the line?
Thanks!
Bas
% Example of data
X=[0.5056 0.5048 0.5040 0.5032 0.5024];
Y=[18.1277 18.1241 18.1205 18.1170 18.1134];
% Calculating the area under the data
area=cumtrapz(X,Y);
% Plotting data
figure
plot(X,Y)
grid on
  1 Comment
Tom
Tom on 18 Feb 2023
I have a similar issue, does anyone have any ideas? I have 2 csv files with one column increasing and the other decreasing in each file. i have created a code to calculate the midpoint and plot the csv files with the mid point (1 csv file is a minimum and the other is a maximum). it does this fine but when using cumtrapz it outputs a negative integral

Sign in to comment.

Answers (1)

Stijn Haenen
Stijn Haenen on 30 Dec 2019
Your X and Y are from high value to low values, resulting in negative areas.
use fliplr() to mirror arrays X and Y.
X=[0.5056 0.5048 0.5040 0.5032 0.5024];
Y=[18.1277 18.1241 18.1205 18.1170 18.1134];
% Calculating the area under the data
X=fliplr(X);
Y=fliplr(Y);
area=cumtrapz(X,Y);
% Plotting data
figure
plot(X,Y)
grid on

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!