calculating the integration, arrayfun problem???

조회 수: 6 (최근 30일)
Serhat
Serhat 2012년 9월 18일
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
Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 18일
why @(x,y)
y is not used
Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 18일
편집: Azzi Abdelmalek 2012년 9월 18일
can you writte the function you want to compute? ( without arrayfun)

댓글을 달려면 로그인하십시오.

답변 (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에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by