Error using integral function with anonymous function
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I am trying to use the following code to calculate the values of F(i,j) which is the integral of the anonymous function that I parameterized as a function of T and lambda. I believe I vectorized it correclty. However, the error I get says that the array has incompatible sizes, which I don't understand because the same size of array worked fine for another section of the code.
```C1 = 3.742e8;
C2 = 1.4388e4;
SB = 1.38*10.^-23; %Boltzmann's constant
C1 = 3.742*10^8; % First constant W-micron^4/m^2
C2 = 1.4388*10^4; %Second constant microns-K
T = [300,500,1000,2000,3000,5800]; %Temperature in Kelvins
lambda = 0.1:0.1:30;%wavelength in microns
lengthT = length(T);
length_L = length(lambda);
E = zeros(lengthT,length_L);
E_prime = zeros(lengthT,length_L);
F = zeros(lengthT,length_L);```
```for i = 1:lengthT
for j = 1:length_L
fun = @(T, lambda) C1./(SB.*T.^4*(lambda.^5.*(exp(C2/(lambda.*T))-1)));
F(i,j) = integral(fun(T,lambda),0,length_L);
end
end```
2 comentarios
Dyuman Joshi
el 10 de Dic. de 2023
With respect to which variable are you performing the integration?
And as the integrand is not varying w.r.t any loop, you should define it outside the loops.
Respuestas (2)
Karl
el 10 de Dic. de 2023
In your function call, you're passing T as a 6-element row vector and lambda as a 300-element row vector. Your function needs T and lambda to have the same size, to be able to evaluate lambda.*T
2 comentarios
Karl
el 10 de Dic. de 2023
Sorry that I might not have understood exactly what you're trying to do. If your aim is to evaluate for selected values of T, you could use something like:
C1 = 3.742e8;
C2 = 1.4388e4;
SB = 1.38*10.^-23; %Boltzmann's constant
C1 = 3.742*10^8; % First constant W-micron^4/m^2
C2 = 1.4388*10^4; %Second constant microns-K
T = [300,500,1000,2000,3000,5800]; %Temperature in Kelvins
lengthT = length(T);
lambdaMin = 0; % minimum wavelength in microns
lambdaMax = 30; % maximum wavelength in microns
F = 0;
fun = @(T, lambda) C1./(SB*T^4*(lambda.^5.*(exp(C2./(lambda.*T))-1)));
for i = 1:lengthT
F(i) = integral(@(x) fun(T(i), x),lambdaMin,lambdaMax);
fprintf('T=%d; F=%.4e\n', T(i), F(i))
end
Star Strider
el 10 de Dic. de 2023
Editada: Star Strider
el 10 de Dic. de 2023
C1 = 3.742e8;
C2 = 1.4388e4;
SB = 1.38*10.^-23; %Boltzmann's constant
C1 = 3.742*10^8; % First constant W-micron^4/m^2
C2 = 1.4388*10^4; %Second constant microns-K
T = [300,500,1000,2000,3000,5800]; %Temperature in Kelvins
lambda = 0.1:0.1:30;%wavelength in microns
lengthT = length(T);
length_L = length(lambda);
E = zeros(lengthT,length_L);
E_prime = zeros(lengthT,length_L);
F = zeros(lengthT,length_L);
for i = 1:lengthT
for j = 1:length_L
fun = @(T, lambda) C1./(SB.*T.^4.*(lambda.^5.*(exp(C2./(lambda.*T))-1)));
F(i,j) = integral2(fun,0,T(i),0,lambda(j));
end
end
figure
surfc(lambda, T, F, 'FaceColor','interp', 'EdgeColor','interp')
colormap(turbo)
xlabel('\lambda')
ylabel('T')
zlabel('F(T,\lambda)')
view(200,30)
EDIT — (10 Dec 2023 at 17:37)
I still don’t understand what the objective is here. The statement ‘my intention is to perform the intergration for lambda with respect to T’ is a bit mystifying, since λ does not appear to be a function of T.
So perhaps this instead —
C1 = 3.742e8;
C2 = 1.4388e4;
SB = 1.38*10.^-23; %Boltzmann's constant
C1 = 3.742*10^8; % First constant W-micron^4/m^2
C2 = 1.4388*10^4; %Second constant microns-K
T = [300,500,1000,2000,3000,5800]; %Temperature in Kelvins
lambda = 0.1:0.1:30;%wavelength in microns
lengthT = length(T);
length_L = length(lambda);
E = zeros(lengthT,length_L);
E_prime = zeros(lengthT,length_L);
F = zeros(lengthT,length_L);
for i = 1:lengthT
% for j = 1:length_L
fun = @(T, lambda) C1./(SB.*T.^4.*(lambda.^5.*(exp(C2./(lambda.*T))-1)));
F(i,:) = integral(@(T)fun(T,lambda),0,T(i), 'ArrayValued',1);
% end
end
figure
surfc(lambda, T, F, 'FaceColor','interp', 'EdgeColor','interp')
colormap(turbo)
xlabel('\lambda')
ylabel('T')
zlabel('F(T,\lambda)')
view(200,30)
.
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!