Negative Value when using Trapz
古いコメントを表示
Hi guys,
I try to calculate the area under this curve using trapz. However, it returns a negative value. Can someone tell me as to why this is the case when my x and y-values are positive?
x=[1 0.938524445788592 0.928012855054005 0.869986463167799 0.866618294101049 0.851905469533143 0.816509718296436 0.804756601303802 0.773481908667312 0.743487036373908 0.721555011244502 0.692238382577883 0.660395319804889 0.622278234454403 0.600185408288678 0.582390124224061 0.534500435615996 0.496551977223480 0.460628844607043 0.403312845618717 0.396635208896749 0.369880255480953 0.330164761722580 0.320181673106196 0.266016313621435 0.232051898082808 0.207117082563950 0.160899350279211 0.149854984446954 0.0908664503933046 0.0762867242364327 0.00582165604699889 0];
y=[0.4503 0.9715 1.0442 1.1506 1.1598 1.2079 1.3224 1.3278 1.3576 1.2198 1.0836 0.8967 0.6814 0.5081 0.4139 0.3949 0.3297 0.3276 0.3335 0.3500 0.3516 0.3560 0.3627 0.3634 0.3651 0.3640 0.3629 0.3594 0.3587 0.3522 0.3511 0.3488 0.3486]
a= trapz(x,y)

2 件のコメント
Star Strider
2019 年 10 月 3 日
‘Can someone tell me as to why this is the case when my x and y-values are positive?’
Not without your code and data.
Jana Stucke
2019 年 10 月 3 日
採用された回答
その他の回答 (2 件)
Guillaume
2019 年 10 月 3 日
Can someone tell me as to why this is the case
Because your x vector is decreasing, so
is negative for each trapeze
a = trapz(fliplr(x), fliplr(y))
to use increasing x and matching y.
Steven Lord
2019 年 10 月 3 日
編集済み: Steven Lord
2019 年 10 月 3 日
Your x vector is sorted descending.
>> issorted(x, 'ascend')
ans =
logical
0
>> issorted(x, 'descend')
ans =
logical
1
In essence, you're integrating the function represented by the y data from x = 1 to x = 0, not from x = 0 to x = 1. If you flip your x vector so you're integrating from x = 0 to x = 1 (essentially swapping the limits of integration) the area will be positive.
>> trapz(flip(x), flip(y))
[edited: I had forgotten to flip y until I saw Guillaume's answer.]
カテゴリ
ヘルプ センター および File Exchange で Numerical Integration and Differentiation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
