Need help in using trapz to integrate a definite function

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)
Error using trapz
Too many input arguments.

Risposte (3)

trapz is probably not the best way to do this. You could use integral instead
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)
q1 = 1.2934e+03
q2 = integral(A2,3,5)
q2 = 5.6099e-14
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))
q = 0
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

Hi cyclist, you're right! that's not the answer I am expecting. Thank you anyway.
The function decribes a curve and I want to find the area under a part of the curve from x =3 to 5 using the trapezoidal method of integration. Thanks
Do you mean x = 3, or x = 3e-6?

Accedi per commentare.

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)
ans = 1.2934e+03
6e8*2e-6 % Approximate value of the integral
ans = 1200

2 Commenti

Hi Torsten, the function, A2 plots a curve and I want to find the area under a part of the curve from x = 3 to 5 using the trapezoidal method of integration. I hope this helps. Thanks for your help anyway
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.

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by