필터 지우기
필터 지우기

I am having problem with integration in matlab. But unable to find the error.

조회 수: 2 (최근 30일)
ATHIRA
ATHIRA 2023년 4월 26일
댓글: ATHIRA 2023년 4월 27일
k=1.38e-23;
N=6e23;
Td=323
T=0:2*Td;
for i=1:length(T)
%s=@(x) (x.^4.*exp(x))./((exp(x)-1)^2);
%xmin=0;
%xmax=T./Td;
I=integral(((x.^4).*exp(x))./((exp(x)-1)^2),0,T./Td);
Cvd(i)=(9.*N.*k.*I.*(T(i)./Td)^3);
end
plot(T,Cvd)
Error in untitled10 (line 10)
I=integral(s,xmin,xmax);

답변 (1개)

albara
albara 2023년 4월 26일
편집: Rik 2023년 4월 26일
The error in this MATLAB code is due to the incorrect use of the variable x in the integral calculation.
In the line that calculates the integral, the expression ((x.^4).*exp(x))./((exp(x)-1)^2) is used as the integrand. However, x has not been defined before, so MATLAB does not know what value to use for it. This causes an error, because the integral function needs a valid function handle to compute the integral.
To fix the error, you can modify the code to define x as the integration variable and use it in the integrand expression. Here's the corrected code:
k = 1.38e-23;
N = 6e23;
Td = 323;
T = 0:2*Td;
for i = 1:length(T)
I = integral(@(x) ((x.^4).*exp(x))./((exp(x)-1).^2), 0, T(i)/Td);
Cvd(i) = 9*N*k*I*(T(i)/Td)^3;
end
Warning: Inf or NaN value encountered.
plot(T, Cvd)
In this corrected code, x is defined as the integration variable in the anonymous function @(x), and is used in the expression for the integrand. Also, note that T(i)/Td is used to define the upper limit of the integral, because the upper limit depends on the current value of T(i) being iterated over.
  댓글 수: 4
albara
albara 2023년 4월 26일
The issue with the updated code is in the plot function. The plot function is outside of the loop and is only plotting the last calculated value of Cvd(i) instead of the entire vector Cvd.
To fix this issue, move the plot function outside of the loop, after Cvd has been fully calculated, like this:
k = 1.38e-23;
N = 6e23;
Td = 150;
T = 0:2*Td;
for i = 1:length(T)
I = integral(@(x) ((x.^4).*exp(x))./((exp(x)-1).^2), 0, T(i)./Td);
Cvd(i) = (9.*N.*k.*I.*(T(i)./Td)^3);
end
Warning: Inf or NaN value encountered.
plot(T, Cvd)

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

카테고리

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