# In a for loop, how do I assign a table of values to a new variable for every iteration?

I am trying to create a divided differences table for a set of data of depth n, specified by the user. I am not sure how to create and assign a new variable name for each iteration of the for loop. Below is the code that I currently have written.

My current plan is to have an initial condition "d0table" and have further divided difference tables called "d1table", "d2table", and so on.

d0table = {53.32,
51.7, 56.05, 57.71, 62.05, 78.94, 74.74, 76.24, 68.29, 85.24}; (*initial condition*)
n = 1; (*depth of table*)

For[i = 1, i <= n, i++,
ToExpression["d" <> ToString[i] <> "table"] =
Table[(ToExpression["d" <> ToString[i - 1] <> "table"][[k + 1]] -
ToExpression["d" <> ToString[i - 1] <> "table"][[k]])/(
t[[k + i]] - t[[k]]), {k, 1,
Length[ToExpression["d" <> ToString[i - 1] <> "table"] - 1]}]
]


Upon running the code, I find that the tag "ToExpression" in "ToExpression[d1table]" is Protected. Is there any way to create a different variable name for each iteration of the for loop?

In the end, d1table should be an input like

d1table =
Table[(d0table[[k + 1]] - d0table[[k]])/(
t[[k + 1]] - t[[k]]), {i, 1, 9}]


and output to

{-1.62, 4.35, 1.66, 4.34, 16.89, -4.2, 1.5, -7.95, 16.95}


Thanks!

• Welcome to the Mathematica Stack Exchange. Could you manually calculate and add to your post what d1table should look like?
– Syed
Mar 17, 2022 at 17:29
• This is a terrible abuse of ToExpression. You can use indexed variables like dtable[i] and dtable[i-1] etc. for assignments. Mar 17, 2022 at 17:32
• @Roman, I can't believe I didn't think of this. Didn't realize you could specify parts for indexed variables like that - Thank you so much! Mar 17, 2022 at 17:44

Use NestList:

NestList[Differences, d0table, 3]

{{53.32, 51.7, 56.05, 57.71, 62.05, 78.94, 74.74, 76.24, 68.29,
85.24}, {-1.62, 4.35, 1.66, 4.34, 16.89, -4.2, 1.5, -7.95,
16.95}, {5.97, -2.69, 2.68, 12.55, -21.09, 5.7, -9.45,
24.9}, {-8.66, 5.37, 9.87, -33.64, 26.79, -15.15, 34.35}}


As suggested in the comments, ToExpression was the wrong command to use. Assigning indexed variables and asking Mathematica to find the specific parts works like a charm. The code with the correct output is below.

dtable[0] = {53.32, 51.7, 56.05, 57.71, 62.05, 78.94, 74.74, 76.24,
68.29, 85.24};(*initial condition*)
n = 1;(*depth of table*)
For[i = 1, i <= n, i++,
dtable[i] =
Table[(dtable[i - 1][[k + 1]] - dtable[i - 1][[k]])/(t[[k + i]] -
t[[k]]), {k, 1, Length[dtable[i - 1]] - 1}]]