Area between three curves

6 Ansichten (letzte 30 Tage)
Noush
Noush am 16 Okt. 2021
Beantwortet: Star Strider am 16 Okt. 2021
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 Kommentare
Noush
Noush am 16 Okt. 2021
I want the sum of all of the red areas!
Scott MacKenzie
Scott MacKenzie am 16 Okt. 2021
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.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Matt J
Matt J am 16 Okt. 2021
Bearbeitet: Matt J am 16 Okt. 2021
A=Einspeiseanteil;
B=GabelstaplerP;
C=max(A,B);
area=trapz(X,(C-Y).*(C>=Y))

Star Strider
Star Strider am 16 Okt. 2021
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.
.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by