How can I feed MATLAB's integral2 a vector of parameters using nested functions?

5 ビュー (過去 30 日間)
I want to calculate a 2D-integral for a vector of parameters in MATLAB. I know that integral2 has no 'ArrayValued' option. I've looked at a very similar question: I want to calculate a 2D-integral for a vector of parameters in MATLAB. I know that integral2 has no 'ArrayValued' option. I've looked at a very similar question: https://stackoverflow.com/questions/31032086/how-can-i-feed-matlabs-integral2-a-vector-of-parameters-via-nested-functions but am worried that my problem might be caused due to having a an array (T) of 2 dimensions in the overall for loops:
for j= 0:1:max_time
for n= 1:N
if n < N && n > 1 % Intermediate Layers
fun1 = @(lambda,mu) [(2*h*c^2./(lambda).^(5)).*(1./(exp(h*c./(lambda.*k_b.*Tp))-1)).*exp(-(z(n).*4*pi.*niquartz(n)./lambda-tauprime(n))./mu) + ...
(2*h*c^2./(lambda).^(5)).*(1./(exp(h*c./(lambda.*k_b.*T(n,j+1)))-1)).*(1-exp(-(z(n).*4*pi.*niquartz(n)./lambda-tauprime(n))./mu))].*2*pi.*mu;
fun2 = @(lambda,mu) [(2*h*c^2./(lambda).^(5)).*(1./(exp(h*c./(lambda.*k_b.*T(n,j+1)))-1)).*exp(-(z(n).*4*pi.*niquartz(n)./lambda-tauprime(n))./mu) + ...
(2*h*c^2./(lambda).^(5)).*(1./(exp(h*c./(lambda.*k_b.*T(n,j+1)))-1)).*(1-exp(-(z(n).*4*pi.*niquartz(n)./lambda-tauprime(n))./mu))].*2*pi.*mu;
Fplus(n,j+1) = integral2(fun1,7e-6,50e-6,0,1); % Upward Flux
Fneg(n,j+1) = integral2(fun2,7e-6,50e-6,0,1); % Downward Flux
end
end
The error message is "Matrix dimensions must agree." But technically, there are no matrices involved, since none of the parameters is array-valued anymore. What argument do I need to pass on differently? Alternatively, can you suggest a different way of approaching this integral?
  4 件のコメント
Walter Roberson
Walter Roberson 2018 年 1 月 3 日
Unrecognized function or variable 'CO2frost'.
Error in test (line 8)
tfrost = CO2frost(P); % CO2 frost point
Neil Guertin
Neil Guertin 2018 年 1 月 5 日
You are most likely seeing this error because the values passed to your functions are not what you expect them to be.
For performance reasons integration is vectorized and your functions are evaluated on a vector of many values at once. The output of this function must be the same size as the input, even when the inputs given are vectors.
For example, the following will error:
>> integral(@(x)8,0,1)
This is because the function @(x)8 can be given a vector as input, but always returns a scalar. To fix this, verify that your functions outputs are the same size as the inputs.
>> integral(@(x)8*ones(size(x)),0,1)

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

採用された回答

Neil Guertin
Neil Guertin 2018 年 1 月 5 日
There is currently no way to do this using integral2. As a workaround you could run integral2 separately for each parameter value or use a nested call to integral with 'ArrayValued',true.
  1 件のコメント
Aditya Khuller
Aditya Khuller 2018 年 1 月 6 日
Thank you Matt, Walter and Neil! I used a nested call to 'integral' as a workaround.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by