フィルターのクリア

Error using integral function with anonymous function

2 ビュー (過去 30 日間)
Matthew Palermo
Matthew Palermo 2023 年 12 月 10 日
コメント済み: Karl 2023 年 12 月 10 日
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
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.
Matthew Palermo
Matthew Palermo 2023 年 12 月 10 日
the integral should be varying lambda with wrt T, then take the next step in T, and integrate again wrt the update T

サインインしてコメントする。

回答 (2 件)

Karl
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 件のコメント
Matthew Palermo
Matthew Palermo 2023 年 12 月 10 日
As you can see in the code, my intention is to perform the intergration for lambda with respect to T, update the T value, and then perform the process again for the stated values of T. This is why T is only a 1x6 array vs a 1x300 array like Lambda. Is there a way to vecotrize these taking this into account, or maybe an alternative method to using the anonymous function without having to vecotrize so I can preserve the array sizes?
Karl
Karl 2023 年 12 月 10 日
Sorry that I might not have understood exactly what you're trying to do. If your aim is to evaluate Integral over lambda 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
T=300; F=3.6569e+15 T=500; F=3.9813e+15 T=1000; F=4.0896e+15 T=2000; F=4.1063e+15 T=3000; F=4.1081e+15 T=5800; F=4.1088e+15

サインインしてコメントする。


Star Strider
Star Strider 2023 年 12 月 10 日
編集済み: Star Strider 2023 年 12 月 10 日
Perhjaps using integral2 instead will work here —
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)
.

カテゴリ

Help Center および File ExchangeStrategy & Logic についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by