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
  댓글 수: 1
Austin Thai
Austin Thai 2021년 4월 17일
What does your data look like? Are these curves analytic functions or discrete datapoints? Are they equally spaced in x?

댓글을 달려면 로그인하십시오.

채택된 답변

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