![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/769111/image.jpeg)
Area between three curves
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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? ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/769056/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/769056/image.png)
7 commentaires
Scott MacKenzie
le 16 Oct 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.
Réponses (2)
Star Strider
le 16 Oct 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.
.
0 commentaires
Voir également
Catégories
En savoir plus sur Interpolation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!