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

1 vue (au cours des 30 derniers jours)
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 commentaire
Austin Thai
Austin Thai le 17 Avr 2021
What does your data look like? Are these curves analytic functions or discrete datapoints? Are they equally spaced in x?

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 17 Avr 2021
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])
.

Plus de réponses (0)

Catégories

En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by