3
$\begingroup$

Hi I have this code which runs fine, however it does not print things at the end in nice TableForm. I want it to print things nicely in the form of a table with some headings. However if I use TableForm it only print last iteration results not all. How can I achieve this. Thanks in advance

        xs = Array[x, 3];
        newF[list_List] := (#[[1]]^2 #[[2]] - 1) & /@ Partition[list, 2, 1, {1, 1}]
        newG[xs_List] := RotateLeft[1/Sqrt[xs]]
        newJac[xs : {Symbol__}] := D[#, {xs}] & /@ newG[xs];

        eps = 10^-16000;
        dim = 3;
        pre = 10000;
        max = 10;
        a = 1;
        b = Norm[newF[ts]];
        ts = SetAccuracy[ConstantArray[8/10, dim], pre];
        k = 0;
        While[k <= max && a + b > eps, 
          M = SetAccuracy[-newJac[xs] /. Thread[xs -> ts], pre];
          J = SetAccuracy[
            Inverse[IdentityMatrix[dim] - newJac[xs] /. Thread[xs -> ts]], 
            pre];
          hs = newG[newG[J . M . ts + J . newG[ts]]];
          a = SetAccuracy[Norm[(ts - hs)], pre];
          b = SetAccuracy[Norm[newF[hs]], pre];
          p[k] = hs;
          
          Print["k=", k, "  ", "a=", NumberForm[a, 20], "  ", "b=", NumberForm[b, 20], "  ", "hs=", NumberForm[hs, 20]];
          ts = hs;
          k = k + 1
          ];
        (*TableForm[Table[NumberForm[{n,a[n],b[n],t[n],a[n]+b[n],p[n]},10],{n,0,11}]]*)
        Print["The number of iterations is:" NumberForm[(k), 2]];
        Print["a+b=" NumberForm[a + b, 20]]
$\endgroup$

1 Answer 1

2
$\begingroup$
eps = 10^-16000;
dim = 3;
pre = 10000;
max = 10;
a = 1;
b = Norm[newF[ts]];
ts = SetAccuracy[ConstantArray[8/10, dim], pre];

k = 0;
list = {};

To get a clearer display I reduced the NumberForm precision to 2. You can of course change that back to any value you want.

While[k <= max && a + b > eps, 
  M = SetAccuracy[-newJac[xs] /. Thread[xs -> ts], pre];
  J = SetAccuracy[
    Inverse[IdentityMatrix[dim] - newJac[xs] /. Thread[xs -> ts]], 
    pre];
  hs = newG[newG[J . M . ts + J . newG[ts]]];
  a = SetAccuracy[Norm[(ts - hs)], pre];
  b = SetAccuracy[Norm[newF[hs]], pre];
  p[k] = hs;
  ts = hs;
  AppendTo[list, {k, NumberForm[a, 2], NumberForm[b, 2], NumberForm[hs, 2]}];
  k = k + 1];

list // TableForm[#, TableHeadings -> {{}, {"k", "a", "b", "hs"}}] &

enter image description here

Update

For more styling options you should use Grid:

Grid[Prepend[{"k", "a", "b", "hs"}] @ list,
 Dividers -> {{2 -> Black}, {2 -> Black}}]

enter image description here

Grid[Prepend[{"k", "a", "b", "hs"}] @ list,
 Background -> {{{None, None}}, {{GrayLevel[0.9], None}}},
 Dividers -> {{1 -> Black, 2 -> Black, -1 -> Black}, {1 -> Black, 2 -> Black, -1 -> Black}}]

enter image description here

$\endgroup$
6
  • $\begingroup$ Amazing! Thank you. $\endgroup$
    – Learner
    Commented Oct 14, 2023 at 11:06
  • $\begingroup$ You're most welcome - and thanks for the accept :) $\endgroup$
    – eldo
    Commented Oct 14, 2023 at 11:07
  • $\begingroup$ A quick question. How can I print the k left to the vertical and line and all other to the right? $\endgroup$
    – Learner
    Commented Oct 14, 2023 at 12:29
  • $\begingroup$ See update. Grid has much more options. $\endgroup$
    – eldo
    Commented Oct 14, 2023 at 14:28
  • $\begingroup$ Thanks once again $\endgroup$
    – Learner
    Commented Oct 14, 2023 at 16:27

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.