How to calculate the area between two curves separately based on being below or above.

1 次查看(过去 30 天)
Hi Alll
how can I seperate calculate all the area difference when the orange line is below the black line and vice versa. In other words, calculating A1 and A3 seprately from A2.
Thanks

采纳的回答

Star Strider
Star Strider 2021-4-17
Try something like this:
t = linspace(0, 10*pi, 500); % Create Data
y1 = exp(-0.1*t) .* sin(t); % Create Data
y2 = exp(-0.2*t) .* cos(t); % Create Data
icptidx = find(diff(sign(y1 - y2))); % Approximate Indices Of Intersections
icptidx = [1, icptidx, numel(t)];
areavct = cumtrapz(t, y1 - y2); % Cumulative Areas
for k = 1:numel(icptidx)-1
areas(k) = areavct(icptidx(k+1)) - areavct(icptidx(k));
xtxt(k) = (t(icptidx(k+1))+t(icptidx(k)))/2; % Used in ‘text’ To Label Areas
ytxt(k) = (y1(icptidx(k+1))+y2(icptidx(k)))/2; % Used in ‘text’ To Label Areas
end
figure
plot(t, y1)
hold on
plot(t,y2)
hold off
grid
text(xtxt, ytxt, compose('$\\leftarrow Area = %.3f$', areas), 'Interpreter','latex', 'Rotation',60)
ylim([min(ylim) max(ylim)+0.2])
.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by