-1
$\begingroup$

The following code is supposed generates a table based on different calculations for a given q (e.g. q[1] for this example):

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};

ttall = TableForm[
  Join[{{"Delta t (s)", b[1] = deltat}, {"q (K/s)", 
     b[2] = q[[1]]}, {""}, {""}, {"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] = 90.01, 
     c[6] = (kref[[1]]*
         Exp[(-Earef[[1]]/
             8.314)*((1/(90.01 + 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[[1]], e[6] = (b[6] - b[7])*d[6] + initialxt,
      f[6] = e[6]*90.01, g[6] = e[6]}}, 
   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[[1]], 
     e[i] = (b[i] - b[i + 1])*d[i] + e[i - 1], f[i] = e[i]*90.01, 
     g[i] = e[i] - e[i - 1]}, {i, 7, numberofrows - 9}]]]

What I want is to get a code where I can get the same table for the 10 different values of q. I tried using a Doloop putting different values of q (e.g. q[[j]]) such as:

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] = 90.01, 
       c[6] = (kref[[1]]*
           Exp[(-Earef[[1]]/
               8.314)*((1/(90.01 + 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]*90.01, 
       g[6] = e[6]}}, 
     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]*90.01, 
       g[i] = e[i] - e[i - 1]}, {i, 7, numberofrows - 10}]]] // 
   Print, {j, 1, 10, 1}]

But it does not seem to work and I don't understand why or how to fix it. Edit: The problem with the Do loop is that some of the values from the second table and on do not seem to be computing. The values which usually do not compute from table 2 and beyond are those from column 4,5,6,7 or in my notation d,e,f and g. This happens when numberofrows is bigger than 21 for some reason. Here's a picture of how the second table (meaning q=0.3) look like for numberofrows=30 and when using the Do loop:

image

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?

PS: This is a corrected and revised version than I previously asked question here: Problem with Do Loop and TableForm . I made sure it works and it is more clear now.

I will appreciate your comments

$\endgroup$
7
  • $\begingroup$ Hi Bill! Thank you!. I forgot to mention but I also tried that as ......g[i] = e[i] - e[i - 1]}, {i, 7, numberofrows - 9},{j, 1, 10} and it does not work. I am not sure if that is the position where {j, 1, 10} should go to make it work $\endgroup$
    – John
    Commented May 2, 2020 at 17:43
  • 1
    $\begingroup$ Isn't this the same question you asked here? $\endgroup$
    – MarcoB
    Commented May 2, 2020 at 18:35
  • 1
    $\begingroup$ @John, my answer to your question about why you must change {i, 7,numberofrows - 9} still explains the same problem with this new version. But also, the problem I mentioned is the reason why the table 2 has incorrect values for d, e, f and g. $\endgroup$
    – creidhne
    Commented May 3, 2020 at 17:51
  • 1
    $\begingroup$ @John, When table 1 is computed, each d[i], e[i], f[i], and g[i] in the Table statement depends on undefined values for b[i+1] and c[i+1]. However, when table 2 is computed, these b[i+1] and c[i+1] values are not undefined, because their values remain from table 1, so instead of using undefined b[i+1] and c[i+1] for table 2, you use the values from table 1. You can solve that with Do[ Clear[a,b,c,d,e,f,g]; ... Still, the last row will show undefined values (e.g., b[31] and c[31]). $\endgroup$
    – creidhne
    Commented May 3, 2020 at 17:51
  • 2
    $\begingroup$ @John in the future, it is better to update your original question in these cases, when you update and revise the question (but it remains overall the same). $\endgroup$ Commented May 4, 2020 at 21:30

1 Answer 1

0
$\begingroup$

Let's fix the problem that requires you to change {i,7,numberofrows-9} after you exit Mathematica. Also, make changes so that the data is correctly computed and saved. The fixes make your data much easier to use!

The biggest, most important change is to remove TableForm from your data. It's a mistake to use TableForm when you compute your data. TableForm is for displaying data. Bob Hanlon commented about this same mistake when he said:

I do not understand your comment. However, in general since TableForm is a wrapper and intended only for display, you do not want it included in a definition of data. That is rather than data = TableForm[datavalues] you would instead use TableForm[data = datavalues]. That way the definition of data does not contain the wrapper.

Let's fix the TableForm problem! Here are the reasons for some of important changes.

  1. Another reason to remove TableForm is that your method is much easier to understand and test. In the end, the tables have numbers you can use for plots and export, etc. Simply stated, TableForm makes a mess of the tables.
  2. The method you're using requires b[i+1] and c[i+1] to compute row i. I haven't changed that, but we can eliminate the undefined values in the last row. Let's compute one more row than we need (that means change {i,7,numberofrows-9} to {i,7,numberofrows-9 + 1}), and then remove the last row of each table.
  3. When we remove the undefined values, you can exit Mathematica, and when you restart, you won't need to change {i,7,numberofrows-9+1} to get good numbers.
  4. Clear[a,b,c,d,e,f,g] is necessary because the values for table j must not be used for table j+1.
  5. We want to make a table for each value of q, so change {j,1,10} to {j,Length[q]}.
  6. Save the tables as tbl[j]. For example, tbl[2] is the table for q[[2]] = 0.3;

If something isn't clear, or if you find a problem, please comment.

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*^-12;(*shortcut for 1*10^-12*)
q = {0.1, 0.3, 1, 3, 10, 30, 100, 300, 600, 1000};
rowNames = {"Delta t (s)", "q (K/s)"};
colNames = {"Time(s)", "T[C]", "K(T)=k^(1/n)", "dx/dT", "x(t)", 
   "DH,aged-DH,unaged (J/g)", "Check dx"};
Clear[tbl];(*because numberofrows or Length[q] can change*)
Do[
  Clear[a, b, c, d, e, f, g];
  b[1] = deltat;
  b[2] = q[[j]];
  tbl[j] = Join[{{
      a[6] = 0,
      b[6] = 90.01,
      c[6] = (kref[[1]]*Exp[(-Earef[[1]]/8.314)*
            ((1/(90.01 + 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]*90.01,
      g[6] = e[6]}},
    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]*90.01,
      g[i] = e[i] - e[i - 1]},
       {i, 7, numberofrows - 9 + 1}]
    ],(*end Join*)
  {j, Length[q]}];
(*remove last row from all tables*)
(tbl[#] = tbl[#][[;; -2]]) & /@ Range[Length[q]];
(*combine all tables*)
ttall = Flatten[Join[tbl /@ Range[Length[q]]], 1];

With the data in the tables, tbl[j], print every table:

Print[Column[{
  TableForm[{deltat, q[[#]]}, TableHeadings -> {rowNames, None}],
  " ",
  TableForm[tbl[#], TableHeadings -> {None, colNames}],
  " "
}]] & /@ Range[Length[q]];

You can get a quick look at any of the tables. For example, to display table 10:

tbl[10] // TableForm

It's easy to display columns from a table and rearrange them. For example, display columns for "Time(s)", "x(t)" and "dx/dT" from table 6:

tbl[6][[All, {1, 5, 3}]] // TableForm

Here's a way you can plot graphs of data from any of the tables:

ListPlot[tbl[1][[All,{1,4}]],Joined->True,PlotRange->All,PlotLabels->"dx/dT"]

dx/dT as values of time

ListPlot[tbl[5][[All,{1,7}]],Joined->True,PlotRange->All,PlotLabels->"Check dx"]

Check dx as values of time

$\endgroup$
4
  • $\begingroup$ creidhne great explanation! This is crystal clear! I really appreciate this explanation! $\endgroup$
    – John
    Commented May 4, 2020 at 20:39
  • $\begingroup$ creidhne, when you do this step (tbl[#] = tbl[#][[;; -2]]) & /@ Range[Length[q]]; to remove the last row of all tables, could you tell me what step of code would I need to do in order to shift by one row all the data from column d to column g leaving the columns of columns through a and c as they are?. Pretty much in a similar fashion of what was asked here: mathematica.stackexchange.com/questions/221065/… but only for those columns. Somehow the same commands are not working for me as solved in the question for this code. Thank you $\endgroup$
    – John
    Commented May 4, 2020 at 23:47
  • 1
    $\begingroup$ @John, using kglr's method, you can make a shifted copy of all the tables with: Clear[shifted]; (shifted[#]=Join[Most[tbl[#][[All,1;;3]]],Rest[tbl[#][[All,4;;-1]]],2])&/@Range[Length[q]];. $\endgroup$
    – creidhne
    Commented May 5, 2020 at 15:52
  • $\begingroup$ creidhe! one last question about this code if you don't mind. I now need to run the same code but with a condition. If the temperature (which is b[i] in the code) is greater than 74, I need to run the code as it is but if the temperature is lower than 74, then kref[[1]],nref[[1]] and Earef[[1]] need to be change to kref[[2]],nref[[2]] and Earef[[2]]. Could you please tell me how to introduce this condition?. I tried using Which[b[i]]>74,Join[{{ a[6] = 0, b[6] = 90.01...etc] but it does not work like this. Thank you $\endgroup$
    – John
    Commented May 7, 2020 at 2:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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