2
$\begingroup$

Im looking at n particles and functions of their coordinates which I want to integrate over:

{r1, r2,..., rn} = Table[Symbol["r" <> ToString@i], {i, n}] =: LoC[n_] 

then I want to define functions e.g.

f[x_] := x

g[L_] := Sum[f[i], {i, L}]

where later I plug in L = LoC[n].

Then I define intervals for integration:

IB[i_] := {Symbol["r" <> ToString@i], x, R} = {r1, x, R}

IIB[n_] := Array[IB, n] = {{r1, x, R},...., {rn, x, R}}

but the integration doesnt work:

NIntegrate[g[LoCC[3]], Row[IIB[3], ","]].

With errors like "NIntegrate::ilim: Invalid integration variable or limit(s) in {r1,1,100}, {r2,1,100}, {r3,1,100}."

What am I doing wrong? I guess the arguments or the integration limits arent defined properly.

n=2 (x=1, R=100):

LoC[2]
IB[2]
IIB[2]

Out[10]= {r1, r2}

Out[11]= {r2, 1, 100}

Out[12]= {{r1, 1, 100}, {r2, 1, 100}}

In[32]:= g[LoC[2]]

Out[32]= r1 + r2

The row command removes one pair of brackets {} because Integrate wants the integration limits like

{r1, 1, 100}, {r2, 1, 100}

In[33]:= Row[IIB[2], ","]

Out[33]= {r1, 1, 100}, {r2, 1, 100}

but then

In[34]:= NIntegrate[g[LoC[2]], Row[IIB[2], ","]]

During evaluation of In[34]:= NIntegrate::ilim: Invalid integration 
variable or limit(s) in {r1,1,100},{r2,1,100}.

Out[34]= NIntegrate[g[LoC[2]], Row[IIB[2], ","]]
$\endgroup$
1
  • 2
    $\begingroup$ You can't integrate across limits in a Row - that's for presentation. Remove the Row. $\endgroup$
    – flinty
    Commented May 25, 2020 at 12:15

1 Answer 1

6
$\begingroup$

The integration limits can't be Row and there were some syntax issues I cleaned up. Hopefully the comments (* ... *) below explain everything. You can change the $x,R,n$ in the With to different values if needed:

(* Function to create the symbols r[1],r[2],...,r[n] *)
LoC[n_] := Array[r,n]

(* Functions to integrate *)
f[x_] := x
g[l_] := Sum[f[i], {i, l}]

(* Function to generate an interval for integration *)
iv[variable_, x_, R_] := {variable, x, R}

(* Given x,R,n - integrate g[LoC[n]]. *)
With[{x = 1, R = 100, n = 3},
 NIntegrate[g[LoC[n]],
  (* generate intervals and splice into arguments here with Sequence *)
  Evaluate[Sequence @@ (iv[#, x, R] & /@ LoC[n])]
  ]
 ]
$\endgroup$
3
  • 1
    $\begingroup$ Good cleanup. While you are at it, I would suggest changing the Symbol+ToString stuff to regular indexed variables, like r[1], r[2],.... Then LoC is simply Array[r, n] or r /@ Range[n]. $\endgroup$ Commented May 25, 2020 at 12:41
  • $\begingroup$ Done. Much cleaner looking. $\endgroup$
    – flinty
    Commented May 25, 2020 at 12:43
  • $\begingroup$ Thaanks! No way I wouldve figured that out. :) $\endgroup$
    – VN23
    Commented May 25, 2020 at 15:06

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