Area under the curve ignoring axis values (Absolute area)

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.

 Risposta accettata

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);
Area: 1.630000
% plot it
plot(x,y);
set(gca,'ylim', [-95 -70]);
hold on;
plot(ps);

4 Commenti

Hi Scott, thanks for your answer. Currently for my work I am using "ginput" to select two points and computing the area. Would this work for ginput? Please let me know your opinion. I will try it my end and let you know.
Hmmm, in that case, I guess you'd need to first decide on what area you are interested in. It's unlikely a mouse click will be exactly on a data point; so, what do you want to happen in the various possibilities there?
Ganesh Naik
Ganesh Naik il 24 Lug 2021
Modificato: Ganesh Naik il 24 Lug 2021
Hi Scott I have managed to get the precise points using modified "ginput" function. Everytime, I select (click) two points and generate (x1,y1) and (x2,y2) points. The figure above is the result of using modified ginput. After that I compute the area and shade them. I think I can modifiy your solution to achieve the result. I will try it and let you know. Any opinion or suggestion from your end is highly appreciated.
I just posted an new answer that uses ginput.

Accedi per commentare.

Più risposte (1)

@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 Commento

Hi Scott, thanks for your help with the ginput part. Although I am using modifed ginput function but interpolating part that you mentioned would help me to select the precise points. Thanks again for your help, much appreciated. I am going to accept this answer.

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by