Area under a curve

9 vues (au cours des 30 derniers jours)
Q TRAN
Q TRAN le 4 Juil 2016
Commenté : Q TRAN le 5 Juil 2016
Hi,
I want to calculate the area of this curve. y = [0 1 3 -1 -2 -3 -1 0];
I know a portion of the curve has negative value, so my solution is make all the y values absolute. But then the area of absolute y will be higher. Can anyone help me?
Thanks.

Réponses (3)

Walter Roberson
Walter Roberson le 4 Juil 2016

Star Strider
Star Strider le 4 Juil 2016
The problem wasn’t immediately obvious to me. You need to find the zero-crossing, and then add the two separate areas:
y = [0 1 3 -1 -2 -3 -1 0];
x = 1:length(y);
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) < 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
yzxi = zci(y); % Zero-Crossing Index
x0 = interp1(y(yzxi:yzxi+1), x(yzxi:yzxi+1), 0); % Interpolate To Find Zero-Crossing
AUC = polyarea([x(1:yzxi) x0], [y(1:yzxi) 0]) + polyarea([x0 x(yzxi+1:end)], [0 y(yzxi+1:end)]);
INT = trapz(x, abs(y)) % Compare (Optional)
AUC =
10.2500
INT =
11.0000
I used the polyarea function rather than the integration functions. If you have a more complicated function, this will work as well, but you will have to make the appropriate changes to the code. (I included the trapz function integration of the absolute value for comparison.)
  2 commentaires
Q TRAN
Q TRAN le 5 Juil 2016
Thank you.
Do you think if this works for the case we have more than one zero crossing?
Star Strider
Star Strider le 5 Juil 2016
Yes.
You simply have to find each one, calculate the zero-crossing, and do polyarea for each segment.

Connectez-vous pour commenter.


Piyush  Madame
Piyush Madame le 5 Juil 2016
Modifié(e) : Walter Roberson le 5 Juil 2016
just by some codding
  1 commentaire
Q TRAN
Q TRAN le 5 Juil 2016
Would you please share your code?

Connectez-vous pour commenter.

Catégories

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by