Need help in using trapz to integrate a definite function
Mostra commenti meno recenti
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)
Risposte (3)
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)
1 Commento
Emmanuel Sarpong
il 31 Ago 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 Commenti
Emmanuel Sarpong
il 31 Ago 2023
the cyclist
il 31 Ago 2023
Do you mean x = 3, or x = 3e-6?
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
2 Commenti
Emmanuel Sarpong
il 31 Ago 2023
If you define x as
x=(0.0:0.01:50).*1e-6; % in meters
and you evaluate A2 with this vector, you cannot determine the value of the integral between 3 and 5, but only between 3e-6 and 5e-6. And that's what the code above does.
Categorie
Scopri di più su Numerical Integration and Differentiation in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
