2
$\begingroup$

Is there a way to change how numbers are displayed in the output from RecurrenceTable?

stepSize = .5; stepNum = 5; t0 = -1; y0 = -1; f[y_, t_] := y^2 - t;

Grid[Prepend[
RecurrenceTable[{k[n + 1] == k[n] + 1, t[n + 1] == t[n] + stepSize, 
y[n + 1] == m[n]*stepSize + y[n], y[0] == y0, k[0] == 0, t[0] == t0,
m[0] == f[y[0], t[0]], 
m[n + 1] == f[m[n]*stepSize + y[n], t[n] + stepSize]}, {k, t, y, 
m}, {n, stepNum}], {"k", "t", "y[t]", "m"}], Frame -> All]

RecurrenceTable output

For example, in the output above RecurrenceTable changes the integer values in the first column, k, to floating point. I'd like for the k column (step number) to display as an integer. I've tried applying functions like Style, IntegerPart, etc. in RecurrenceTable as could be used in Table, but they generate errors.

Also, I used RecurrenceTable here instead of Table because one row element requires the results of a previous row's information in its calculation. This kind of table is fairly simple to create in Excel. Is there a simpler/easier way to emulate this kind of Excel table functionality in Mathematica? RecurrenceTable seems like overkill for this purpose.

$\endgroup$
  • $\begingroup$ You could define stepsize = 1/2 rather than using machine precision numbers. That will generate exact results rather than approximate ones, including the k step counters. $\endgroup$ – MarcoB Mar 8 '16 at 21:55
  • $\begingroup$ @MarcoB: thanks, but I would like the other three columns to remain as decimals. Is there a way to only affect the k column? $\endgroup$ – BCott Mar 8 '16 at 22:02
  • $\begingroup$ Are you implementing the Euler method here? You could use NDSolve[] instead $\endgroup$ – J. M. will be back soon Mar 8 '16 at 23:14
  • $\begingroup$ @J.M., yes, but just as a demonstration of the how the Euler method works, not to generate a solution to a DE $\endgroup$ – BCott Mar 8 '16 at 23:22
  • $\begingroup$ @MarcoB, thank you. Exactly what I needed! $\endgroup$ – BCott Mar 8 '16 at 23:36
2
$\begingroup$

You can Rationalize only the first member of each list returned by RecurrenceTable, e.g. by Applying the following modifier function:

{Rationalize@#1, ##2} &

at each level. #1 represents the first argument in the sequence supplied to a function; ##2 represents the sequence of all arguments, starting with the second one (see SlotSequence).

In other words:

results = RecurrenceTable[
  {k[n + 1] == k[n] + 1,
   t[n + 1] == t[n] + stepSize,
   y[n + 1] == m[n]*stepSize + y[n],
   y[0] == y0, k[0] == 0, t[0] == t0, m[0] == f[y[0], t[0]],
   m[n + 1] == f[m[n]*stepSize + y[n], t[n] + stepSize]},
  {k, t, y, m}, {n, stepNum}
 ];

{Rationalize@#1, ##2} & @@@ results

(* Out:
{{0, -1., -1., 2.}, {1, -0.5, 0., 0.5}, {2, 0., 0.25, 0.0625}, {3, 0.5, 0.28125, -0.420898}, 
 {4, 1., 0.0708008, -0.994987}, {5, 1.5, -0.426693, -1.31793}}
*)

You can use this in your grid expression for further formatting:

Grid[
  Prepend[
    {Rationalize@#1, ##2} & @@@ results,
    {"k", "t", "y[t]", "m"}
  ],
  Frame -> All
]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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