How to find area under graph between two points ?

조회 수: 580 (최근 30일)
Mohamad Firdaus Bin Adnan
Mohamad Firdaus Bin Adnan 2021년 6월 11일
편집: Soham 2024년 4월 5일 5:49
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일 5:47
편집: Soham 2024년 4월 5일 5:49
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에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by