I am having problem with integration in matlab. But unable to find the error.
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
albara
el 26 de Abr. de 2023
Editada: Rik
el 26 de Abr. de 2023
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 comentarios
albara
el 26 de Abr. de 2023
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)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!