Area under the curve ignoring axis values (Absolute area)
3 views (last 30 days)
Show older comments
Ganesh Naik
on 24 Jul 2021
Commented: Ganesh Naik
on 25 Jul 2021
Hi all, I am computing area under the curve using "Trapz" function where it takes x and y co-ordinates into consideration. For example, if you consider the attached figure, I am getting the value of shaded area as 296 where it takes into x and y axis values. But I would like to compute only the absolute area ignoring x and y axis values. In this example y axis height is around 7 (-84 to -91) and x axis distance is around 0.1 hence the absolute area would be very small. Any help in this would be highly appreciated.
0 Comments
Accepted Answer
Scott MacKenzie
on 24 Jul 2021
Edited: Scott MacKenzie
on 24 Jul 2021
The trapz function uses integration so the result is the area under the curve. That's not what you want. I suggest you first create a polygon of points encompassing the area of interest. Then use the area function to get the area. Here's and example using some test data similar to your data.
% test data
x = -0.8:0.1:1;
y = [-75 -76 -77 -78 -78 -79 -80 -81 -86 -87 -88.3 -92 -91 ...
-88 -86 -85 -84 -84 -83];
% x range for area of interest
x1 = 0;
x2 = 0.6;
% find x indices for range of interest
idx1 = find(x >= x1, 1);
idx2 = find(x >= x2, 1);
% create polyshape for area of interest
x1 = x(idx1:idx2);
y1 = y(idx1:idx2);
ps = polyshape(x1, y1);
% compute and output area
a3 = polyarea(x1,y1);
fprintf('Area: %f\n', a3);
% plot it
plot(x,y);
set(gca,'ylim', [-95 -70]);
hold on;
plot(ps);
4 Comments
More Answers (1)
Scott MacKenzie
on 24 Jul 2021
Edited: Scott MacKenzie
on 24 Jul 2021
@Ganesh Naik Here's a new answer that uses ginput to "select" data points along the line for definiing the area of interest. You'll need to run this yourself to get a feel for the interaction with a mouse. Select two points along the data line and the area will be computed, sent to the command window, and plotted.
% test data (similar pattern to data in question)
xx = -0.8:0.1:1;
yy = [-75 -76 -77 -78 -78 -79 -80 -81 -86 -87 -88.3 -92 -91 ...
-88 -86 -85 -84 -84 -83];
% interpolate to get more points and better "closest" point to mouse click
x = linspace(min(xx), max(xx), 250);
y = interp1(xx, yy, x);
% plot data and set axes
plot(x,y);
hold on;
axis([-1, 1.2, -95, -70]);
% get two mouse button clicks
[gx, gy] = ginput(2);
% get indices of "closest" data points along x-axis
idx1 = find(x >= gx(1), 1);
idx2 = find(x >= gx(2), 1);
% create polyshape for area of interest
x1 = x(idx1:idx2);
y1 = y(idx1:idx2);
ps = polyshape(x1, y1);
% compute, output, and plot area
a = polyarea(x1,y1);
fprintf('Area: %f\n', a);
plot(ps);
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!