フィルターのクリア

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 Center および File ExchangeGrid 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