How to calculate the area between two curves separately based on being below or above.
1 view (last 30 days)
Show older comments
Wolfgang McCormack
on 17 Apr 2021
Answered: Star Strider
on 17 Apr 2021
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 Comment
Austin Thai
on 17 Apr 2021
What does your data look like? Are these curves analytic functions or discrete datapoints? Are they equally spaced in x?
Accepted Answer
Star Strider
on 17 Apr 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])
.
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!