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[1] = deltat}, {"q (K/s)",
b[2] = q[[j]]}, {""}, {""}, {"Time(s)", "T[C]",
"K(T)=k^(1/n)",
"dx/dT", "x(t)", "DH,aged-DH,unaged (J/g)",
"Check dx"}, {a[6] = 0, b[6] = Tnematiconset,
c[6] = (kref[[1]]*
Exp[(-Earef[[1]]/
8.314)*((1/(Tnematiconset + 273.15)) - (1/
Tref[[1]]))])^(1/nref[[1]]),
d[6] = (c[7]*
nref[[1]]*(1 -
initialxt)*(-Log[1 - initialxt])^((nref[[1]] - 1)/
nref[[1]]))/q[[j]],
e[6] = (b[6] - b[7])*d[6] + initialxt,
f[6] = e[6]*dhnematicmax, g[6] = e[6]}}(*,*)
](*]; ... // 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[1],
b[i] = ((b[i - 1] + 273.15) - b[2]*a[i]) - 273.15,
c[i] = (kref[[1]]*
Exp[(-Earef[[1]]/
8.314)*((1/(b[i] + 273.15)) - (1/Tref[[1]]))])^(1/
nref[[1]]),
d[i] = (c[i + 1]*
nref[[1]]*(1 -
e[i - 1])*(-Log[1 - e[i - 1]])^((nref[[1]] - 1)/
nref[[1]]))/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[8]
and c[8]
, that is, b[i + 1]
and c[i + 1]
are undefined when i = 7
.
tbl[7]
(* {{1.*10^-8, 100., 17.4386, 65.4859 c[8],
0.0000413008 + 65.4859 (100. - b[8]) c[8],
200. (0.0000413008 + 65.4859 (100. - b[8]) c[8]),
0. + 65.4859 (100. - b[8]) c[8]}} *)
With maximum i = 8
, the first row has values for b[8]
and c[8]
, but there are undefined values for b[9]
and c[9]
.
tbl[8]
(* {{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[9],
0.0000435848 + 64.7685 (100. - b[9]) c[9],
200. (0.0000435848 + 64.7685 (100. - b[9]) c[9]),
0. + 64.7685 (100. - b[9]) c[9]}} *)
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[32],
0.000322286 + 42.9976 (100. - b[32]) c[32],
200. (0.000322286 + 42.9976 (100. - b[32]) c[32]),
0. + 42.9976 (100. - b[32]) c[32]}
} *)
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[6]
to g[6]
, but initial values for c[8]
and b[8]
are missing, or maybe c[i + 1]
and b[i + 1]
are incorrect in the Table
statement.