I would like to sum elements of a vector variable, e.g., Is
xvec = Array[x, 10]
s24 = Sum[xvec[i], {i, 2, 4}]
where $s_{24}:=\sum_{i=2}^4 x_i$.
However, I am returned a "Recursion limit". What did I do wrong?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityYou should use Span
to retrieve elements from index 2 to 4. Also note that accessing list elements by indices in Mathematica requires double square brackets (see Part
).
Total[xvec[[2 ;; 4]]]
(* x2 + x3 + x4 *)
Alternatively, you cal also use Sum
, but again – mind the double square brackets.
Sum[xvec[[i]], {i, 2, 4}]
(* x2 + x3 + x4 *)
ClearAll[xvec, x]
and re-executing. Or quit and restart the kernel. (2) Distinguish the syntax for a function callxvec[i]
from extracting aPart
xvec[[i]]
. You want the latter. $\endgroup$