フィルターのクリア

How can I make a integration with trapz?

5 ビュー (過去 30 日間)
shahin sharafi
shahin sharafi 2022 年 4 月 21 日
コメント済み: shahin sharafi 2022 年 4 月 27 日
Hi everyone,
I have x and y vectors, and ploted y verses x as below:
v=[2.2696;2.2696; 2.4646;2.4646;2.6775;2.6775; 2.9101;2.9101;3.1644];
x=[0 , -0.0476 , -0.0476 , -0.0952 , -0.0952 , -0.1429 , -0.1429 , -0.1905 ,-0.1905];
plot (x,v)
I want to find the inegral (The area under the curve) of curve in range of x=[0 and -0.08]. How can I do that?
Thank you in advance,

採用された回答

Bruno Luong
Bruno Luong 2022 年 4 月 21 日
Note: The value is negative since x goes backward.
v=[2.2696;2.2696; 2.4646;2.4646;2.6775;2.6775; 2.9101;2.9101;3.1644];
x=[0 , -0.0476 , -0.0476 , -0.0952 , -0.0952 , -0.1429 , -0.1429 , -0.1905 ,-0.1905];
b=x<=0 & x>=-0.08;
xx=[x(b),-0.08];
vv=[v(b);NaN];
trapz(xx,fillmissing(vv,'previous'))
ans = -0.1879
  2 件のコメント
Davide Masiello
Davide Masiello 2022 年 4 月 21 日
I think that since the function lies all above the x axis, the area should be positive.
Bruno Luong
Bruno Luong 2022 年 4 月 21 日
I don't compute the area but the integral with trapz with x going backward, so it return negative of the area, as noted in my answer.

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

その他の回答 (2 件)

Davide Masiello
Davide Masiello 2022 年 4 月 21 日
編集済み: Davide Masiello 2022 年 4 月 21 日
I don't think you really need trapz for this function
clear,clc
x=[0,-0.0476,-0.0476,-0.0952,-0.0952,-0.1429,-0.1429,-0.1905,-0.1905];
y=[2.2696;2.2696; 2.4646;2.4646;2.6775;2.6775; 2.9101;2.9101;3.1644];
plot (x,y)
x1 = [0,-0.0476,-0.0476,-0.08];
y1 = [2.2696;2.2696; 2.4646;2.4646];
dx = diff(unique(x1,'stable'))
dx = 1×2
-0.0476 -0.0324
dy = unique(y1,'stable')'
dy = 1×2
2.2696 2.4646
area = abs(sum(dx.*dy))
area = 0.1879
  4 件のコメント
Bruno Luong
Bruno Luong 2022 年 4 月 21 日
編集済み: Bruno Luong 2022 年 4 月 21 日
Check again. There are 2 steps I give the manual formula and value above: 0.1879
Davide Masiello
Davide Masiello 2022 年 4 月 21 日
The answer was indeed wrong because unique sorts the output unless specified otherwise. I fixed it and now it works. I also implemented to calculate the absolute value so the area comes out positive.
Thanks for pointing it out @Bruno Luong!

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


shahin sharafi
shahin sharafi 2022 年 4 月 25 日
What should I do guys? Please inform me about the correct code
  2 件のコメント
Bruno Luong
Bruno Luong 2022 年 4 月 25 日
@shahin sharafi Only you can decide
"I want to find the inegral (The area under the curve)"
integral has sign changing with x direction, area has not.
You also stated you want using trapz.
shahin sharafi
shahin sharafi 2022 年 4 月 27 日
Thank you for your response.

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

カテゴリ

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

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by