# Evaluating multiple integrals

I'm trying to evaluate many long but easy multiple integrals of the form

$$\int^{\tau}_{s_n=0}\int^{s_n}_{s_{n-1}=0}\dots\int^{s_2}_{s_1=0} C(s_1,\dots,s_n)\ ds_1\dots ds_n$$

where there are multiple integrals over some well behaving function $$C$$. Here is an example of a simple $$n=3$$ one:

$$\int^{\tau}_{s_3=0}\int^{s_3}_{s_2=0}\int^{s_2}_{s_1=0}\frac{1}{2\theta}e^{-(s_1+s_3)/\theta}ds_1ds_2ds_3.$$

The symbol $$\theta$$ is a constant. I've calculated this particular integral by hand, and found it to be equal to

$$\frac{\theta}{4}\Big(\theta-\theta e^{-2\tau/\theta}-2\tau e^{-\tau/\theta}\Big).$$

I can use the Mathematica Integrate function to check this

Integrate[(1/(2*\[Theta]))*E^(-(s+s)/\[Theta]),{s,0,\[Tau]},{s,0,s},{s,0,s}]

which returns my original answer. However I want to be able to evaluate these integrals for higher $$n$$ and different functions $$C$$, and I have many of these integrals I need to calculate.

Using this answer as a starting point, I managed to make a start at generalising the calculations

multi[f_, x_, t_, n_] :=
Integrate @@ {f @@ Array[x[#] &, {n}]} ~ Join ~ Array[{x[n - # + 1], 0, If[# == 1, t, x[n - # + 2]]} &, {n}]


however when I try to reproduce the above example on the function

h[s] = (1/(2θ)) E^(-(s + s)/θ)


the only answer I receive is a formatted version of my input: My question is: How do I get Mathematica to evaluate this integral and return the same answer as my first attempt? I'm a relatively new user of Mathematica, so please forgive me if this is a trivial question.

The command (where I have used $$t$$ for $$\theta$$ and $$a$$ for $$\tau$$)

Fold[Integrate[#1, {#2[], 0, #2[]}] &, Exp[-(s1 + s3)/t]/(2 t),
Partition[{s1, s2, s3, a}, 2, 1]]


evaluates the exact interval you have provided as an example. Note that the result I get is $$\frac{1}{4} \theta \left(\theta - \theta e^{-2 \tau/\theta} - 2 \tau e^{-\tau / \theta} \right)$$ which differs from your answer in that the last term has $$e^{-\tau/\theta}$$ rather than $$e^{-\tau \theta}$$.

The command does generalize readily, assuming that the lower limit of integration is always $$0$$ and the upper limit is the variable of integration for the next iterated integral. For example:

F[n_, g_] := Fold[Integrate[#1, {#2[], 0, #2[]}] &, g,
Partition[Append[Table[s[k], {k, 1, n}], a], 2, 1]]


which can be used to evaluate F[5, Exp[-s + s - 5 s]/t s] which is $$\int_{s_5 = 0}^\tau \int_{s_4 = 0}^{s_5} \int_{s_3 = 0}^{s_4} \int_{s_2 = 0}^{s_3} \int_{s_1 = 0}^{s_2} \frac{s_5}{\theta} e^{-s_1 + s_2 - 5s_5} \, ds_1 \, ds_2 \, ds_3 \, ds_4 \, ds_5.$$

• Thanks for the speedy reply. This works great, I guess the fold function was the more appropriate choice for this problem. The $e^{\tau/\theta}$ discrepancy is a typo on my part, I will correct it. – WilliamMorris Jan 6 '19 at 2:14
• @WilliamMorris I should point out that Fold is not even necessary, since Integrate[] handles multiple integrals directly; e.g., Integrate[Exp[-(s + s)/t]/(2 t), Sequence @@ Reverse[Insert[#, 0, 2] & /@ Partition[Append[Table[s[k], {k, 1, 3}], a], 2, 1]]] also works. – heropup Jan 6 '19 at 2:43

Instead of using Fold[] method in heropup's answer, which does multiple univariate integrals, you can directly construct a multivariate integral this way:

Inactive[Integrate][Exp[-(s + s)/θ]/(2 θ), ##] & @@
Prepend[Table[{s[k - 1], 0, s[k]}, {k, 3, 2, -1}], {s, 0, τ}]


$$\int_0^\tau \int_0^{s} \int_0^{s} \frac{e^\frac{-s-s}{\theta}}{2\theta} \mathrm ds\mathrm ds\mathrm ds$$

where I used Inactive[] so you could visually inspect the integral produced. To evaluate, just use Activate[]:

Activate[%]
1/4 θ (θ - E^(-((2 τ)/θ)) θ - 2 E^(-(τ/θ)) τ)


In general, you can use a construction like Integrate[f, ##] & @@ Prepend[Table[{s[k - 1], 0, s[k]}, {k, n, 2, -1}], {s[n], 0, τ}], minding the order of arguments used by Integrate[].