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

조회 수: 13 (최근 30일)
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!

채택된 답변

Star Strider
Star Strider 2019년 11월 21일
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
Fiona
Fiona 2019년 11월 21일
I can't believe I didn't figure that out myself. I was making it way too hard for myself! Thank you so much!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by