How can i find the areas under single parts of a plot?

1 view (last 30 days)
I have this vector P wich represent the istant power used by a certain load in 24 h.
I would like to obtain a vector a vector E [1x24] whit the area under the segment given by 2 consecutive element of P.
I tryed this
%Power
P=[ 0.6495 0.7393 0.7551 0.7947 0.7974 0.8713 1.0482 1.2621 1.6027 1.7189 1.5710 1.3070 1.4416 1.5684 1.608 1.6053 1.6581 2.2865 2.4925 2.6087 2.3288 2.1941 1.9671 1.6265];
%Hours
H=[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23];
%I can plot these 2 ad obtain
plot(H,P);
Now i can find the whole area of the plot whit "trapz" or the cumulative area whit "cumtrapz" like this
trapz(H,P)
ans = 35.3648
cumtrapz(H,P)
ans = 1×24
0 0.6944 1.4416 2.2165 3.0126 3.8469 4.8066 5.9618 7.3942 9.0550 10.6999 12.1389 13.5132 15.0182 16.6064 18.2131 19.8448 21.8171 24.2066 26.7572 29.2259 31.4874 33.5680 35.3648
As i said I would like to obtain instead a vector E [1x23] whit the area under the segment given by 2 consecutive element of P.
For example for the first 2 element
E1=trapz(P(1,1:2))
E1 = 0.6944
E2=trapz(P(1,2:3))
E2 = 0.7472
I would like to avoid to write manually each element of E, is it possible?

Accepted Answer

Voss
Voss on 2 Jul 2022
%Power
P=[ 0.6495 0.7393 0.7551 0.7947 0.7974 0.8713 1.0482 1.2621 1.6027 1.7189 1.5710 1.3070 1.4416 1.5684 1.608 1.6053 1.6581 2.2865 2.4925 2.6087 2.3288 2.1941 1.9671 1.6265];
%Hours
H=[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23];
diff(cumtrapz(H,P))
ans = 1×23
0.6944 0.7472 0.7749 0.7961 0.8343 0.9597 1.1551 1.4324 1.6608 1.6449 1.4390 1.3743 1.5050 1.5882 1.6066 1.6317 1.9723 2.3895 2.5506 2.4688 2.2614 2.0806 1.7968

More Answers (0)

Categories

Find more on Frequently-used Algorithms in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!