1

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.

a06e
  • 11,327
  • 4
  • 48
  • 108

0 Answers0