2
$\begingroup$

I want to do a multiple integrate with n variables. When I change the value of n, I do not want to rewrite the variables in the integral manually like here:

fn[n_] := Product[x[i], {i, 1, n}];
n = 3;
Integrate[fn[n], {x[1], 0, 1}, {x[2], 0, 1}, {x[3], 0, 1}]

Instead of that, I want to generate the variables in the integral somehow, depending only on n. I have tried the following but it did not work out.

fn[n_] := Product[x[i], {i, 1, n}];
vars = Row[Table[{x[i], 0, 1}, {i, 1, n}], ","]
n = 3;
Integrate[fn[n], vars]

Could you point out how to do that?

$\endgroup$
1
  • $\begingroup$ ”…but it did not work out.” can you, please, be more specific? While you got your answer (the key being some application of Sequence), one problem here is the attempted use of Row to create the structure that Sequence assists with generating. There would be many other ways to achieve the same thing, as the answer states…providing more detail/context would improve this question immensely! $\endgroup$ Commented Apr 23 at 8:44

1 Answer 1

3
$\begingroup$

I am sure there are many ways to do this. Instead of

fn[n_] := Product[x[i], {i, 1, n}];
n = 3;
Integrate[fn[n], {x[1], 0, 1}, {x[2], 0, 1}, {x[3], 0, 1}]
(* 1/8 *)

You could do

n = 3
v = Array[x, n]
Integrate[Times @@ v, Sequence @@ ({#, 0, 1} & /@ v)]
(* 1/8 *)

And for more variables, you just need to change n value. Nothing else needs to change.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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