0
$\begingroup$

Why are the k-values 0,10 not showing in the TableForm below?

TableForm[RecurrenceTable[{u[k + 1] == (9/10)*u[k] + s[k] + k,    
s[k + 1] == (9/10)*s[k] + (1/10)*u[k], u[0] == 50, s[0] == 20}, {u,
s}, {k, 0, 10}], TableHeadings -> {{}, {"k","u[k]", "s[k]"}}]
$\endgroup$
2
$\begingroup$

You can't simply refer to k outside the scope of RecurrenceTable, but there are plenty of ways to add in a value.

TableForm[MapIndexed[Flatten[{#2 - 1, #1}] &,
  RecurrenceTable[{u[k + 1] == (9/10)*u[k] + s[k] + k,
    s[k + 1] == (9/10)*s[k] + (1/10)*u[k], u[0] == 50, s[0] == 20}, {u,
    s}, {k, 0, 10}]], TableHeadings -> {None, {"k", "u[k]", "s[k]"}}]

or

{kmin, kmax} = {0, 10};
TableForm[MapThread[Prepend,
  {RecurrenceTable[{u[k + 1] == (9/10)*u[k] + s[k] + k,
     s[k + 1] == (9/10)*s[k] + (1/10)*u[k], u[0] == 50, s[0] == 20}, {u,
     s}, {k, kmin, kmax}], Range[kmin, kmax]}], TableHeadings -> {None,
   {"k", "u[k]", "s[k]"}}]
$\endgroup$
1
  • $\begingroup$ All: Each of your recommendations was very innovative and most helpful; many thanks to all ... prg $\endgroup$ – PRG Nov 4 '19 at 0:13
1
$\begingroup$

I don't have any reputation so I can't comment on or upvote Chris's answer, consider this an addendum (with an additional solution at the end):

We know TableForm should have a column of data for each heading (in this case 3 columns).

Dimensions of original data (11 rows, 2 columns):

In[]:= Dimensions@
 RecurrenceTable[{u[k + 1] == (9/10)*u[k] + s[k] + k, 
   s[k + 1] == (9/10)*s[k] + (1/10)*u[k], u[0] == 50, s[0] == 20}, {u,
    s}, {k, 0, 10}]

Out[]= {11, 2}

Dimensions of Chris's Data (11 rows, 3 columns):

In[]:= Dimensions@
 MapIndexed[Flatten[{#2 - 1, #1}] &, 
  RecurrenceTable[{u[k + 1] == (9/10)*u[k] + s[k] + k, 
    s[k + 1] == (9/10)*s[k] + (1/10)*u[k], u[0] == 50, 
    s[0] == 20}, {u, s}, {k, 0, 10}]]

Out[]= {11, 3}

Here's another way to get 3 columns:

TableForm[
 count = 0; 
 Table[Prepend[row, count++], {row, #}] &@
  RecurrenceTable[{u[k + 1] == (9/10)*u[k] + s[k] + k, 
    s[k + 1] == (9/10)*s[k] + (1/10)*u[k], u[0] == 50, 
    s[0] == 20}, {u, s}, {k, 0, 10}], 
 TableHeadings -> {{}, {"k", "u[k]", "s[k]"}}]
$\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.