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

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

What argument do I need to pass on differently?
We can't locate the problem for you because we don't have h, c, T or anything else needed to run your code. However, if it's a simple matrix dimensions error, it should be a routine matter for you to locate where it's occurring and why using the debugger.
Thanks for your quick reply! When I use the debugger, I'm not sure what I'm supposed to look for- every term appears to be a scalar when it's being multiplied in the function- do you know why that could be? I'm basically asking how I should find out what matrices' dimensions don't match and why it matters in the first place. The reason I didn't post the entire code was is because it's long and has an associated function and 3 data files. This is the full error:
Matrix dimensions must agree.
Error in
test>@(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
Error in integral2Calc>integral2t/tensor (line 228)
Z = FUN(X,Y); NFE = NFE + 1;
Error in integral2Calc>integral2t (line 55)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 9)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 106)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Error in test (line 53)
Fneg(n,j+1) = integral2(fun2,7e-6,50e-6,0,1); %Downward Flux
Unrecognized function or variable 'CO2frost'.
Error in test (line 8)
tfrost = CO2frost(P); % CO2 frost point
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

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 Comment

Thank you Matt, Walter and Neil! I used a nested call to 'integral' as a workaround.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!