area in trapz coming out negative
4 views (last 30 days)
Show older comments
Howdy, I am simulating a blackbody distribution and am needing to calculate the area under the curve to get the power. I know that if I can flip it to be in ascending order it will work but becasue of the nature of my y-data I cannot do this since it fluctuates. I am getting negative values for the output at low wavelengths and was wondering if I can just take the abolute value and if that would be bad data?
My code sweeps over a user specified range of wavelength and here is how I have the trapz function:
iradlu_integral_vector(idx) = trapz(lambda_vector, irdlu_vector);
iradatAM_integral_vector(idx) = trapz(lambda_vector, irdatAMu_vector);
and here is my output:
-1.28422266954966e-51
-3.03483007505529e-49
-4.53134955403754e-47
-4.34382130386169e-45
-2.82606215188402e-43
-1.30760887613441e-41
-4.47658757608325e-40
-1.17279656379977e-38
-2.42014441485306e-37
-4.03278333734613e-36
-5.54460320730048e-35
-6.40889845961779e-34
1 Comment
Torsten
on 17 Jan 2024
We don't see what your input to "trapz" is - thus we cannot interprete the output.
Answers (1)
Star Strider
on 17 Jan 2024
Edited: Star Strider
on 17 Jan 2024
‘... was wondering if I can just take the abolute value and if that would be bad data?’
If the descending values of the independent variable are causing the negative values, and if flipping the independent variable would produce positive results, taking the absolute value would probably work. See the documentation section on Trapezoidal Method for details on how it works and the reason that the absolute value would likely be correct.
Testing that —
x = linspace(0, 10, 21);
y = 1 + randn(size(x));
IntFwd = cumtrapz(x, y)
IntRev = cumtrapz(flip(x), y)
AbsIntRev = abs(cumtrapz(flip(x), y))
Check = all(IntFwd == AbsIntRev)
However this only holds true if the independent variable data sampling intervals are constant. If they are not, it fails —
x = sort(rand(1, 21));
y = 1 + randn(size(x));
IntFwd = cumtrapz(x, y)
IntRev = cumtrapz(flip(x), y)
AbsIntRev = abs(cumtrapz(flip(x), y))
Check = all(IntFwd == AbsIntRev)
.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!