Area under the curve ignoring axis values (Absolute area)
Show older comments
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.

Accepted Answer
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);

1 Comment
Ganesh Naik
on 25 Jul 2021
Categories
Find more on Numerical Integration and Differentiation 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!