I have a function of the following form: $$ \varphi_n(x)= \underbrace{\int\limits_\mathbb{R}\ldots\int\limits_\mathbb{R}}_{n}\exp\left[-\sum\limits_{j=2}^{n}\left(x_{j}-x_{j-1}\right)^2-(x-x_{n})^2\right]dx_1\ldots dx_{n}\quad \left(\varphi_n(x)\equiv \pi^{n/2}\right), $$ and I'm trying to define a function Phi[x, n] that will calculate the above integral. I know, how to define this function for fixed $n$, for example $$ \varphi_1(x)= \int\limits_\mathbb{R}e^{-(x-x_{1})^2}dx_1, $$ defines as

Phi1 = Integrate[Exp[-(x - #)^2], {x, -Infinity, Infinity}] &;

and $$ \varphi_2(x)= \int\limits_\mathbb{R}\int\limits_\mathbb{R}e^{-(x_2-x_1)^2-(x-x_2)^2}dx_1dx_2, $$ defines as

Phi2 = Integrate[Integrate[Exp[-(x2 - x1)^2 - (# - x2)^2], {x1, -Infinity, Infinity}], {x2, -Infinity, Infinity}] &;

How to define a function, that would take $n$ as an argument?


I bet you could do this more elegantly with Fold, but this gets you there,

Phi[n_] := 
 Integrate[Exp[-Sum[(x[j] - x[j - 1])^2, {j, 2, n}] - (x - x[n])^2], 
  Sequence @@ ({x[#], -∞, ∞} & /@ Range[n])]

Phi /@ Range[5]
(* {Sqrt[π], π, π^(3/2), π^2, π^(5/2)} *)

You could make it a function of x, but as you write, the answer is independent of x so I don't see why you would.

  • $\begingroup$ @Glinka In order to obtain a true dependence on x you could change the integration interval. (Although the integral might not be solved symbolically anymore). $\endgroup$ – Dr. Wolfgang Hintze Mar 16 '16 at 15:47
  • $\begingroup$ @JasonB, thank you for your answer! Your method worked on the example from the question, and it worked when I used a function that actually depended on $x$. The problem though that it works very slow when $n>3$ (for $n=3$ the process lasts several minutes and for $n=4$ the process doesn't seem to end). I figured that I use NIntegrate instead of Integrate, it would speed up a process. But when I just change Integrate to NIntegrate in your code, it show the following error: Invalid integration variable or limit(s) in {x[1],-[Infinity],[Infinity]}. What do I do wrong? $\endgroup$ – Glinka Mar 16 '16 at 19:03
  • $\begingroup$ @Glinka It's true that multidimensional integrals can take a while, and can be sped up by using numerical methods. But it's hard to fix an error without having the code to reproduce it. You could post it here as a comment or as another question. $\endgroup$ – Jason B. Mar 16 '16 at 19:07
  • $\begingroup$ @Dr.WolfgangHintz, in our task we always consider integrals over $\mathbb{R}$, and a true dependence on x occurs when we consider more complex integrands. In the example from the question $x$ variable is dummy by accident. $\endgroup$ – Glinka Mar 16 '16 at 19:12
  • $\begingroup$ @JasonB, I posted it as a new question: mathematica.stackexchange.com/questions/110197/… $\endgroup$ – Glinka Mar 16 '16 at 19:36

My take:

With[{n = 6}, 
     Integrate[Exp[-#.# & @ Differences[Append[Array[C, n], x]]], ##] & @@ 
     Array[{C[#], -∞, ∞} &, n]]

Here's some silliness along the lines of Jason B's answer:

int[n_]:= Integrate[
  Exp[-#^2] &@*Subtract @@@ Partition[Array[x, n + 1, 0], 2, 1] // Times @@ # &, 
  Sequence @@ Array[{x[#], -∞, ∞} &, 3]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.