필터 지우기
필터 지우기

How to calculate individual areas under plot

조회 수: 1 (최근 30일)
Hamzah Mahmood
Hamzah Mahmood 2020년 7월 21일
댓글: Hamzah Mahmood 2020년 7월 22일
I'm trying to calculate areas between individual x values underneath my current plot, from the code below;
clear
clc
x = [0 1 2 3 4 5 6 7 8 9 10 11 12];
y = -[0 -0.5 -0.8 -0.8 -1 -1.1 -1.2 -1.2 -1.4 -1.2 -1.1 -1 0];
plot (x,y,'r')
area (x,y)
An = (trapz(x,y));
How would I calculate and store values of the area between the ranges of 0 to 1, 1 to 2 etc. onwards, whilst relating it still to the original matrix its attached to? Would it be also possible to return this value for the areas as a matrix the same size as x and y?
Any help would be appreciated,
Thanks.

채택된 답변

Image Analyst
Image Analyst 2020년 7월 22일
Try this:
% Create sample data.
x = [0 1 2 3 4 5 6 7 8 9 10 11 12]
y = -[0 -0.5 -0.8 -0.8 -1 -1.1 -1.2 -1.2 -1.4 -1.2 -1.1 -1 0];
subplot(2, 1, 1);
area(x,y)
grid on;
hold on;
% Draw grid lines on top of area.
for k = 1 : length(x)
xline(x(k), 'Color', 'k');
end
yt = yticks;
for k = 1 : length(yt)
yline(yt(k), 'Color', 'k');
end
plot (x, y, 'r.-', 'LineWidth', 3, 'MarkerSize', 30)
xlabel('x', 'FontSize', 20);
ylabel('y', 'FontSize', 20);
% Compute areas.
An = (trapz(x,y)) % Overall area.
% Compute areas within each pair of x locations.
midpoints = (y(1:end-1) + y(2:end))/2
deltax = diff(x)
areas = deltax .* midpoints
subplot(2, 1, 2);
bar(x(1:end-1)-x(1)+ 0.5, areas);
title('Areas of Each Zone', 'FontSize', 20);
xlabel('x', 'FontSize', 20);
ylabel('Area in zone', 'FontSize', 20);
grid on;
  댓글 수: 1
Hamzah Mahmood
Hamzah Mahmood 2020년 7월 22일
Thank you so much Image Analyst, this works great This is a big help! Thanks.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by