필터 지우기
필터 지우기

Need help in using trapz to integrate a definite function

조회 수: 6 (최근 30일)
Emmanuel Sarpong
Emmanuel Sarpong 2023년 8월 31일
댓글: Emmanuel Sarpong 2023년 8월 31일
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.

답변 (3개)

the cyclist
the cyclist 2023년 8월 31일
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

the cyclist
the cyclist 2023년 8월 31일
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
Emmanuel Sarpong
Emmanuel Sarpong 2023년 8월 31일
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
the cyclist
the cyclist 2023년 8월 31일
Do you mean x = 3, or x = 3e-6?

댓글을 달려면 로그인하십시오.


Torsten
Torsten 2023년 8월 31일
편집: Torsten 2023년 8월 31일
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
Emmanuel Sarpong
Emmanuel Sarpong 2023년 8월 31일
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
Torsten
Torsten 2023년 8월 31일
편집: Torsten 2023년 8월 31일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by