![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/333015/image.jpeg)
How do I calculate the area between a curve and a horizontal line?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I want to calculate the area of a region below a specific y-value for a curve. This is the code I'm working with now, but it is only highlighting the area I want and not returning a specific value.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/332862/image.png)
%% variables
p1d = Profile18(:,1);
p1e = Profile18(:,2);
hwMark = 4181.0164;
%% graph and x-sectional area highlighted ~2.5m above lowest channel elev.
plot(p1d,p1e);
title('Profile #1');
ylabel('Elevation (m)');
xlabel('Distance (m)');
hold on
area(p1d, min(p1e,hwMark), hwMark);
0 Kommentare
Antworten (1)
Roger J
am 17 Jul. 2020
Bearbeitet: Roger J
am 17 Jul. 2020
Capture the return value from "area()", and then find the x values for the two points where p1e crosses hwMark.
Now you can calculate the area of the shaded region, if you realize that it is equal to the area of the rectangle, minus the area under the curve p1e.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/333015/image.jpeg)
Something like the following:
a = area(p1d, min(p1e,hwMark), hwMark);
range=find(a.YData ~= hwMark)
areaUnderP1e = trapz(p1e(range(1):range(end)))
areaOfRectangle = hwMark*(range(end)-range(1))
area = areaOfRectangle-areaUnderP1e
0 Kommentare
Siehe auch
Kategorien
Mehr zu Interpolation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!