Area between two curves

조회 수: 6 (최근 30일)
Christian Reece
Christian Reece 2015년 4월 23일
답변: Star Strider 2015년 4월 23일
I was wondering if there was a simpler/faster way of calculating the area between the two curves at each point (both of which are normailsed so the total area is 1). I am currently using a simple for loop to get the area between each set of points for the two curves, then subtracting it to get the difference i.e.
for i = 1:length(normpeak)-1
resN(i) = trapz(time(i:i+1),peak1(i:i+1));
resF(i) = trapz(time(i:i+1),peak2(i:i+1));
end
res = resN-resF;
This works, but unfortunately it is very slow. I was reading up on the trapz function and from what I understood it calculates the total integral by performing something very similar to what i'm doing now (see image) but sums the data at the end.
Is there a way to stop trapz from performing this summing of the data, so I can speed up my code?

채택된 답변

Star Strider
Star Strider 2015년 4월 23일
Use the cumtrapz function to get the cumulative integral at every point.
I don’t have your data so I can’t test it, but this approach could work:
res = cumtrapz(time, peak1) - cumtrapz(time, peak2);

추가 답변 (1개)

Binbin Qi
Binbin Qi 2015년 4월 23일
if you can get the points of every curve, you can use polyarea function to get the area, and then diff
  댓글 수: 1
Christian Reece
Christian Reece 2015년 4월 23일
Thanks for the answer, but this doesn't seem to work. I think I was too vague before but what I need is the difference in area at each point on the curve.

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

카테고리

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