# How to calculate partial area-under-the-curve?

57 views (last 30 days)
Jacqueline Rigatto on 15 Mar 2021
Answered: Ngo Nguyen on 19 Apr 2021
Hi, I'm trying to find the area under the curve (ABC) for a part of a graph. I use the "trapz" function, but this function calculates the ABC for an entire area below the selected part of the graph. Any tips on how I can calculate only part of it (not the whole part up to the x axis)? Please see the figure. Interested are to be calculate is in red.
Thanks, Jacqueline Star Strider on 15 Mar 2021
Use trapz twice, once to integrate (A, B, C), and again to integrate (A, C).
Then subtract the (A, C) result from the (A, B, C) result.
It would definitely help to have the data, preferably as a .mat file.
Jacqueline Rigatto on 12 Apr 2021
Thanks for all the help, Star Strider

John D'Errico on 4 Apr 2021
After all of those comments, to be honest, sorry, but you were both working far too hard on a moderately simple problem.
Simplest is to just create a polygon, then use polyarea of that object. Alternatively, create a polyshape object. Again, compute the area of said polygonal region.
help polyshape
help polyshape/area
AREA Find the area of a polyshape A = AREA(pshape) returns the area of a polyshape. The area is the sum of the area of each solid region of pshape. A = AREA(pshape, I) returns the area of the I-th boundary of pshape. This syntax is only supported when pshape is a scalar polyshape object. See also perimeter, centroid, sortboundaries, polyshape Documentation for polyshape/area doc polyshape/area
The result will be a piecewise linear approximation to the area. That is the same result you will get from trapz anyway, since trapz is simply a tool that performs integration using linear segments to connect the points.
Jacqueline Rigatto on 12 Apr 2021
Thank you for the help, John D'Errico

Ngo Nguyen on 19 Apr 2021
You can use determinants to calculate the area of any polygons.
For example:
function S = areaGate(P)
S = 0;
for i = 1:length(P)-1
S = S + det(P(i:i+1,:));
end
S = abs(S)/2;