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

5 ビュー (過去 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)
ATHIRA
ATHIRA 2023 年 4 月 27 日
Thank you sir.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by