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

14 次查看(过去 30 天)
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
Star Strider 2021-3-15
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.
  27 个评论
Star Strider
Star Strider 2021-4-4
I have forgotten where we were on this.
The easiest way would be:
Area_2 = (15.16 - 10.15)*(2.7946-0.68)
producing:
Area_2 =
10.5941
If you prefer a different approach, I will have to remember what we were doing with this, and go back and reconstruct the trapz calls.

请先登录,再进行评论。

更多回答(2 个)

John D'Errico
John D'Errico 2021-4-4
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
POLYSHAPE Create polyshape object A polyshape is a 2-D polygon that can consist of one or more solid regions, and can have holes. polyshape objects are created from the x- and y-coordinates that describe the polygon's boundaries. Boundaries that make up a polyshape should have no self-intersections or intersections with other boundaries, and all boundaries should be properly nested. Otherwise, the polyshape is ill-defined, and can lead to inaccurate or unexpected results. PG = POLYSHAPE(X, Y) creates a polyshape object from 2-D vertices given by two vectors X and Y. X and Y must have the same size. PG = POLYSHAPE(P) creates a polyshape from 2-D points contained in the 2-column array P. PG = POLYSHAPE({X1, X2, ..., Xn}, {Y1, Y2, ..., Yn}) creates a polyshape with n boundaries from two cell arrays. Each cell array contains n vectors, where each Xi contains the x-coordinates and Yi contains the y-coordinates of the i-th boundary. PG = POLYSHAPE(..., 'SolidBoundaryOrientation', DIR) specifies the direction convention for determining solid versus hole boundaries. DIR can be one of the following: 'auto' (default) - Automatically determine direction convention 'cw' - Clockwise vertex direction defines solid boundaries 'ccw' - Counterclockwise vertex direction defines solid boundaries This name-value pair is typically only specified when creating a polyshape from data that was produced by other software that uses a particular convention. PG = POLYSHAPE(..., 'Simplify', tf) specifies how ill-defined polyshape boundaries are handled. tf can be one of the following: true (default) - Automatically alter boundary vertices to create a well-defined polygon false - Do not alter boundary vertices even though the polyshape is ill-defined. This may lead to inaccurate or unexpected results. PG = POLYSHAPE(..., 'KeepCollinearPoints', tf) specifies how to treat consecutive vertices lying along a straight line. tf can be one of the following: true - Keep all collinear points as vertices of PG. false - Remove collinear points so that PG contains the fewest number of necessary vertices. The value of the 'KeepCollinearPoints' parameter is carried over when the ADDBOUNDARY, INTERSECT, SIMPLIFY, SUBTRACT, UNION, and XOR functions are used with input PG. PG = POLYSHAPE() creates a polyshape object with no vertices. Polyshape properties: Vertices - 2-D polyshape vertices NumRegions - Number of regions in the polyshape NumHoles - Number of holes in the polyshape Methods for modifying a polyshape: addboundary - Add boundaries to a polyshape rmboundary - Remove boundaries in a polyshape rmholes - Remove all the holes in a polyshape rmslivers - Clean up degeneracies in a polyshape simplify - Fix degeneracies and intersections in a polyshape Methods for querying a polyshape: ishole - Determine if a boundary is a hole issimplified - Determine if a polyshape is simplified numsides - Find the total number of sides in a polyshape numboundaries - Find the total number of boundaries in a polyshape Methods for geometric information: area - Find the area of a polyshape boundary - Get the x- and y-coordinates of a boundary boundingbox - Find the bounding box of a polyshape centroid - Find the centroid of a polyshape convhull - Find the convex hull of a polyshape holes - Convert all holes in a polyshape into an array of polyshapes isinterior - Determine if a point is inside a polyshape nearestvertex - Find the vertex of a polyshape nearest to a point overlaps - Determine if two polyshapes overlap perimeter - Get the perimeter of a polyshape regions - Put all regions in a polyshape into an array of polyshapes triangulation - Construct a 2-D triangulation from polyshape Methods for transformation: rotate - Rotate a polyshape by angle with respect to a center scale - Scale a polyshape by a factor translate - Translate a polyshape by a vector Methods for Boolean operations: intersect - Find the intersection of two polyshapes or between polyshape and line subtract - Find the difference of two polyshapes union - Find the union of two polyshapes xor - Find the exclusive or of two polyshapes Other methods: polybuffer - Create buffer zone around polyshape isequal - Determine if two polyshapes are identical plot - Plot polyshape and return a Polygon object handle sortboundaries - Sort boundaries in polyshape sortregions - Sort regions in polyshape turningdist - Find the turning distance of two polyshapes Example: Find the area and centroid of a polyshape and plot it %create a polyshape with 4 vertices quadShp = polyshape([0 0 1 3], [0 3 3 0]); %compute area and centroid a = area(quadShp); [cx, cy] = centroid(quadShp); figure; plot(quadShp); hold on %plot centroid point as '*' plot(cx, cy, '*'); See also area, centroid, nsidedpoly Documentation for polyshape doc 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.

Ngo Nguyen
Ngo Nguyen 2021-4-19
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;

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by