numerical integration with nonarray function

조회 수: 4 (최근 30일)
zoe
zoe 2013년 4월 12일
Hello, guys,
I have a trouble in using numerical integration command. The integrand in my case is det(x*A),x is the variable, and A is a n*n matrix. I noticed that nearly all the numerical integration command has requirement of array function, which means, they have to use .*, ./ when needed? do we have numerical integration command without such requirement?
Thank you very much!
Clair

채택된 답변

Mike Hosea
Mike Hosea 2013년 4월 12일
It is, indeed, unnecessary to perform numerical integration on this integrand. However, to answer the question in general, The INTEGRAL function has an option called 'ArrayValued' that allows you to integrate array-valued functions. When this option is set to true, the integrator will only call the integrand function with scalar inputs, and it doesn't matter if the "array value" only has one element. So, this does it
integral(@(x)det(A*x),a,b,'ArrayValued',true)
For example
>> rng(0)
>> A = round(100*rand(3))
A =
81 91 28
91 63 55
13 10 96
>> integral(@(x)det(A*x),0,1,'ArrayValued',true)
ans =
-7.050624999999999e+04
>> det(A)/4
ans =
-7.050624999999999e+04
  댓글 수: 2
zoe
zoe 2013년 4월 13일
편집: zoe 2013년 4월 13일
Thank you so much, Mike, it is so helpful. I am considering the case where x is a bivariate. ur methods still works! But,it takes sometime to have the result, do we have the similar result for double integral, and it will produce result really fast?. ps: you are right, it is not necessary. det(A*x) is a simplified representation for my problem, and the function i am used is more complicated. Thanks again.
Mike Hosea
Mike Hosea 2013년 4월 15일
편집: Mike Hosea 2013년 4월 15일
ARRAYFUN is usually a faster way for scalar-valued problems, and since INTEGRAL2 and INTEGRAL3 do not support an 'ArrayValued' option, you will have to do something like that (or write a wrapper function with a loop). Here's how to use ARRAYFUN. If f(x,y) is a bivariate integrand but that only works with scalar inputs, integrate
g = @(x,y)arrayfun(f,x,y)
Try this technique with the univariate function as well if speed is an issue, i.e. integrate
g = @(x)arrayfun(f,x);
Here I am assuming in both cases that f is defined as an anonymous function. If f is defined in a MATLAB program file, f.m, then of course you need @f instead of just f as the first argument to ARRAYFUN.

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

추가 답변 (2개)

Yao Li
Yao Li 2013년 4월 12일
Assuming
x=[x0,x1,x2];
B=[det(x0*A) det(x1*A) det(x2*A)];
trapz(x,B)

Roger Stafford
Roger Stafford 2013년 4월 12일
With x used as a vector, your integrand can be written as:
(x.^n)*det(A)
with the x factored out, which should integrate very nicely. However, why bother to do numerical integration when the indefinite integral is already known from elementary calculus, namely
x^(n+!)/(n+1)*det(A)
  댓글 수: 1
zoe
zoe 2013년 4월 13일
편집: zoe 2013년 4월 13일
det(A*x) is a simplified form. The really form is det(A-x*B-y*C),A,B,C is matrix of n*n. So it is indeed a double integral

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by