0
$\begingroup$

This works:

Table[Subscript[x, j], {j, 1, 10}]

But this doesn't:

x = Table[Subscript[x, j], {j, 1, 10}]

Giving this warning:

$RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of Subscript[x, 1]. >>

Can someone explain why this happens?

Students can do this of course:

Clear[x,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10]
x={x1,x2,x3,x4,x5,x6,x7,x8,x9,x10};

But what's the easiest way (for students just starting to use Mathematica) to enter x1, x2, through x100 and store them in the variable x?

$\endgroup$
5
  • 3
    $\begingroup$ You are defining x in terms of x; hence the error. Use Indexed[] instead. $\endgroup$ Commented Jun 23, 2016 at 19:22
  • 3
    $\begingroup$ Or do xs = Array[x, 100], which is how I would do it. If you insist on Subscripts, do xs = Array[Subscript[x,#]&, 100] $\endgroup$
    – march
    Commented Jun 23, 2016 at 19:55
  • 2
    $\begingroup$ Indexed or simply x[i] for the win. $\endgroup$ Commented Jun 23, 2016 at 20:29
  • $\begingroup$ @J.M. Can you please give me a complete line of code for the Indexed command, storing the answer in a variable, so I am sure of what you are suggesting? Thanks. $\endgroup$
    – David
    Commented Jun 23, 2016 at 21:33
  • $\begingroup$ Closely related: (22376). Also perhaps worth reading, including the various questions linked therein: (6511) $\endgroup$
    – Mr.Wizard
    Commented Sep 25, 2016 at 13:10

1 Answer 1

2
$\begingroup$

As others have said, you can't define a symbol in terms of its own DownValues or in terms of Subscripts of itself, so these both give recursion errors:

ClearAll[x]
x = Table[x[n], {n, 5}]
ClearAll[x]
x = Table[Subscript[x, n], {n, 5}]

I would suggest either of these workarounds:

ClearAll[x]
x = Table[Symbol["x" <> IntegerString[n]], {n, 5}]
(* {x1, x2, x3, x4, x5} *)

or

ClearAll[x]
xlist = Table[x[n], {n, 5}]
(* {x[1], x[2], x[3], x[4], x[5]} *)

That last one of course would work with subscripts too, although we try to discourage using subscripts for anything other than display formulas,

ClearAll[x]
xlist = Table[Subscript[x, n], {n, 5}]

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ I'd like to thank you and your colleagues for some excellent help. $\endgroup$
    – David
    Commented Jun 24, 2016 at 1:16
  • $\begingroup$ Jason, I marked this question as a duplicate. Please let me know if you disagree with that action. I noticed only after the final question here, though that is probably also a duplicate of others. $\endgroup$
    – Mr.Wizard
    Commented Sep 25, 2016 at 13:08

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