Need help in using trapz to integrate a definite function
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a simple function, A2 to integrate (from x = 3 um to 5 um) using the trapezoidal method so I used the "trapz" but it didn't work. I would be grateful if someone could assist. My code is below. Thank you in advance.
clear all
c=3*10^8; % speed of light in vaccum
h=6.626*10.^-34; % Planck constant
k=1.38*10.^-23; % Boltzmann constant
T=700; % Temperatures in Kelvin
x=(0.0:0.01:50).*1e-6; % in meters
A2=(2*h*c*c)./((x.^5).*(exp((h.*c)./(k.*T.*x))-1));
q=trapz(A2,x,3,5)
0 Kommentare
Antworten (3)
the cyclist
am 31 Aug. 2023
c=3*10^8; % speed of light in vaccum
h=6.626*10.^-34; % Planck constant
k=1.38*10.^-23; % Boltzmann constant
T=700; % Temperatures in Kelvin
A2 = @(x) (2*h*c*c)./((x.^5).*(exp((h.*c)./(k.*T.*x))-1));
% Unclear what you really want for x values. Here are two versions.
q1 = integral(A2,3.e-6,5.e-6)
q2 = integral(A2,3,5)
the cyclist
am 31 Aug. 2023
Did you read the documentation for the trapz function? It expects either two or three inputs, not the four you provided.
I am going to guess that your intention is to only integration your A2 function in the range x in (3,5). In that case, you could do
c=3*10^8; % speed of light in vaccum
h=6.626*10.^-34; % Planck constant
k=1.38*10.^-23; % Boltzmann constant
T=700; % Temperatures in Kelvin
x=(0.0:0.01:50).*1e-6; % in meters
A2=(2*h*c*c)./((x.^5).*(exp((h.*c)./(k.*T.*x))-1));
inRange = (x >= 3) & (x<=5);
q=trapz(x(inRange),A2(inRange))
That's probably not the result you are expecting, but I am not going to debug this too much, since it is not actually clear what you are trying to achieve. Please explain more.
2 Kommentare
Torsten
am 31 Aug. 2023
Bearbeitet: Torsten
am 31 Aug. 2023
Maybe you mean
clear all
c=3*10^8; % speed of light in vaccum
h=6.626*10.^-34; % Planck constant
k=1.38*10.^-23; % Boltzmann constant
T=700; % Temperatures in Kelvin
x=(0.0:0.01:50).*1e-6; % in meters
A2=(2*h*c*c)./((x.^5).*(exp((h.*c)./(k.*T.*x))-1));
%q=trapz(A2,x,3,5)
idx = x>=3e-6 & x<=5e-6;
x = x(idx);
A2 = A2(idx);
plot(x,A2)
trapz(x,A2)
6e8*2e-6 % Approximate value of the integral
Siehe auch
Kategorien
Mehr zu Numerical Integration and Differentiation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!