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

22 vues (au cours des 30 derniers jours)
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!

Réponse acceptée

Star Strider
Star Strider le 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.
  2 commentaires
Fiona
Fiona le 21 Nov 2019
I can't believe I didn't figure that out myself. I was making it way too hard for myself! Thank you so much!
Star Strider
Star Strider le 21 Nov 2019
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by