Why trapz is giving me negative area?

12 ビュー (過去 30 日間)
Azarang
Azarang 2025 年 1 月 31 日
コメント済み: Azarang 2025 年 1 月 31 日
Hey all,
I have two vectors, frequency and power. I'm trying to get the area under the curve of the power plot but it ends up giving me a negative value. When I do trapz(power) alone, it's a positive value. diff(freq) is also positive. I'm attaching the data for your reference. Thanks!
  1 件のコメント
Matt J
Matt J 2025 年 1 月 31 日
編集済み: Matt J 2025 年 1 月 31 日
You should get into the habit of demonstrating the issue you are posting about with code, as I have done below. As you can see, if I just guess the code you used, I don't reproduce what you say you are seeing.
P=load('power').power;
F=load('frequency').freq;
plot(F,P)
trapz(F,P)
ans = 3.3292e-10

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2025 年 1 月 31 日
The syntax of trapz() is trapz(X, Y) -- so independent variable followed by dependent variable.
If you trapz(freq, power) then you get a positive value.
  1 件のコメント
Azarang
Azarang 2025 年 1 月 31 日
Oh god, my bad! Thanks!

サインインしてコメントする。

その他の回答 (1 件)

Les Beckham
Les Beckham 2025 年 1 月 31 日
編集済み: Les Beckham 2025 年 1 月 31 日
I get a positive value for the area.
load power.mat
load frequency.mat
plot(freq, power)
grid on
area = trapz(freq, power)
area = 3.3292e-10
Maybe you switched the X and Y arguments to trapz. See the doc for trapz. When there are two arguments X comes first.
trapz(power, freq)
ans = -3.3291e-10

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

タグ

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by