Here is something not far from your example:

Collecting data using Sow
and Reap
as shown by cormullion:
Sow[{i, a, b, La, Mu, Row@{tthetaLa, tLacalc}, Row@{tthetaMu, tMucalc}}]
We get:
dat =
{{1, -3, 5, 0.0557281, 1.94427, Row[{0.114562, "*"}], Row[{7.66874, "*"}]}, {2, -3,
1.94427, -1.11146, 0.0557281, Row[{-0.987578, "*"}], Row[{0.114562, " "}]}, {3, -3,
0.0557281, -1.83282, -1.11146, Row[{-0.306418, "*"}],
Row[{-0.987578, " "}]}, {4, -1.83282, 0.0557281, -1.11146, -0.665631,
Row[{-0.987578, " "}],
Row[{-0.888198, " "}]}, {5, -1.83282, -0.665631, -1.38699, -1.11146,
Row[{-0.850238, "*"}],
Row[{-0.987578, " "}]}, {6, -1.38699, -0.665631, -1.11146, -0.941166,
Row[{-0.987578, " "}],
Row[{-0.996539, " "}]}, {7, -1.11146, -0.665631, -0.941166, -0.835921,
Row[{-0.996539, " "}],
Row[{-0.973078, " "}]}, {8, -1.11146, -0.835921, -1.00621, -0.941166,
Row[{-0.999961, "*"}],
Row[{-0.996539, " "}]}, {9, -1.11146, -0.941166, -1.04641, -1.00621,
Row[{-0.997846, "*"}], Row[{-0.999961, " "}]}}
And produce the table shown above with:
headings = {"k", "\!\(\*SubscriptBox[\"a\", \"k\"]\)",
"\!\(\*SubscriptBox[\"b\", \"k\"]\)", "\!\(\*SubscriptBox[\"\[Lambda]\", \"k\"]\)",
"\!\(\*SubscriptBox[\"\[Mu]\", \"k\"]\)",
"\[Theta](\!\(\*SubscriptBox[\"\[Lambda]\", \"k\"]\))",
"\[Theta](\!\(\*SubscriptBox[\"\[Mu]\", \"k\"]\))"};
headings2 = Item[Style[#, Italic], Alignment -> 1] & /@ headings;
divs = # -> AbsoluteThickness@#2 & @@@ {{1, 3}, {2, 2}, {-1, 2}};
Grid[
dat ~Prepend~ headings2,
Dividers -> {{}, divs},
Alignment -> ".",
BaseStyle -> FontFamily -> "Calibri"
]
(Sorry about the horrible looking code for headings
; it is simply a list of formatted strings in the Front End.)
As requested, here is complete code to generate the dat
expression shown above:
itmax = 10;
tolerancia = 0.2;
alfa = (-1 + Sqrt[5])/2;
ttheta[Lambda_] := Lambda^2 + 2*Lambda;
a = -3;
b = 5;
La = a + (1 - alfa)*(b - a) // N;
Mu = a + alfa*(b - a) // N;
tthetaLa = ttheta[La];
tLacalc = "*";
tthetaMu = ttheta[Mu];
tMucalc = "*";
dat =
Reap[
Do[L = b - a;
Sow[{i, a, b, La, Mu, Row@{tthetaLa, tLacalc}, Row@{tthetaMu, tMucalc}}];
If[tthetaLa > tthetaMu, If[b - a < tolerancia, Break[]];
a = La;
La = Mu;
Mu = a + alfa (b - a);
tthetaLa = tthetaMu;
tLacalc = " ";
tthetaMu = ttheta[Mu];
tMucalc = " ";, b = Mu;
Mu = La;
La = a + (1 - alfa) (b - a);
tthetaMu = tthetaLa;
tMucalc = " ";
tthetaLa = ttheta[La];
tLacalc = "*";], {i, 1, itmax}]
][[2, 1]]
It will be necessary to read the documentation for Sow
and Reap
to understand this. Also, func @ arg
is equivalent to func[arg]
, I just prefer to use the former sometimes. The [[2, 1]]
after Reap
is a syntax for Part
, and is used to extract (only) the sowed expression from the returned value of Reap
. (Again, see the documentation for Reap
.)
Grid
orTableForm
and get rid of all yourPrint
statements - the Mathematica workflow offers much better solutions. $\endgroup$Print
statements. Focus your code on creating the data you want as a result, then, as Yves has suggested, you can output that data in almost any form you desire using functions likeGrid
orTable
. You may want to look at the functional programming features of Mathematica which would help make your code much more concise. $\endgroup$