Area between curve (numerical data with positive and negative values) and y = 0

22 views (last 30 days)
Hi all,
I am trying to calculate the area between a curve (defined by numerical data rather than a function) and the x-axis. I don't think it is a straightforward trapz application as the curve has noise that at times oscillates above and below y = 0 (i.e. the numerical data is positive and negative) and I don't want the 'negative' areas to be subtracted from the positive ones. Hopefully that makes sense...
An example of the data is attached.
Thank you so much in advance!
Edit: To clarify, I don't want to areas under y = 0 to contribute to the result at all so I can't abs(y). Thanks again!

Accepted Answer

Star Strider
Star Strider on 21 Nov 2019
I do not have a clear idea of what you want as the result.
It is relatively straightforward to integrate the areas greater than 0 and the areas less than 0 separately:
x = 0:100;
y = randn(1, 101);
gt0 = y>0;
posArea = trapz(x(gt0), y(gt0)); % Area > 0
negArea = trapz(x(~gt0), y(~gt0)); % Area < 0
If you want greater resolution in the areas, use interp1 first (before doing the logical comparisons and trapz calls) to interpolate to more detailed independent and dependent variable values.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!