How to calculate individual areas under plot

1 次查看(过去 30 天)
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;

更多回答(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