How to calculate the area between two curves

4 ビュー (過去 30 日間)
Paolo Rosettani
Paolo Rosettani 2021 年 1 月 7 日
コメント済み: Paolo Rosettani 2021 年 1 月 8 日
Hi,
How can I calculate the erea between this two curves? (I need the yellow coloured area)
where:
x1 = [0 1 1.5608 2.4538 2.7360];
y1 = [0 0 -0.8278 -0.3777 -1.5954];
and
x2 = {vector 14494x1}
y2 = {vector 14494x1}
All the datas are in the attached Curve.mat file.
Thank you in advance!
  2 件のコメント
Rik
Rik 2021 年 1 月 7 日
What did you try already?
Paolo Rosettani
Paolo Rosettani 2021 年 1 月 7 日
I've tried this:
I used
trapz
for each curve and then subtract them...
but with this way I cannot find the yellow area (in the prevoius figure)

サインインしてコメントする。

採用された回答

David Wilson
David Wilson 2021 年 1 月 8 日
編集済み: David Wilson 2021 年 1 月 8 日
Try the following : I've used one of the many interpolation routines, but I notice that you have not sorted your (x2, y2) data, and that you must extrapolate to get the full range.
load curves
x1 = [0 1 1.5608 2.4538 2.7360];
y1 = [0 0 -0.8278 -0.3777 -1.5954];
plot(x1, y1, x2, y2)
grid on
%% Now interpolate x1 to x2
% sort first (for some strange reason)
[x2s, idx] = sort(x2, 'ascend');
y2s = y2(idx);
[y1i] = interp1(x1, y1, x2s, 'pchip', 'extrap'); % Assume extrapolation
subplot(2,1,1);
plot(x2s, [y1i], '-', x2s, y2s)
ylim([-2, 0.5])
dy = abs([y1i-y2s]);
A = trapz(x2s,dy)
subplot(2,1,2);
plot(x2s, abs([y1i-y2s]))
ylabel('|y_1-y_2|')
title(sprintf('Area = %2.3f', A))
  1 件のコメント
Paolo Rosettani
Paolo Rosettani 2021 年 1 月 8 日
Thank you so much!
This solution is perfect :-)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by