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

5 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 3 Jan 2018
Unrecognized function or variable 'CO2frost'.
Error in test (line 8)
tfrost = CO2frost(P); % CO2 frost point
Neil Guertin
Neil Guertin on 5 Jan 2018
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)

Sign in to comment.

Accepted Answer

Neil Guertin
Neil Guertin on 5 Jan 2018
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.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!