1
$\begingroup$

At its core, mine is a very simple communication error between me and Mathematica that I haven't been able to crack, so I do apologise if it seems embarassingly inane.

Please consider the following simplified set-up. I have x[-1] and I seek an expression for x[5] using Table:

root = Table[Solve[2x[b]+x[b-1]+b == 0, x[b]] /.x[-1]->1, {b,0,5}]

The output:

{{{x[0]->-(1/2)}}, {{x[1] -> 1/2 (-1 - x[0])}}, {{x[2] -> 1/2 (-2 - x[1])}},

{{x[3] -> 1/2 (-3 - x[2])}}, {{x[4] -> 1/2 (-4 - x[3])}},

{{x[5] -> 1/2 (-5 - x[4])}}}.

(I don't have the right lingo for this, but) the output elements don't engage like numbers. If I were to try something like root[[1]]+1,

I would get

{{1 + (x[0] -> -(1/2))}}

(and Rules->Equal just changes that -> to =; the behaviour remains the same).

Is it possible to define these x[b] such that back-substitution happens while Table is at work? If not, can you please advise on how to define individual output elements (without directly assigning values from the output to each x[b])?

As an aside, is it better Mathematica practice to abandon this whole Table business and use a loop instead?

So many thanks in advance!

--PhysicsHobbit

$\endgroup$
1
$\begingroup$

This seems to be a task for RecurrenceTable:

ClearAll[x, b]
RecurrenceTable[{2 x[b] + x[b - 1] + b == 0, x[-1] == 1}, x, {b, 0, 5}]
{-(1/2), -(1/4), -(7/8), -(17/16), -(47/32), -(113/64)}

Alternatively, you can use RSolve:

ClearAll[func]
func = x /. RSolve[{2 x[b] + x[b - 1] + b == 0, x[-1] == 1}, x, b][[1]];

func /@ Range[0, 5]
 {-(1/2), -(1/4), -(7/8), -(17/16), -(47/32), -(113/64)}

If you have to use Table + Solve you can do

roots = Flatten @ Table[Solve[2 x[b] + x[b - 1] + b == 0, x[b]] /. x[-1] -> 1, 
  {b, 0,  5}]; 
Fold[# /. #2 &, roots[[All, 2]], Reverse[roots]]
 {-(1/2), -(1/4), -(7/8), -(17/16), -(47/32), -(113/64)}
$\endgroup$
0

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.