I am having problem with integration in matlab. But unable to find the error.
6 次查看(过去 30 天)
显示 更早的评论
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);
0 个评论
回答(1 个)
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
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
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
plot(T, Cvd)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!