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
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
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!