Definite integral over discrete data

조회 수: 6 (최근 30일)
Zoro
Zoro 2015년 11월 25일
댓글: Star Strider 2015년 11월 26일
Hi, I have a set of discrete data "A" over frequency "f". I want to integrate "A" over "f" from "f1" to "f2". I don't want to integrate over all frequency range. Using trapz(f,A) it gives me the integral over all frequencies... How can I do that? -Thank you

채택된 답변

Star Strider
Star Strider 2015년 11월 25일
편집: Star Strider 2015년 11월 25일
Without your data I can only outline the approach:
ix1 = find(f <= f1, 1, 'last');
ix2 = find(f >= f2, 1, 'first');
Alim = A(ix1:ix2);
flim = f(ix1:ix2);
int_f1_f1 = trapz(flim, Alim)
This finds the last index of ‘f’ less than or equal to ‘f1’ and the first index greater than or equal to ‘f2’. It then uses those indices to create ‘flim’ and ‘Alim’.
This is UNTESTED CODE but should work.
  댓글 수: 2
Zoro
Zoro 2015년 11월 25일
It worked... Thanks!
Star Strider
Star Strider 2015년 11월 25일
My pleasure!

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

추가 답변 (1개)

Adam
Adam 2015년 11월 25일
편집: Adam 2015년 11월 25일
trapz( f( startIdx:endIdx ), A( startIdx:endIdx ) )

where startIdx and endIdx are the indices of the frequencies f1 and f2 which you can find by e.g.

startIdx = find( f >= f1, 1 );
endIdx = find( f >= f2, 1 );

if you don't already have them. I use >= there because exact equality tests are not stable with doubles, but you can change the test there to whatever you prefer.

  댓글 수: 4
Adam
Adam 2015년 11월 26일
Thanks! I've never come across it before.
Star Strider
Star Strider 2015년 11월 26일
My pleasure! You will if you post often enough, although the spam trap has gotten progressively more sophisticated since the spam inundations in the Spring of 2014.
There should be a link on the spam notice you see that tells you to contact MathWorks if you believe the spam trap flagged it erroneously. I always click on it and include the URL of the post in my message, although I suspect several people with privileges to do so see the false positive and remove it, as I did with yours.

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

카테고리

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