Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
ATHIRA
ATHIRA am 26 Apr. 2023
Kommentiert: ATHIRA am 27 Apr. 2023
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);

Antworten (1)

albara
albara am 26 Apr. 2023
Bearbeitet: Rik am 26 Apr. 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
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 Kommentare
albara
albara am 26 Apr. 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
Warning: Inf or NaN value encountered.
plot(T, Cvd)

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by