# How to calculate partial area under a graph?

조회 수: 8(최근 30일)
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표시숨기기 이전 댓글 수: 1
friet 2016년 11월 29일
편집: John Kelly 2016년 12월 12일
I ask another simplified version of the previous question.

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

### 채택된 답변

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표시숨기기 이전 댓글 수: 1
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.

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

### 범주

Find more on Numerical Integration and Differentiation in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by