area in trapz coming out negative

4 views (last 30 days)
Julien
Julien on 17 Jan 2024
Edited: Star Strider on 17 Jan 2024
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
Torsten on 17 Jan 2024
We don't see what your input to "trapz" is - thus we cannot interprete the output.

Sign in to comment.

Answers (1)

Star Strider
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)
IntFwd = 1×21
0 0.3825 0.5717 0.9250 1.6490 2.0027 2.2594 2.7532 2.9140 2.9920 3.2265 3.4922 3.5675 3.8307 4.4531 5.7495 7.3478 8.0987 8.3065 8.2436 8.1699
IntRev = cumtrapz(flip(x), y)
IntRev = 1×21
0 -0.3825 -0.5717 -0.9250 -1.6490 -2.0027 -2.2594 -2.7532 -2.9140 -2.9920 -3.2265 -3.4922 -3.5675 -3.8307 -4.4531 -5.7495 -7.3478 -8.0987 -8.3065 -8.2436 -8.1699
AbsIntRev = abs(cumtrapz(flip(x), y))
AbsIntRev = 1×21
0 0.3825 0.5717 0.9250 1.6490 2.0027 2.2594 2.7532 2.9140 2.9920 3.2265 3.4922 3.5675 3.8307 4.4531 5.7495 7.3478 8.0987 8.3065 8.2436 8.1699
Check = all(IntFwd == AbsIntRev)
Check = logical
1
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)
IntFwd = 1×21
0 0.0003 0.2544 0.5619 0.6067 0.6457 0.6526 0.6943 0.7057 0.7580 0.8185 0.9166 0.9347 0.9378 0.9385 0.9486 0.9814 1.0200 1.0755 1.0783 1.2942
IntRev = cumtrapz(flip(x), y)
IntRev = 1×21
0 -0.1086 -0.1117 -0.1886 -0.2116 -0.2203 -0.2361 -0.2374 -0.2402 -0.2667 -0.4585 -0.4894 -0.5252 -0.5381 -0.5582 -0.5626 -0.7104 -0.7854 -1.0073 -1.2379 -1.2385
AbsIntRev = abs(cumtrapz(flip(x), y))
AbsIntRev = 1×21
0 0.1086 0.1117 0.1886 0.2116 0.2203 0.2361 0.2374 0.2402 0.2667 0.4585 0.4894 0.5252 0.5381 0.5582 0.5626 0.7104 0.7854 1.0073 1.2379 1.2385
Check = all(IntFwd == AbsIntRev)
Check = logical
0
.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!