フィルターのクリア

How to calculate partial area under a graph?

1 回表示 (過去 30 日間)
friet
friet 2016 年 11 月 29 日
コメント済み: Star Strider 2017 年 5 月 10 日
Hello,
I am trying to find the area as a part of a graph( Red area in the picture below). I am asking here a simplified question of my complext problem. The area is definitely 0.5. However, when I am using "trapz" function, It is giving me 4.5.
x=[0 1 2 3 4 5 10 15 20];
y=[0 -1 -2 -3 -4 -5 0 5 0];
plot(x,y)
area=trapz(4:5)
Thanks, Frite
  2 件のコメント
KSSV
KSSV 2016 年 11 月 29 日
編集済み: Walter Roberson 2016 年 11 月 30 日
friet
friet 2016 年 11 月 29 日
編集済み: John Kelly 2016 年 12 月 12 日
I ask another simplified version of the previous question.

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

採用された回答

Star Strider
Star Strider 2016 年 11 月 29 日
You are not coding the integration correctly to integrate the area you want.
Explore this and experiment with it:
x=[0 1 2 3 4 5 10 15 20];
y=[0 -1 -2 -3 -4 -5 0 5 0];
yi = interp1(x, y, [4:5]);
shaded_area = trapz([4:5],yi+5)
plot(x,y)
hold on
patch([4 5 5 4], [yi -5 -5], 'r')
hold off
shaded_area =
500.0000e-003
  2 件のコメント
Namira
Namira 2017 年 5 月 10 日
I was looking your code. Why did you add 5 with yi in line 4? I can't understand it. Sorry I am not good at Matlab coding.
Star Strider
Star Strider 2017 年 5 月 10 日
I added 5 because the area to be integrated goes from -5 to the line. The trapz function would otherwise integrate from 0 to the line. The offset ‘resets’ the 0 reference to -5.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by