# Why does cumtrapz return negative values?

조회 수: 18(최근 30일)
Bas Wagemaker 2019년 12월 30일
댓글: Tom 2023년 2월 18일
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표시 없음숨기기 없음
Tom 2023년 2월 18일
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

댓글을 달려면 로그인하십시오.

### 답변(1개)

Stijn Haenen 2019년 12월 30일
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

댓글을 달려면 로그인하십시오.

### 범주

Find more on 2-D and 3-D Plots in Help Center and File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!