I need to evaluate NIntegrate with a variable number of variables.
Here's a concrete example. I'll accept an answer that finds a way to make the example work. I want to calculate the following integral:
$$\int_0^1 \left( \sum_{i=1}^n x_i \right) \prod_{i=1}^n\mathrm{d}x_i = \frac{n}{2} $$
where $n$ is the number of variables. This particular integral can be separated into independent integrals, but in general this is not possible. I chose it because it is a simple example.
In Mathematica, we start by defining n, say:
n = 4;
Then I try to evaluate the following command:
NIntegrate[Sum[x[i], {i, 1, n}],
Sequence@@Table[{x[i], 0, 1}, {i, 1, n}]]
and it gives an error. What I'm trying to do here is use x[1], x[2], ...,x[n] as variables of integration.
Note that Integrate does work. So the problem is restricted to NIntegrate.
Evaluate[Sequence@@Table[...]]insideNIntegratesolve your issue? – István Zachar Dec 12 '13 at 16:48NIntegrate[Sum[x[i], {i, 1, n}], ##] & @@ Table[{x[i], 0, 1}, {i, 1, n}]? – andre314 Dec 12 '13 at 17:27NIntegrate[...,##] @@ Table[...]comes from the documentation ofSlotSequence(##) where there is a example ofNIntegrate[...]over 1000 variables (see Application) – andre314 Dec 12 '13 at 17:52