Effacer les filtres
Effacer les filtres

How to calculate individual areas under plot

1 vue (au cours des 30 derniers jours)
Hamzah Mahmood
Hamzah Mahmood le 21 Juil 2020
Commenté : Hamzah Mahmood le 22 Juil 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.

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by