How to do numerical multiple integral (more than triple) by using matlab?
Show older comments
Dear friends, my question is How to do numerical multiple integral (more than triple) by using matlab?
For example, I have a function,Y = Func. It has 5 var. Hence, Y= func(a,b,c,d,e). If I want to do the integration with respect to a,b,c,d,e. (var a is the first one, e is the last one.) And region of a = [0 ,b] (this is a function handle), b = [0,c], c= [0.d], d= [0,e], e=[0, Inf].
Thank you so much for your time.
Thank you.
1 Comment
Mike Hosea
on 29 Dec 2014
You have failed to supply any arguments to out1fcn. Remember that function handles always require arguments when you are evaluating them.
Accepted Answer
More Answers (2)
Shoaibur Rahman
on 28 Dec 2014
Edited: Shoaibur Rahman
on 28 Dec 2014
You can eliminate the variables one by one until you feel comfortable. You can make down to 3 or 2 or 1 variable before final integration, since Matlab has integral3, integral2 and integral to perform triple, double and single integration respectively.
y = @(a,b,c,d,e) a+b+c+d+e; % your function
y1 = @(a,b,c,d) integral(@(e) y(a,b,c,d,e),L,H); % L,H are the integration limits for e
y2 = @(a,b,c) integral(@(d) y1(a,b,c,d),L,H); % L,H are the integration limits for d
% and so on
Also look at integral doc for further input arguments if required.
2 Comments
Shoaibur Rahman
on 29 Dec 2014
Try with this, although the explicit integral could not be found:
y1 = int(y, u1, 0, u2)
y2 = int(y1, u2, 0, u3)
y3 = int(y2, u3, 0, u4)
y4 = int(y3, u4, 0, inf)
John D'Errico
on 28 Dec 2014
You need to consider the curse of dimensionality. Adaptive numerical integrations often take hundreds of function evaluations. Depending on the complexity of your functions, a single adaptive numerical integration might take hundreds or more function evaluations.
For example, a quick test of the integral function shows to integrate a simple function like sin(x) over the interval [0, 10*pi], took 390 function evaluations.
Even a very simple and smooth function like exp, over the interval [0, 2] took 150 function evaluations.
So suppose these were a reasonable counts for integral? A 5 level iterated integral could then take on the order of
(390)^5
ans =
9.0224e+12
or
(150)^5
ans =
7.5938e+10
function evaluations! In either case, we might be talking about something between 75 billion and 9 trillion function evaluations.
So if you have no choice but to do the multiple integration using such a numerical tool, do what you must do, but expect it to be SLOW. Better is to look for alternatives. For example, can you do at least one of those integrals analytically?
I would point out that far too often, I see people doing numerical integration of simple Gaussian density functions, when in fact, that is doable using a function call for the Gaussian CDF (or erfc if you wish to do the transformation yourself.)
Another choice is the use of Monte Carlo integration, which becomes relatively more efficient when you go into a higher number of dimensions.
9 Comments
Mike Hosea
on 29 Dec 2014
True (curse of dimensionality). The integralN function is cranking... I have no idea when it will stop. :)
John D'Errico
on 29 Dec 2014
Long ago, in galaxy far, far away, I did write a 5-fold integration code for a specific problem. Ran like a dog with two legs. The only thing I learned from that effort was to never try to solve that class of problem with brute force integration. We did not need the accuracy I thought I had to achieve, and there would have been many alternative routes I'd try now if I truly understood that fact. So, I suppose I did get something useful out of it, even though the project was a complete failure otherwise.
sun
on 29 Dec 2014
Mike Hosea
on 29 Dec 2014
This is not really a "MATLAB" topic. John's pointing out that the amount of work required to do a 5-fold integral numerically via a quadrature rule is roughly the amount of work raised to the 5th power of doing a single integral. Sometimes that's a pessimistic analysis, but sometimes it's quite accurate. If you look at the documentation that I wrote in integralN, you'll see that I don't really recommend the approach. It's worth a try, but I think I used the words "excruciatingly slow" for 6-fold integrals. Your problem appears to be such in 5-fold integrals. I will see if it was successful tomorrow morning.
sun
on 29 Dec 2014
Mike Hosea
on 29 Dec 2014
The integration was unsuccessful:
>> myscript
Warning: Reached the maximum number of function evaluations (10000). The result fails the global error test.
> In integral2Calc>funfun\private\integral2Calc>integral2t (line 129)
[snip]
q =
NaN
Since the number of function evaluations was exceeded in a double integral sub-calculation, the boundary singularity may be a bit too strong for these codes to handle.
John D'Errico
on 29 Dec 2014
Edited: John D'Errico
on 29 Dec 2014
I was a bit surprised, that the number of function evals taken was not apparently one of the things that would be returned by an integration tool, as one of the later arguments. (Any code that I would write would offer that as important and useful information, but I'm a bit over the top sometimes in terms of how complete I make my code.)
It is trivial to learn however. I simply wrote a kernel function for those problems that counted the number of times it was called. In fact, integral only called the kernel function once for exp(x), but it sent in 150 points in that call to be evaluated. the sin(x) kernel was a bit more curvy, it had two calls to the objective, with 390 total points to evaluate.
sun
on 29 Dec 2014
Mike Hosea
on 29 Dec 2014
Edited: Mike Hosea
on 29 Dec 2014
I decided not to return the number of function evaluations because I think it is more confusing to the average user than it is helpful in the context of vectorized methods. Do we return 1 or 150? The former is literally correct, the latter is the number that you might connect with your past uses of this measure of "work". The 150 number, is, of course, just reflecting the conservative initial mesh that INTEGRAL uses. Possibly it could have gotten away with many fewer evaluations, and in fact, when INTEGRAL2 and INTEGRAL3 iterate with INTEGRAL's 1-D algorithm, they do use a coarser initial mesh.
What I have been wanting to do is develop some kind of visualization of the integration data that will help you to figure out what happened when integrations fail, something that generalizes well to all methods we might want to put under the "hood" of INTEGRAL, INTEGRAL2, and INTEGRAL3. My first stab at this was the "FailurePlot" in QUAD2D, but that was "free", and applying something like that for the 'iterated' method would require extra work and storage (as INTEGRAL does not maintain a master list of all abscissa's used), not to mention require some creative choices when limits are infinite. At the present time I recognize that there is a dearth of diagnostic information available.
Another thing I want is an "expert" interface to the integration routines that exposes all manner of knobs and switches as well as provides extra output data, such as the global error estimate and perhaps a lot more.
Categories
Find more on Numeric Solvers 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!