How to do numerical multiple integral (more than triple) by using matlab?
117 views (last 30 days)
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.
Mike Hosea on 28 Dec 2014
Edited: Mike Hosea on 28 Dec 2014
You can use INT if your problem can be handled symbolically. If not, numerical integration of a 5-fold integral in MATLAB requires nesting INTEGRAL, INTEGRAL2, and INTEGRAL3. I got tired of explaining how to do this, so I wrote integralN and put it on the file exchange.
There are some examples in the text at the top of the file. Basically, it's a straightforward extension of INTEGRAL2 and INTEGRAL3 in terms of how you call it.
You're going to have to reorder your variables because of the way your region is defined. By convention, the outermost integral is the first variable, and the innermost integral is the last. You need that reversed.
q = integeralN(@(e,d,c,b,a)fun(a,b,c,d,e),0,inf,0,@(e)e,0,@(e,d)d,0,@(e,d,c)c,0,@(e,d,c,b)b)
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.
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
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.