Trapz not referred to x-axis

1 回表示 (過去 30 日間)
Simone Speltoni
Simone Speltoni 2021 年 6 月 14 日
コメント済み: Simone Speltoni 2021 年 6 月 17 日
Hi, I'm having some issues regarding the chance to calculate the area between two curves or lines, as the ones following:
clear all
close all
xA=0:4;
A=[-2 -1 2 3 4];
figure(1)
plot(xA,A,'o');
grid on
hold on
%polyfit to build a curve with "A" data
[pA,~,muA] = polyfit(xA, A, 4);
fA = polyval(pA,xA,[],muA);
plot(xA,fA);
xi = linspace(min(xA), max(xA)); % Interpolation Vector
Ai = interp1(xA,A,xi,'linear'); % Interpolated Data
y=xi*(-1)-2;
plot(xi,y);
Areabetweencurve=trapz(abs(y),abs(Ai)); % area between the curves should be around 21
I thought that trapz could have been the right tool, but instead of the x-line as a reference, trapezoids should be considered between the two lines. The point is, I could calculate the area between the two curves with a loop where I consider (b+B)*h/2, but maybe (and probably) there is a smarter way. Any suggestion? thanks
  1 件のコメント
Scott MacKenzie
Scott MacKenzie 2021 年 6 月 14 日
Check your trapz arguments. For the 2-arg version, the first argument is for the x spacing.

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

採用された回答

Cris LaPierre
Cris LaPierre 2021 年 6 月 14 日
編集済み: Cris LaPierre 2021 年 6 月 14 日
If you want to use trapz, use it to calculate the area below both lines, and then subtract them to find the area between the lines.
  1 件のコメント
Simone Speltoni
Simone Speltoni 2021 年 6 月 17 日
Thank you Cris

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by