# Problem with Do Loop and TableForm

The following code is supposed to generate 10 tables based on different calculations for different q:

numberofrows = 40;
deltat = 0.00000001;
Tref = {353.15, 333.15};
nref = {0.830144995, 0.654953157};
kref = {1.541030575, 0.016538198};
Earef = {106310.1492, 261971.1364};
initialxt = 1*10^-12;
q = {0.1, 0.3, 1, 3, 10, 30, 100, 300, 600, 1000};

Do[
ttall =
TableForm[
Join[{{"Delta t (s)", b = deltat}, {"q (K/s)",
b = q[[j]]}, {""}, {""}, {"Time(s)", "T[C]", "K(T)=k^(1/n)",
"dx/dT", "x(t)", "DH,aged-DH,unaged (J/g)",
"Check dx"}, {a = 0, b = Tnematiconset,
c = (kref[]*
Exp[(-Earef[]/
8.314)*((1/(Tnematiconset + 273.15)) - (1/
Tref[]))])^(1/nref[]),
d = (c*
nref[]*(1 -
initialxt)*(-Log[1 - initialxt])^((nref[] - 1)/
nref[]))/q[[j]],
e = (b - b)*d + initialxt,
f = e*dhnematicmax, g = e}},
Table[{a[i] = a[i - 1] + b ,
b[i] = ((b[i - 1] + 273.15) - b*a[i]) - 273.15,
c[i] = (kref[]*
Exp[(-Earef[]/
8.314)*((1/(b[i] + 273.15)) - (1/Tref[]))])^(1/
nref[]),
d[i] = (c[i + 1]*
nref[]*(1 -
e[i - 1])*(-Log[1 - e[i - 1]])^((nref[] - 1)/
nref[]))/q[[j]],
e[i] = (b[i] - b[i + 1])*d[i] + e[i - 1],
f[i] = e[i]*dhnematicmax , g[i] = e[i] - e[i - 1]}, {i, 7,
numberofrows - 9}]]] // Print, {j, 1, 10}
]


if I run the code removing the Do loop, the code runs fine for one q (e.g. q[]).However, I want to run it for all different q values (10 in total).How can I make this happen with no issues?.

Additionally, I notice that everytime I close the mathematica program and I open it again, for some reason I have to use instead of {i, 7,numberofrows - 9} I have to change it to {i, 7,numberofrows - 10} and then {i, 7,numberofrows - 11} and so on. Is this a problem of using TableForm in the way I am doing it or why does this happen?

• Not enough explanation for your complex code. We would have to understand what you need by reading CODE THAT DOESNT WORK by your own admission, and then fix it? Help us help you. Describe your problem in detail and the desired output, and provide a minimal working example. May 2, 2020 at 5:07
• Not enough explanation, nor enough code. When I cutted and pasted it into a Mathematica notebook there were undeclared symbols. Call me a millennial flake if you will, but I gave up at that point. May 2, 2020 at 10:48

Generally, I suggest that it's better to compute a table first, then use TableForm to format it. Putting the code to compute the table inside TableForm is difficult to understand and hard to test. Also, consider using TableForm's TableHeadings option.

A part of the problem seems to be something you found. Briefly, because of the code inside the Table statement, the results for step i are dependent on (or require) the values computed for step i + 1. In summary, the problem is caused by c[i + 1] and b[i + 1] within the Table statement.

I notice that everytime I close the mathematica program and I open it again, for some reason I have to use instead of {i, 7,numberofrows - 9} I have to change it to {i, 7,numberofrows - 10} and then {i, 7,numberofrows - 11} and so on. Is this a problem of using TableForm in the way I am doing it or why does this happen?

So why does changing {i, 7, numberofrows - 9} seem to give good answers? Each time the Table statement runs, there are undefined values for the maximum, ending value of i. That's why reducing i from numberofrows - 9 to numberofrows - 10 or numberofrows - 11 seems to fix the problem. Moreover, when Mathematica exits, the values that were computed for the largest i are lost, and the problem comes back.

Let's look only at the case when j = 1, because running {j, 1, 10} produces lots of output, takes a long time, and, most importantly, it hides what's happening. I want to break the code apart so we can test each step. First, your initial values:

Clear["Global*"](*Important step unless Mathematica is restarted each time*)
numberofrows = 40;
deltat = 0.00000001;
Tref = {353.15, 333.15};
nref = {0.830144995, 0.654953157};
kref = {1.541030575, 0.016538198};
Earef = {106310.1492, 261971.1364};
initialxt = 1*10^-12;
q = {0.1, 0.3, 1, 3, 10, 30, 100, 300, 600, 1000};
(*I assume some random values for unknown variables*)
Tnematiconset = 100.;
dhnematicmax = 200.;


Set j=1 to define values we need for the Table statement. We don't need the TableForm, just the values for the variables. (This testing awkwardness is why you should compute a table first, and format with of TableForm afterwards.)

j = 1;
(*Do[
ttall =
TableForm[*)
Join[{{"Delta t (s)", b = deltat}, {"q (K/s)",
b = q[[j]]}, {""}, {""}, {"Time(s)", "T[C]",
"K(T)=k^(1/n)",
"dx/dT", "x(t)", "DH,aged-DH,unaged (J/g)",
"Check dx"}, {a = 0, b = Tnematiconset,
c = (kref[]*
Exp[(-Earef[]/
8.314)*((1/(Tnematiconset + 273.15)) - (1/
Tref[]))])^(1/nref[]),
d = (c*
nref[]*(1 -
initialxt)*(-Log[1 - initialxt])^((nref[] - 1)/
nref[]))/q[[j]],
e = (b - b)*d + initialxt,
f = e*dhnematicmax, g = e}}(*,*)
](*];  ... // Print, {j, 1, 10}
]*);


Now we can look at the Table statement alone. First, I'll make a function to show results from Table for values of i. Let's see what happens at each step (that is, for each maximum value of i).

tbl[iMax_] := Table[{a[i] = a[i - 1] + b,
b[i] = ((b[i - 1] + 273.15) - b*a[i]) - 273.15,
c[i] = (kref[]*
Exp[(-Earef[]/
8.314)*((1/(b[i] + 273.15)) - (1/Tref[]))])^(1/
nref[]),
d[i] = (c[i + 1]*
nref[]*(1 -
e[i - 1])*(-Log[1 - e[i - 1]])^((nref[] - 1)/
nref[]))/q[[j]],
e[i] = (b[i] - b[i + 1])*d[i] + e[i - 1],
f[i] = e[i]*dhnematicmax, g[i] = e[i] - e[i - 1]}, {i, 7, iMax}];


Here's the result after one iteration of the Table statement with maximum i = 7. Notice there are undefined values for b and c, that is, b[i + 1] and c[i + 1] are undefined when i = 7.

tbl
(* {{1.*10^-8, 100., 17.4386, 65.4859 c,
0.0000413008 + 65.4859 (100. - b) c,
200. (0.0000413008 + 65.4859 (100. - b) c),
0. + 65.4859 (100. - b) c}} *)


With maximum i = 8, the first row has values for b and c, but there are undefined values for b and c.

tbl
(* {{1.*10^-8, 100., 17.4386, 1141.98, 0.0000435848, 0.00871695, 2.28394*10^-6},
{2.*10^-8, 100., 17.4386, 64.7685 c,
0.0000435848 + 64.7685 (100. - b) c,
200. (0.0000435848 + 64.7685 (100. - b) c),
0. + 64.7685 (100. - b) c}} *)


This repeats as the maximum value of i increases. In effect, you must compute the values for step i + 1 to define values for needed for step i.

tbl[numberofrows - 9]
(* {
{1.*10^-8, 100., 17.4386, 1141.98, 0.0000435848, 0.00871695, 2.28394*10^-6},
{2.*10^-8, 100., 17.4386, 1129.47, 0.0000469732, 0.00939464, 3.38844*10^-6},
[ ... ]
{2.5*10^-7, 100., 17.4386, 42.9976 c,
0.000322286 + 42.9976 (100. - b) c,
200. (0.000322286 + 42.9976 (100. - b) c),
0. + 42.9976 (100. - b) c}
} *)


When you make numberofrows - 9 smaller, the values for larger iterations exist until you exit Mathematica.

Again, for testing and ease of understanding, it's better to compute a table first, before using TableForm. Make that change, then look at how each row is computed. You compute the initial values for a to g, but initial values for c and b are missing, or maybe c[i + 1] and b[i + 1] are incorrect in the Table` statement.