Area between three curves

7 次查看(过去 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 中查找有关 Interpolation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by