calculating the integration, arrayfun problem???
1 view (last 30 days)
Show older comments
Hi all. I have a question. My code is
f = @(theta)(arrayfun(@(x,y)(det(sigmaZ/sin(x).^2 + eye(4)))^-1,theta));
out = quad(f,0,pi/2);
I want to add the expression "exp((sin(x)^2.*eye(4))^-1)" to my code.
But when I type f like this
f = @(theta)(arrayfun(@(x,y)((det(sigmaZ/sin(x).^2 + eye(4) ))^-1) *exp(((sin(x).^2).*eye(4))^-1),theta));
out = quad(f,0,pi/2);
It gives an error again. How can I fix it?
Error using arrayfun Non-scalar in Uniform output, at index 1, output 1. Set 'UniformOutput' to false.
Error in @(theta)(arrayfun(@(x,y)((det(sigmaZ/sin(x).^2+I))^-1)*exp(((sin(x).^2).*eye(4))^-1),theta))
Error in quad (line 72) y = f(x, varargin{:});
Error in Union_bound_rayleigh (line 104) out = quad(f,0,pi/2);
2 Comments
Azzi Abdelmalek
on 18 Sep 2012
Edited: Azzi Abdelmalek
on 18 Sep 2012
can you writte the function you want to compute? ( without arrayfun)
Answers (2)
Honglei Chen
on 18 Sep 2012
First of all, like @Azzi mentioned in the comment, your y is unused.
It's not clear what you want to do, but
exp(((sin(x).^2).*eye(4))^-1
returns a matrix, but the expression in front of it returns a scalar for each input. Therefore, the entire expression gives a matrix. Even if you set 'UniformOutput' to false, I don't think quad can deal with it directly. So I would suggest you to check your equation again and make sure that's what you want.
Mike Hosea
on 18 Sep 2012
Edited: Mike Hosea
on 18 Sep 2012
I'm not sure what type of mathematical problem you are trying to express and solve. If you have a function f(x) which returns a 4-by-4 matrix when given a scalar x, then each element of y = f(x) can be considered a function of x. Say f(x) = [f1(x),f2(x);f3(x),f4(x)]. Then
Q = integral(f,a,b,'ArrayValued',true)
will return a 4-by-4 matrix Q where Q(1,1) = integral(f1,a,b), Q(2,1) = integral(f2,a,b), etc.
If, however, your function y = f(x) is supposed to return a scalar y given a scalar x, then the integrators expect f to be defined in such a way that if x is a vector or matrix, the function is evaluated at each element. So, if you give an input of x = [1,2,3,4], it expects y = [f(1),f(2),f(3),f(4)] to be returned. ARRAYFUN is a handy way of making that work for a function f that is not written to work "elementwise" like that, but actually here again
Q = integral(f,a,b,'ArrayValued',true)
solves the problem because if you tell INTEGRAL that f is an array-valued function, it won't dare to call it with more than a scalar.
If you don't have r2012a, you won't have INTEGRAL. In that case, if you are trying to compute a matrix output, try using QUADV instead of QUAD. You won't need ARRAYFUN with QUADV.
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!