Error using integral function with anonymous function
조회 수: 3 (최근 30일)
이전 댓글 표시
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
Dyuman Joshi
2023년 12월 10일
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.
답변 (2개)
Karl
2023년 12월 10일
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
Karl
2023년 12월 10일
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
2023년 12월 10일
편집: Star Strider
2023년 12월 10일
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!