Re: N-dimensional NIntegrate
- To: mathgroup at smc.vnet.net
- Subject: [mg79083] Re: N-dimensional NIntegrate
- From: Peter Pein <petsie at dordos.net>
- Date: Wed, 18 Jul 2007 02:55:18 -0400 (EDT)
- References: <f7hrme$s03$1@smc.vnet.net>
mfedert at gmail.com schrieb: > Hi everyone, > > I want to define an N-dimensional definite integral---numerical > integration rather than symbolic. > > Eg, > > compute integral of f(x) dx > > where x can be an N-vector. I want to define the integral for general > N. (Obviously before evaluating the integral, I'll specify N.) I > can't think how to define the range of integration in a neat way in > the general case. Eg if the variables are x_{1}, x_{2}, ... x_{N}, > how can I specify that the integration range is > (say) R^{N}? > > Something like > > NIntegrate[ f(x), {x_{1}, -inf, inf}, {x_{2}, -inf, inf}, ..., {x_{N}, > -inf, inf} ] > > is what I want... would be neat to have x defined as a list or > something. > > There must be a neat way to do this. Sorry for being such an > amateur. > > Cheers, > MF > > Hi, this can be solved using Apply[] (@@): first we need a function to integrate: In[1]:= f[dim_] := (Times @@ (#1^2*Exp[-Subscript[x, #1]^(2*#1)/2] & ) /@ Range[dim])^(1/dim) In[2]:= f[3] Out[2]= 6^(2/3)*(E^(-(Subscript[x, 1]^2/2) - Subscript[x, 2]^4/2 - Subscript[x, 3]^6/2))^(1/3) just a shorthand for {x_{dim},...,x_{1}}}: In[3]:= theVector[dim_] := Table[Subscript[x, k], {k, dim, 1, -1}] ndimInt takes an expression, a list of variables and optional options [sic!] and should do what you described above. In[4]:= ndimInt[expr_, vars_, opts___] := (NIntegrate[expr, ##1, opts] & ) @@ ({#1, -Infinity, Infinity} & ) /@ vars Test: In[5]:= ndimInt[f[3], theVector[3], Method -> DoubleExponential] Out[5]= 101.7291537738819 Verification: In[6]:= Integrate[f[3], {Subscript[x, 3], -Infinity, Infinity}, {Subscript[x, 2], -Infinity, Infinity}, {Subscript[x, 1], -Infinity, Infinity}] Out[6]= 24*6^(7/12)*Sqrt[Pi]*Gamma[7/6]*Gamma[5/4] In[7]:= N[%] Out[7]= 101.72915377388057 so the outcome of ndimInt seems to be OK. hth, Peter