MATLAB Answers

Area under the curve ignoring axis values (Absolute area)

1 ビュー (過去 30 日間)
Ganesh Naik
Ganesh Naik 2021 年 7 月 24 日
コメント済み: Ganesh Naik 2021 年 7 月 25 日
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.

採用された回答

Scott MacKenzie
Scott MacKenzie 2021 年 7 月 24 日
編集済み: Scott MacKenzie 2021 年 7 月 24 日
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 件のコメント
Scott MacKenzie
Scott MacKenzie 2021 年 7 月 24 日
I just posted an new answer that uses ginput.

サインインしてコメントする。

その他の回答 (1 件)

Scott MacKenzie
Scott MacKenzie 2021 年 7 月 24 日
編集済み: Scott MacKenzie 2021 年 7 月 24 日
@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 件のコメント
Ganesh Naik
Ganesh Naik 2021 年 7 月 25 日
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.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by