How to find area under graph between two points ?

449 次查看(过去 30 天)
I used this coding but i dont know how to set the specific points. I used array data.
M = area(A(:,1),A(:,2));
Int = trapz(A(:,1),A(:,2));
  1 个评论
Scott MacKenzie
Scott MacKenzie 2021-6-12
What are the "specific points" of interest? Are they indices within A, x-values, y-values, or something else? Also, post the data for A if you can.

请先登录,再进行评论。

回答(2 个)

Scott MacKenzie
Scott MacKenzie 2021-6-15
编辑:Scott MacKenzie 2021-7-17
Assuming you want the area under the curve between two values of x (the "specific points" in your questions), here's what I put together using test data. The area under the curve is computed from x = 60 to x = 110. The area is computed two ways, using trapz and using integral. They give the same result. trapz is useful if the data are sample points gathered empirically. If you have a formula to work with, then integral can be used. It's also possible to use polyarea, although that is not demonstrated here.
% test data
x = 0:0.1:150;
y = 0.2 + sind(x) .* cosd(2*x).^2;
fun = @(x) (0.2 + sind(x) .* cosd(2*x).^2);
% organize in matrix A, as in question
A = [x' y'];
% plot total area under curve over x domain
area(A(:,1), A(:,2));
xticks(0:10:150);
hold on;
% example "specific points" to find area between
x1 = 60;
x2 = 110;
% find indices of these points in A
idx1 = find(A(:,1) >= x1, 1);
idx2 = find(A(:,1) >= x2, 1);
% show the area under curve between x1 and x2
area(A(idx1:idx2,1), A(idx1:idx2,2), 'facecolor', [.7 .8 .9]);
% get area under curve from x1 to x2 using trapz
a1 = trapz(A(idx1:idx2,1), A(idx1:idx2,2))
% get area under curve from x1 to x2 using integral
a2 = integral(fun, x1, x2)
% print area in chart
ax = gca;
xt = (x2 + x1) / 2;
yt = 0.8 * mean(ax.YLim);
s = sprintf('Area = %.2f', a1);
text(xt, yt, s);
Output in command window:
a1 =
47.285
a2 =
47.285
  1 个评论
Soham
Soham 2024-4-5
编辑:Soham 2024-4-5
I dont know if anything changed since the time this answer was posted, but this code will give an error with Indexing, since the starting value of A(:, 1) is a zero, while the values of A(:, 2) do not.
The following code shows the figure as shown in the answer.
NB: The error has since disappeared, but I will keep my answer here, incase someone runs into the issue as an alterntive
% test data
x = 0:0.1:150;
y = 0.2 + sind(x) .* cosd(2*x).^2;
fun = @(x) (0.2 + sind(x) .* cosd(2*x).^2);
% organize in matrix A, as in question
A = [x' y'];
% plot total area under curve over x domain
area(A( [1; find(A(:,1))] , 1), A( (find(A(:,2))) , 2));
xticks(0:10:150);
hold on;
% example "specific points" to find area between
x1 = 60;
x2 = 110;
% find indices of these points in A
idx1 = find(A(:,1) >= x1, 1);
idx2 = find(A(:,1) >= x2, 1);
% show the area under curve between x1 and x2
area(A(idx1:idx2,1), A(idx1:idx2,2), 'facecolor', [.7 .8 .9]);
% get area under curve from x1 to x2 using trapz
a1 = trapz(A(idx1:idx2,1), A(idx1:idx2,2));
% get area under curve from x1 to x2 using integral
a2 = integral(fun, x1, x2);
% print area in chart
ax = gca;
xt = (x2 + x1) / 2;
yt = 0.8 * mean(ax.YLim);
s = sprintf('Area = %.2f', a1);
text(xt, yt, s);

请先登录,再进行评论。


Vimal Rathod
Vimal Rathod 2021-6-15
编辑:Vimal Rathod 2021-6-15
Hi,
If you would want to find area by specific points from array, you could use the indices of array to find the area.
M = trapz(A(k:l,1),A(k:l,2)); % for area between k and l index values of array A
If you would want to find area from a custom point on the line to another custom point on the line, make sure to include the array indices along with the custom point in the area.
%finding area between (x1,y1) and (x2,y2) considering these two lies on the
%plot
%k and l are the indices of array values lying in between these points.
M = trapz([x1,A(k:l,1),x2], [y1, A(K:l,2), y2]);
Hope this helps!
  1 个评论
Scott MacKenzie
Scott MacKenzie 2021-6-15
@Vimal Rathod M, in your code, is a handle to the area object, not the area of the object.
@Mohamad Firdaus Bin Adnan's question is limited to a sub-area, as given by "specific points". I asked in my comment for clarification on this, for example whether the specfic points are indices into the A array (k and l in your answer) or values along the x-axis (e.g., 80 and 100).

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Polygons 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by