Negative Value when using Trapz
92 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Jana Stucke
il 3 Ott 2019
Commentato: Star Strider
il 24 Mar 2021
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 Commenti
Star Strider
il 3 Ott 2019
‘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.
Risposta accettata
Star Strider
il 3 Ott 2019
The problem is easiest to see with:
dx = diff(x)
All the ‘dx’ results are negative because ‘x’ is goinmg from highest-to-lowest.
To get a positive result:
a = trapz(fliplr(x),fliplr(y))
produces:
a =
0.60535
4 Commenti
Andrea Mira
il 24 Mar 2021
In case the "function" has a part where dx decreases and another where dx increases. Could fliplr be used?
Or would it be necessary to calculate the area in two parts? The part where dx decreases applying fliplr and the part where dx increases without fliplr?
(I'm interested in calculating the red area between the "scatter function" and the blue horizontal line)
Thanks in advance
Star Strider
il 24 Mar 2021
Andrea Mira — If I understand correctly what you want to do, I would simply calculate (integrate) those two red areas separately and then add their absolute values if you want to get the total area. Otherwise, they would subtract from each other, producing an area that would be essentially (within calculation error) 0.
Più risposte (2)
Guillaume
il 3 Ott 2019
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.
0 Commenti
Steven Lord
il 3 Ott 2019
Modificato: Steven Lord
il 3 Ott 2019
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.]
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!