Filter löschen
Filter löschen

How to calculate individual areas under plot

1 Ansicht (letzte 30 Tage)
Hamzah Mahmood
Hamzah Mahmood am 21 Jul. 2020
Kommentiert: Hamzah Mahmood am 22 Jul. 2020
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.

Akzeptierte Antwort

Image Analyst
Image Analyst am 22 Jul. 2020
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 Kommentar
Hamzah Mahmood
Hamzah Mahmood am 22 Jul. 2020
Thank you so much Image Analyst, this works great This is a big help! Thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by