Area between three curves

조회 수: 5 (최근 30일)
Noush
Noush 2021년 10월 16일
답변: Star Strider 2021년 10월 16일
Hi everyone, I have a plot with three curves. I would like to find the area that corresponds to when the blue and green lines are above the red one. I can't do that with substracting the integrals from each other, because the area underneath the red curve is much bigger than the blue and green.
How can I do that?
  댓글 수: 7
Noush
Noush 2021년 10월 16일
I want the sum of all of the red areas!
Scott MacKenzie
Scott MacKenzie 2021년 10월 16일
Hmm, that seems a bit tricky to me -- doable, but tricky. What if the green horizontal line was at, say, y = 6? You'd be counting only some of the green area above the red line. @Matt J has just posted an answer. Perhaps that's your solution.

댓글을 달려면 로그인하십시오.

답변 (2개)

Matt J
Matt J 2021년 10월 16일
편집: Matt J 2021년 10월 16일
A=Einspeiseanteil;
B=GabelstaplerP;
C=max(A,B);
area=trapz(X,(C-Y).*(C>=Y))

Star Strider
Star Strider 2021년 10월 16일
Try this —
I created the comparisons as ‘>=’ so change those to simply ‘>’ ito make the comparisons strictly ‘greater than’.
x = 0:25;
blue = [0 0 0 8 8 8 0 0 0 8*ones(1,9) zeros(1,8)];
green = 1.75 * ones(size(x));
red = [0 1 2 1 2 2 3 3 4 5 6 8 8 8 14 14 17 17 13 13 7 6 4 2 0 0];
N = 1000; % Interpolation Resolution
xi = linspace(min(x), max(x), N); % New 'x'
bi = interp1(x, blue, xi); % Interpolated 'blue'
gi = interp1(x, green, xi); % Interpolated 'green'
ri = interp1(x, red, xi); % Interpolated 'red'
blue_gt_red_idx = bi >= ri; % 'blue' >= 'red'
green_gt_red_idx = gi >= ri; % 'green' >= 'red'
both_gt_red_idx = (gi >= ri) | (bi >= ri); % Both >= 'red'
blue_gt_red_area = cumtrapz(xi, bi .* blue_gt_red_idx);
green_gt_red_area = cumtrapz(xi, gi .* green_gt_red_idx);
both_gt_red_area = blue_gt_red_area + green_gt_red_area;
figure
yyaxis left
plot(x, blue, '.-b', 'DisplayName','$Blue$')
hold on
plot(x, green, '.-g', 'DisplayName','$Green$')
plot(x, red, '.-r', 'DisplayName','$Red$')
hold off
ylabel('Functions (.-)')
yyaxis right
plot(xi, blue_gt_red_area, ':b', 'LineWidth',1.5, 'DisplayName','$Blue \ge Red Area$')
hold on
plot(xi, green_gt_red_area, ':g', 'LineWidth',1.5, 'DisplayName','$Green \ge Red Area$')
plot(xi, both_gt_red_area, ':k', 'LineWidth',2, 'DisplayName','$Both \ge Red Area$')
hold off
grid
ylabel('Integrals (:)')
xlabel('x')
legend('Location','best', 'Interpreter','latex')
The last (end) element of the cumtrapz vectors is the total area.
Experiment to get different results.
.

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by