Area between three curves

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 Comments

Scott MacKenzie
Scott MacKenzie on 16 Oct 2021
Edited: Scott MacKenzie on 16 Oct 2021
Just to clarify, you want the area where the "blue and green lines are above the red one":
That's the yellow area above, correct?
I might help if you post the script that created the plot above. Gives us something to work with.
John D'Errico
John D'Errico on 16 Oct 2021
Edited: John D'Errico on 16 Oct 2021
That the area under the red curve is much bigger than the others is utterly, completely irrelevant.
You want the area where BOTH the blue and green curves lie above the red curve?
If we assume you have the functions red(t), green(t), and blue(t), then you want to compute the area of the function:
max(min(green(t),blue(t)) - red(t),0)
where max is the function max in MATLAB.
The problem is, these curves likely cross BETWEEN the data points. And that means trapz will fail to produce a good estimate. But you can still make it work.
No I'm sorry, I want the sum of all areas where either the blue or the green curve are above the red one.
This is the skript I used:
plot (X,Y, "r")
title("PV Anlagen Leistung ueber einen Tag im Juli")
ylabel("Leistung [kW]")
xlabel("Stunde [AM/PM]")
grid on
hold on
yline(Einspeiseanteil,"g")
plot(X,GabelstaplerP)
Scott MacKenzie
Scott MacKenzie on 16 Oct 2021
Edited: Scott MacKenzie on 16 Oct 2021
OK, so that's a completely different question.
To clarify again, the "sum of" in your revised question implies you want the area between the lines where the green line is above the red line plus the area where the blue line is above the red line. In this case, the yellow area from my first comment will be double-counted. Is that what you want?
I want the sum of all of the red areas!
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.

Sign in to comment.

Answers (2)

Matt J
Matt J on 16 Oct 2021
Edited: Matt J on 16 Oct 2021
A=Einspeiseanteil;
B=GabelstaplerP;
C=max(A,B);
area=trapz(X,(C-Y).*(C>=Y))
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.
.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Asked:

on 16 Oct 2021

Answered:

on 16 Oct 2021

Community Treasure Hunt

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

Start Hunting!