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 个评论
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by