How to calculate partial area under a graph?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
friet
am 29 Nov. 2016
Kommentiert: Star Strider
am 10 Mai 2017
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/158483/image.jpeg)
Thanks, Frite
2 Kommentare
KSSV
am 29 Nov. 2016
Bearbeitet: Walter Roberson
am 30 Nov. 2016
You have asked the same question....and it got answered too.... http://www.mathworks.com/matlabcentral/answers/314470-how-can-i-calculate-the-area-under-a-graph-shaded-area
Akzeptierte Antwort
Star Strider
am 29 Nov. 2016
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 Kommentare
Namira
am 10 Mai 2017
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
am 10 Mai 2017
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Numerical Integration and Differentiation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!