Why does cumtrapz return negative values?

16 ビュー (過去 30 日間)
Bas Wagemaker
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
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
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

カテゴリ

Help Center および File ExchangeDirected Graphs についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by