0
$\begingroup$

I want to get the recurrence relation for several values of n in a grid form. At the moment I am using

RecurrenceTable[{a[n + 2] == (1 - n)/(n + 1) a[n], a[2] == a0, a[3] == 0}, a, {n, 2, 20}]

To get

{a0, 0, -(a0/3), 0, a0/5, 0, -(a0/7), 0, a0/9, 0, -(a0/11), 0, a0/13, 0, -(a0/15), 
 0, a0/17, 0, -(a0/19)}

But I need it with the values of $n$ in one column running from 2 to 20 with heading n and the above list as a second column with heading a[n]. I tried something funny but it failed horribly:

Grid[Table[{a0, 0, -(a0/3), 0, a0/5, 0, -(a0/7), 0, a0/9, 0, -(a0/11), 0,
            a0/13, 0, -(a0/15), 0, a0/17, 0, -(a0/19)},
           {i, {i, 2, 20}}
     ]
]
$\endgroup$

1 Answer 1

2
$\begingroup$

I think this does what you want.

list = RecurrenceTable[{a[n + 2] == (1 - n)/(n + 1) a[n], a[2] == a0, a[3] == 0}, 
   a, {n, 2, 20}];

TableForm[{Range[2, 20], list}, TableHeadings -> {{"n", "a[n]"}}, TableDirections -> Row]

For Mathematica versions 10.0, 10.1 or 10.2 add the option TableAlignments -> Center to get around this bug:

$\endgroup$
11
  • $\begingroup$ The specified setting for the option GridBoxOptions, RowAlignments cannot be used. $\endgroup$
    – rhermans
    Feb 16, 2016 at 17:28
  • $\begingroup$ @rhermans Yes, which is what I need to look at. It appears to be a bug in TableForm. Nevertheless the output appears correct, no? $\endgroup$
    – Mr.Wizard
    Feb 16, 2016 at 17:30
  • 1
    $\begingroup$ @rhermans Turns out it's a known bug I hadn't noticed until today: (95321) $\endgroup$
    – Mr.Wizard
    Feb 16, 2016 at 17:32
  • $\begingroup$ You are correct. +1 $\endgroup$
    – rhermans
    Feb 16, 2016 at 17:35
  • $\begingroup$ Thanks. Although it gives an error, it works. $\endgroup$
    – Zack Fair
    Feb 16, 2016 at 18:00

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.