How can I calculate the shaded area ?

조회 수: 2 (최근 30일)
wd w
wd w 2024년 9월 18일
댓글: wd w 2024년 9월 18일
I have a representative subset of sample points as attached, and intend to calculate the shaded area from x = -40 to x = 30.

채택된 답변

Kanishk
Kanishk 2024년 9월 18일
Dear wd,
I understand you are interested in calculating the area between a plotted line and the x-axis over the interval from (x = -40) to (x = 30).
You can achieve this using MATLAB's “trapz” function, which performs numerical integration. Below is the simple code to plot the data and compute the desired area.
% Load data from Excel
data = readmatrix('example.xlsx');
%filter data
xRange = [-40, 30];
indices = (data(:,1) >= xRange(1)) & (data(:,1) <= xRange(2));
x = data(indices, 1);
y = data(indices, 2);
% Plot the data
figure;
plot(x, y, 'k');
hold on;
fill([x; flipud(x)], [y; zeros(size(y))], [0.5, 0.5, 0.5], 'FaceAlpha', 0.3, 'EdgeColor', 'none');
hold off;
xlabel('X-axis');
ylabel('Y-axis');
title('Plot of X vs Y');
% Calculate the area under the curve
areaUnderCurve = trapz(x, abs(y));
disp(['The area under the curve is: ', num2str(areaUnderCurve)]);
To learn more please go through this official MATLAB documentation of “trapz”.
Hope this helps!
Thanks
  댓글 수: 1
wd w
wd w 2024년 9월 18일
Kanishk, thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

태그

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by