calculating the integration, arrayfun problem???

1 次查看(过去 30 天)
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 个)

Honglei Chen
Honglei Chen 2012-9-18
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.
  1 个评论
Serhat
Serhat 2012-9-18
sigmaZ=rand(4,4);
mZ=1;
f = @(theta)(arrayfun(@(x,y) (det(sigmaZ/sin(x).^2 + eye(4)))^-1... *exp(-((sigmaZ+(sin(y).^2)*eye(4))^-1)),theta));
I know the expression inside exp is a matrix. That is why I am in trouble to calculate the integral. Isn't it possible to evaluate this in matlab?

请先登录,再进行评论。


Mike Hosea
Mike Hosea 2012-9-18
编辑:Mike Hosea 2012-9-18
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.

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by